Increase cache efficiency

This commit is contained in:
Nariman Jelveh
2025-09-28 21:30:31 -07:00
parent 7382264648
commit ebb0162d79
8 changed files with 36 additions and 35 deletions
+4 -7
View File
@@ -20086,13 +20086,10 @@
}
},
"src/puter-js/node_modules/@heyputer/kv.js": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/@heyputer/kv.js/-/kv.js-0.2.0.tgz",
"integrity": "sha512-qTKOb3DA4jEd9c5NOuWXfBJD/cLQxvqiCSZ85X/nMqkwx0hx/3dkXp1vdNUVexWywte7PrkWCPyPxQVa6RPzyw==",
"license": "MIT",
"dependencies": {
"minimatch": "^9.0.0"
}
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/@heyputer/kv.js/-/kv.js-0.2.1.tgz",
"integrity": "sha512-YhVtzz7ZA/HmuaDvzZZhhUyQWBvp3/TXeY4jULssTdLJwT+tEM4BTYHXttORX+V5auvrYinjj8dNFQnby5T82w==",
"license": "MIT"
},
"src/puter-wisp": {
"name": "@heyputer/puter-wisp",
+32 -16
View File
@@ -125,35 +125,37 @@ export class PuterJSFileSystemModule extends AdvancedBase {
this.socket.on('item.renamed', (item) => {
// check original_client_socket_id and if it matches this.socket.id, don't invalidate cache
if (item.original_client_socket_id !== this.socket.id) {
this.invalidateCache(item);
this.invalidateCache({path: item.old_path, is_dir: item.is_dir ?? true});
}
});
this.socket.on('item.deleted', (item) => {
// check original_client_socket_id and if it matches this.socket.id, don't invalidate cache
if (item.original_client_socket_id !== this.socket.id) {
this.invalidateCache(item);
this.invalidateCache({path: item.path, is_dir: item.is_dir ?? true});
}
});
this.socket.on('item.added', (item) => {
// check original_client_socket_id and if it matches this.socket.id, don't invalidate cache
if (item.original_client_socket_id !== this.socket.id) {
this.invalidateCache(item);
this.invalidateCache({path: item.path, is_dir: item.is_dir ?? true});
}
});
this.socket.on('item.updated', (item) => {
// check original_client_socket_id and if it matches this.socket.id, don't invalidate cache
if (item.original_client_socket_id !== this.socket.id) {
this.invalidateCache(item);
this.invalidateCache({path: item.path, is_dir: item.is_dir ?? true});
}
});
this.socket.on('item.moved', (item) => {
// check original_client_socket_id and if it matches this.socket.id, don't invalidate cache
if (item.original_client_socket_id !== this.socket.id) {
this.invalidateCache(item);
this.invalidateCache({path: item.old_path, is_dir: item.is_dir ?? true});
// invalidate the destination dir
this.invalidateCache({path: path.dirname(item.path), is_dir: true});
}
});
@@ -249,25 +251,39 @@ export class PuterJSFileSystemModule extends AdvancedBase {
* @memberof PuterJSFileSystemModule
* @returns {void}
*/
invalidateCache(item) {
invalidateCache(options) {
console.log('invalidating cache for:', options.path);
// Action: Update last valid time
// Set to 0, which means the cache is not up to date.
localStorage.setItem(LAST_VALID_TS, '0');
// Action: Update cache for the item
if(item?.path){
console.log('invalidated cache for item:', item.path);
// update cache for the item
puter._cache.set('item:' + item.path, item);
if(options?.path){
// delete cache for the item
puter._cache.del('item:' + options.path, options);
// if item is a folder, invalidate the readdir cache for the folder
if(item.is_dir){
puter._cache.del('readdir:' + item.path);
console.log('⮑ invalidated its readdir:', item.path);
// if item is a folder, invalidate the readdir cache for the folder and all its descendants
if(options.is_dir){
puter._cache.del('readdir:' + options.path);
console.log('⮑ invalidated its readdir:', options.path);
// invalidate all descendants readdir and item caches
const descendants_readdir = puter._cache.keys('readdir:' + options.path + '/*');
console.log(descendants_readdir);
for(const descendant of descendants_readdir){
puter._cache.del(descendant);
console.log('⮑ invalidated descendant readdir:', descendant);
}
// invalidate all descendants item caches
const descendants_item = puter._cache.keys('item:' + options.path + '/*');
for(const descendant of descendants_item){
puter._cache.del(descendant);
console.log('⮑ invalidated descendant item:', descendant);
}
}
// invalidate parent folder cache
puter._cache.del('readdir:' + path.dirname(item.path));
console.log('⮑ invalidated its parent readdir:', path.dirname(item.path));
puter._cache.del('readdir:' + path.dirname(options.path));
console.log('⮑ invalidated its parent readdir:', path.dirname(options.path));
}else{
@@ -55,8 +55,6 @@ const copy = function (...args) {
// if user is copying an item to where its source is, change the name so there is no conflict
dedupe_name: (options.dedupe_name || options.dedupeName),
}));
this.invalidateCache();
})
}
@@ -53,8 +53,6 @@ const mkdir = function (...args) {
original_client_socket_id: this.socket.id,
create_missing_parents: (options.recursive || options.createMissingParents) ?? false,
}));
this.invalidateCache(options.path);
})
}
@@ -65,8 +65,6 @@ const move = function (...args) {
new_metadata: (options.new_metadata || options.newMetadata),
original_client_socket_id: options.excludeSocketID,
}));
this.invalidateCache();
})
}
@@ -47,8 +47,6 @@ const rename = function (...args) {
// If dirPath is not provided or it's not starting with a slash, it means it's a relative path
// in that case, we need to prepend the app's root directory to it
dataToSend.path = getAbsolutePathForApp(options.path);
this.invalidateCache(dataToSend.path);
}
xhr.send(JSON.stringify(dataToSend));
@@ -431,8 +431,6 @@ const upload = async function(items, dirPath, options = {}){
// send request
xhr.send(fd);
this.invalidateCache();
})
}
@@ -55,8 +55,6 @@ const write = async function (targetPath, data, options = {}) {
throw new Error({ code: 'field_invalid', message: 'write() data parameter is an invalid type' });
}
this.invalidateCache();
// perform upload
return this.upload(data, parent, options);
}