-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
persistor.getSize() and maxSize
not supported when serialize is false
#427
Comments
So I've tried to implement local work arounds for this, but I've found other areas where changes will need to be made in order to support non-string data. For example, the |
maxSize
not supported when serialize is false
@jimbuck As you have mentioned this could be a tricky change. For the moment it is easy to say that for serialize false limits will not be enforced. Code we have in repo is not the best as typically we should have storage limits enforced and event that handles storage limit and wipes it out. Calculating size is very heavy operation and totally redundant when indexed db database have native methods to enforce size and handle overflows. That being said. I think for advanced cases we can recommend to avoid using internal method and implement your own. |
@jimbuck any thoughts on the previous comment? If you’ve gone a different direction or have found a solution I’ll close this issue. |
We did come up with a work-around but it just replaces some methods on the persistor and storage instances (this is from 0.10.0): this.persistor = new CachePersistor({
cache: this.cache,
storage: this.localStorage,
serialize: false,
maxSize: 4.5 * MEGABYTES,
debounce: 5 * SECONDS,
});
this.persistor.storage.getSize = async () => {
return this.localStorage.getSize();
};
// Directly override the persist method to use the getSize method on our localStorage service.
this.persistor.persistor.persist = async () => {
try {
if (this.persistor.persistor.paused) return;
const data = this.cache.extract();
const size = await this.localStorage.getSize();
if (size > (this.persistor.persistor.maxSize ?? 0)) {
console.log(`Purged cache of size ${size} characters`);
await this.persistor.purge();
this.persistor.persistor.paused = true;
return;
}
await this.persistor.storage.write(data);
console.log(`Persisted cache of size ${size} characters`);
} catch (error) {
console.error('Error persisting cache', error);
throw error;
}
};
I am trying to put together a PR that will show it more officially implemented. It should be up soon as a better reference. |
maxSize
not supported when serialize is falsemaxSize
not supported when serialize is false
When I created our work-around, that was in |
I'm utilizing a custom JSON serialization configuration so I've set
serialize
tofalse
and supplied my ownstorage
instance. Unfortunately the built-inStorage
class is written to expect a JSON string for calculating size. That means it always returnsnull
if the result is not a string. Which means the auto-purge doesn't work correctly.apollo-cache-persist/src/Storage.ts
Lines 30 to 38 in 7bcb322
It would make sense to let storage implementations provide their own
getSize
function, but I figured I would file this and get some feedback before starting on a PR.I'm thinking something simple like this:
It's optional, backwards compatible, and gives full control over how size is calculated. Any thoughts?
The text was updated successfully, but these errors were encountered: