GithubHelp home page GithubHelp logo

tmp-cache's Introduction

tmp-cache CI codecov

A least-recently-used cache in 35 lines of code~!

LRU caches operate on a first-in-first-out queue. This means that the first item is the oldest and will therefore be deleted once the max limit has been reached.

When a maxAge value is set, items are given an expiration date. This allows existing items to become stale over time which, depending on your stale config, is equivalent to the item not existing at all!

In order to counteract this idle decay, all set() and get() operations on an item "refresh" its expiration date. By doing so, a new expires value is issued & the item is moved to the end of the list โ€” aka, it's the newest kid on the block!

Install

$ npm install --save tmp-cache

Usage

const Cache = require('tmp-cache');

let cache = new Cache(3); // sets "max" size

cache.set('a', 1); //~> ['a']
cache.set('b', 2); //~> ['a', 'b']
cache.set('c', 3); //~> ['a', 'b', 'c']
cache.get('a');    //~> ['b', 'c', 'a']
cache.set('d', 4); //~> ['c', 'a', 'd']
cache.peek('a');   //~> ['c', 'a', 'd']
cache.delete('d'); //~> ['c', 'a']
cache.has('d');    //=> false
cache.set('e', 5); //~> ['c', 'a', 'e']
cache.size;        //=> 3
cache.clear();     //~> []

cache = new Cache({ maxAge:10 });

cache.set(123, 'hello'); //~> valid for 10ms
cache.get(123); //=> 'hello'  --  resets 10ms counter
setTimeout(_ => cache.get(123), 25); //=> undefined

cache = new Cache({ maxAge:0, stale:true });

cache.set('foo', [123]); //~> already stale, 0ms lifespan
cache.get('foo'); //=> [123]  --  because options.stale
cache.get('foo'); //=> undefined  --  previous op flagged removal

API

Aside from the items & changes mentioned below, tmp-cache extends the Map class, so all properties and methods are inherited.

Cache(options)

Returns: Cache extends Map

options.max

Type: Number
Default: Infinity

The maximum number of items the cache will hold. Adding more entries will force the oldest, least-recently-used item to be purged.

Failure to include any max restriction could potentially allow infinite unique entries! They will only be purged based on their expires value (if set).

Note: If options is an integer, then it is used as the options.max value.

options.maxAge

Type: Number
Default: -1

The maximum age (in ms) an item is considered valid; aka, its lifespan.

Items are not pro-actively pruned out as they age, but if you try to access an item that has expired, it will be purged and, by default, result in an undefined response.

options.stale

Type: Boolean
Default: false

Allow an expired/stale item's value to be returned before deleting it.

Cache.set(key, value, maxAge?)

Persists the item and its value into the Cache. If a maxAge value exists (via custom or cache-level options), an expiration date will also be stored.

When setting or updating an item that already exists, the original is removed. This allows the new item to be unique & the most recently used!

key

Type: String

The item's unique identifier.

value

Type: Mixed

The item's value to cache.

maxAge

Type: Number
Default: options.maxAge

Optionally override the options.maxAge for this (single) operation.

Cache.get(key, mutate?)

Retrieve an item's value by its key name. By default, this operation will refresh/update the item's expiration date.

May also return undefined if the item does not exist, or if it has expired & stale is not set.

key

Type: String

The item's unique identifier.

mutate

Type: Boolean
Default: true

Refresh the item's expiration date, marking it as more recently used.

Cache.peek(key)

Return an item's value without updating its position or refreshing its expiration date.

May also return undefined if the item does not exist, or if it has expired & stale is not set.

key

Type: String

The item's unique identifier.

License

MIT ยฉ Luke Edwards

tmp-cache's People

Contributors

chocolateboy avatar lukeed avatar mohsen1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tmp-cache's Issues

items are not pro-actively pruned out as they age

items are not pro-actively pruned out as they age

I'm thinking about memory leaks in this case if LRU is used on a server and cache key is something time based (e.g "today"). In this scenario old keys (when today has moved to the next day) will never be accessed (neither set or get), so never pruned. How would you solve it?

I see one simple/stupid solution to do like setInterval that does cache.clear() once in a while. But that would clear all items, not only stale.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.