Refined-github: Caches don't have limits/expirations

Created on 18 Jul 2017  ·  7Comments  ·  Source: sindresorhus/refined-github

We're using the storage to cache the release count and the users' full name. These caches don't expire and they just pile up. There should be a mechanism to either regularly purge the cache or to make them expire.

A thought was to use good ol' cookies in the background.js (yes they work in there) since the expiration is handled natively and new items push out the old items once the limit is reached — just like you'd expect from a cache.

However cookies are limited to 4KB (instead of 5MB), which maybe fits around 100 user names and 40 release counts.

Ideas?

bug help wanted meta

Most helpful comment

I would just go the easy route and purge the cache every x days (100?).

All 7 comments

I would just go the easy route and purge the cache every x days (100?).

The proper way to do it while continuing to use a key-value cache would be to store the current timestamp + time to live in seconds in the value of the cache entry next to the actual value. On every read from the cache you will check if you need to invalidate it based on the current time and the calculated expired timestamp (saving original timestamp + time to live instead of just timestamp to expire is crucial for future changes so you can distinguish young vs. old caches).

Then on every X days or Nth reads, you will iterate over all caches and invalidate the ones which need to be invalidated. This way you're conserving storage and invalidating caches which won't be read otherwise.

Sounds like a request for a package.

I'm fine with purging everything every X days, but thought I'll just leave my suggestion here if anyone wants to implement a package for caching in browser extensions :)

@hkdobrev I considered a TTL too, but I just don't see it being worth the complication. The optimal solution would be to use an LRU store, so we expunge the least recently used items for optimal efficiency. But I still think the naive approach is ok.

@sindresorhus we'd have to use another solution though because browser.storage doesn't support Map

I vote for clearing every 15 days.

if storage.get('last-clear') > today - 15 days
    storage.clear()
    storage.set('last-clear', today)

Done. Less code, probably more efficient. We just have to avoid dropping the unreadNotifications

Make it every 30 days and I'm 👍

Was this page helpful?
0 / 5 - 0 ratings