You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
A common problem with LocalStorage is the in-ability to set an expiration date on a specified key. This can be frustrating when the value of the key doesn't really change all that often, but when it does; since the key does not expire we do not necessarily know when to try to check again.
Describe the solution you'd like
I think a possible solution is to create an internal wrapper that takes the string or model that is provided and appends it to a custom model that also includes a TTL (where TTL is in milliseconds) field.
Example: await SetItemAsync(key, model, ttl);
if TTL is provided wrap the model in something like this:
class ExpiryModel<T>{
int Ttl;
T Data;
}
During the Get operation evaluate the TTL to see if it is still valid, if not then return back as though it was not found.
An added nicety would be to have a method to search all expired keys and remove them.
Hi @b1tzer0, thanks for making this suggestion. I'm open to this as long as we implement it in a way that makes it optional and non-breaking to the existing APIs. An additional overload to a few methods could be a nice solution. It would then be on developers to know if they are expecting the wrapper object back from local storage or not.
Is your feature request related to a problem? Please describe.
A common problem with LocalStorage is the in-ability to set an expiration date on a specified key. This can be frustrating when the value of the key doesn't really change all that often, but when it does; since the key does not expire we do not necessarily know when to try to check again.
Describe the solution you'd like
I think a possible solution is to create an internal wrapper that takes the string or model that is provided and appends it to a custom model that also includes a TTL (where TTL is in milliseconds) field.
Example:
await SetItemAsync(key, model, ttl);
if TTL is provided wrap the model in something like this:
During the Get operation evaluate the TTL to see if it is still valid, if not then return back as though it was not found.
An added nicety would be to have a method to search all expired keys and remove them.
Additional context
There are several examples on the web on how to do this with JavaScript, here is just one example:
https://digitalfortress.tech/js/localstorage-with-ttl-time-to-expiry/
The text was updated successfully, but these errors were encountered: