electron-store icon indicating copy to clipboard operation
electron-store copied to clipboard

Use expiration

Open waghcwb opened this issue 4 years ago • 3 comments

It would be awesome if we can set a max age for a single key or the whole store.

For example:

store.set('foo.bar', true, { maxAge: expirationDate });

waghcwb avatar Jun 16 '20 15:06 waghcwb

if anyone need a workaround, this should work

const Constants = {
  storage: {
    mykey: 'myvalue',
  }
}

const now = new Date();
const inFiveDays = new Date(now.setDate(now.getDate() + 5));

let mykey

if (!(mykey = storage.get(Constants.storage.mykey))) {
  mykey = fnToGetMyKeys();

  storage.set(Constants.storage.mykey, {
    list: mykey,
    maxAge: inFiveDays,
  });
} else {
  const expiration = new Date(storage.get(Constants.storage.mykey).maxAge);
  const invalidateKeys = [Constants.storage.mykey];

  if (expiration < new Date()) {
    for (const key of invalidateKeys) {
      storage.delete(key);
    }
  }
}

waghcwb avatar Jun 16 '20 16:06 waghcwb

I don't see the use-case for setting a expiration date for the whole store, but I agree it could be useful on a per-key basis.

This will need to be implemented in https://github.com/sindresorhus/conf first and then just copy-paste the new readme docs over here.

We already have a mechanism in place to store internal metadata, which could be used to store the expirations for keys: https://github.com/sindresorhus/conf/blob/410cc144141f1467cc91fe4b16f6b7f42a8afac8/index.js#L39

sindresorhus avatar Jun 20 '20 07:06 sindresorhus

Is still being considered? I'm writing a useragent spoofer and caching it with electron-store.
It'd be super neat to be able to set an expiry time for it!

asger-finding avatar May 17 '22 17:05 asger-finding