Socket
Socket
Sign inDemoInstall

expiring-cache

Package Overview
Dependencies
2
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    expiring-cache

An expiring in-memory cache!


Version published
Weekly downloads
11
decreased by-67.65%
Maintainers
1
Install size
137 kB
Created
Weekly downloads
 

Readme

Source

#expiring-cache

An expiring cache!

This is an in-memory cache which stores key/value pairs in a collection and invalidates them after a certain period of time. Useful for things that aren't really important enough to stick into a db, want to be kept around, and should not be kept around for the entire lifetime of the app.

The expiring-cache defines a particular fetch method which all keys must use, though the key is passed in such that you could make an API call based on the key, etc.

In addition, it requires all items in the cache to have the same maximum lifespan (though of course they expire at different times if they were inserted at different times). If each item in the cache needs its own expiry time, check out expiring-per-item-cache

Usage (typescript):

import ExpiringCache from 'expiring-cache';

const pause = (ms) => new Promise(resolve => setTimeout(resolve, ms));

async function getSomeNumberValueAsync(key: string) {
    let sum = 0;
    for (let i = 0; i < key.length; ++i) {
        sum += key.charCodeAt(i);
        await pause(Math.random() * 50);
    }
    
    return sum;
}

const expiringCache: ExpiringCache<string, number> = new ExpiringCache<string, number>({
    fetch: (key: string) => getSomeNumberValueAsync(key),
    expireTime: 5*60*1000, // 5 minutes
    clearTime: 2*60*1000 // 2 minutes 
});

async function main() {
    // As expected, if fetch's promise fails then this will also throw
    // Otherwise value is now a number.
    const value = await expiringCache.get('duck');
}

Upgrading from v1 to v2

  • Stop using Collection methods. They are not supported. If you reallllly need them, use the private _data property
  • (Optional, but recommended) switch to get and has from getValue and hasValid. They are aliases and identical.
  • If you use the expireTime property, it is now a Duration. Use expireTime.inMilliseconds instead.

Keywords

FAQs

Last updated on 16 Jan 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc