Skip to content

Caches don't have limits/expirations #624

Closed

Description

@fregante

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?

Activity

  1. added
    meta
    on Jul 18, 2017
  2. sindresorhus commented on Jul 18, 2017

    @sindresorhus
    Member

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

  3. hkdobrev commented on Jul 18, 2017

    @hkdobrev
    Contributor

    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 :)

  4. sindresorhus commented on Jul 18, 2017

    @sindresorhus
    Member

    @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.

  5. fregante commented on Jul 28, 2017

    @fregante
    MemberAuthor

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

  6. fregante commented on Dec 8, 2017

    @fregante
    MemberAuthor

    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

  7. sindresorhus commented on Dec 8, 2017

    @sindresorhus
    Member

    Make it every 30 days and I'm 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedbugmeta

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions