sindresorhus / electron-store

Simple data persistence for your Electron app or module - Save and load user preferences, app state, cache, etc
MIT License
4.62k stars 151 forks source link

Use expiration #125

Open waghcwb opened 4 years ago

waghcwb commented 4 years ago

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 commented 4 years ago

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);
    }
  }
}
sindresorhus commented 4 years ago

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

asger-finding commented 2 years ago

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!