client-cache: remove pulling (#1620)

* client-cache: remove pulling

* client-cache: fix wrong ts field

* client-cache: purge cache on any local update
This commit is contained in:
Xiaochen Cui
2025-09-25 16:56:01 -07:00
committed by GitHub
parent 8d36e52ae7
commit 7bfa85d4fe
7 changed files with 20 additions and 33 deletions

View File

@@ -107,16 +107,13 @@ export class PuterJSFileSystemModule extends AdvancedBase {
});
this.bindSocketEvents();
// Start cache monitoring
this.startCacheMonitoring();
}
bindSocketEvents() {
this.socket.on('cache.updated', (item) => {
const local_ts = puter._cache.get(LAST_UPDATED_TS);
if (item.ts > local_ts || local_ts === undefined) {
console.log(`remote timestamp (${item.ts}) is newer than local timestamp (${local_ts}), flushing cache`);
if (item.timestamp > local_ts || local_ts === undefined) {
console.log(`remote timestamp (${item.timestamp}) is newer than local timestamp (${local_ts}), flushing cache`);
puter._cache.flushall();
}
});
@@ -211,7 +208,6 @@ export class PuterJSFileSystemModule extends AdvancedBase {
updateCacheTimestamp() {
// Add 1 second to mitigate clock skew and disable self-update.
puter._cache.set(LAST_UPDATED_TS, Date.now() + 1000);
console.log(`set last updated ts to ${puter._cache.get(LAST_UPDATED_TS)}`);
}
/**
@@ -228,7 +224,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
utils.setupXhrEventHandlers(xhr, undefined, undefined, async (result) => {
try {
const response = typeof result === 'string' ? JSON.parse(result) : result;
resolve(response.timestamp || response.ts || Date.now());
resolve(response.timestamp || Date.now());
} catch (e) {
reject(new Error('Failed to parse response'));
}
@@ -237,30 +233,4 @@ export class PuterJSFileSystemModule extends AdvancedBase {
xhr.send();
});
}
/**
* Starts the cache monitoring service that calls the cache API every 5 seconds.
*
* @memberof PuterJSFileSystemModule
* @returns {void}
*/
startCacheMonitoring() {
if (this.cacheMonitoringInterval) {
clearInterval(this.cacheMonitoringInterval);
}
this.cacheMonitoringInterval = setInterval(async () => {
try {
const remoteTimestamp = await this.getCacheTimestamp();
const localTimestamp = puter._cache.get(LAST_UPDATED_TS);
if (remoteTimestamp > localTimestamp || localTimestamp === undefined) {
console.log(`remote timestamp (${remoteTimestamp}) is newer than local timestamp (${localTimestamp}), flushing cache`);
puter._cache.flushall();
}
} catch (error) {
console.error('Failed to get cache timestamp:', error);
}
}, 5000);
}
}

View File

@@ -57,6 +57,9 @@ const copy = function (...args) {
}));
this.updateCacheTimestamp();
// TOOD (xiaochen): puter desktop will have stale cache without this, find out why
puter._cache.flushall();
})
}

View File

@@ -55,6 +55,9 @@ const mkdir = function (...args) {
}));
this.updateCacheTimestamp();
// TOOD (xiaochen): puter desktop will have stale cache without this, find out why
puter._cache.flushall();
})
}

View File

@@ -68,6 +68,8 @@ const move = function (...args) {
this.updateCacheTimestamp();
// TOOD (xiaochen): puter desktop will have stale cache without this, find out why
puter._cache.flushall();
})
}

View File

@@ -52,6 +52,9 @@ const rename = function (...args) {
xhr.send(JSON.stringify(dataToSend));
this.updateCacheTimestamp();
// TOOD (xiaochen): puter desktop will have stale cache without this, find out why
puter._cache.flushall();
})
}

View File

@@ -433,6 +433,9 @@ const upload = async function(items, dirPath, options = {}){
xhr.send(fd);
this.updateCacheTimestamp();
// TOOD (xiaochen): puter desktop will have stale cache without this, find out why
puter._cache.flushall();
})
}

View File

@@ -57,6 +57,9 @@ const write = async function (targetPath, data, options = {}) {
this.updateCacheTimestamp();
// TOOD (xiaochen): puter desktop will have stale cache without this, find out why
puter._cache.flushall();
// perform upload
return this.upload(data, parent, options);
}