Socket
Socket
Sign inDemoInstall

lru-cache

Package Overview
Dependencies
0
Maintainers
2
Versions
132
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    lru-cache

A cache object that deletes the least-recently-used items.


Version published
Weekly downloads
204M
increased by2.28%
Maintainers
2
Install size
24.1 kB
Created
Weekly downloads
 

Package description

What is lru-cache?

The lru-cache package is a JavaScript library that provides a cache object that deletes the least-recently-used items. It is useful for storing a limited amount of data in a way that allows for fast retrieval of entries based on keys.

What are lru-cache's main functionalities?

Creating a cache instance

This code sample demonstrates how to create a new LRU cache instance with a maximum of 500 items and a maximum age of one hour for each item.

{"const LRU = require('lru-cache');
const options = { max: 500, maxAge: 1000 * 60 * 60 };
const cache = new LRU(options);"}

Setting and getting cache items

This code sample shows how to set a value in the cache with a key and then retrieve that value using the same key.

{"const LRU = require('lru-cache');
const cache = new LRU();
cache.set('key', 'value');
const value = cache.get('key');"}

Checking if a key is in the cache

This code sample illustrates how to check if a key is present in the cache without updating the recent-ness or deleting it.

{"const LRU = require('lru-cache');
const cache = new LRU();
cache.set('key', 'value');
const hasKey = cache.has('key');"}

Deleting a key from the cache

This code sample shows how to delete a specific key from the cache.

{"const LRU = require('lru-cache');
const cache = new LRU();
cache.set('key', 'value');
cache.del('key');"}

Resetting the cache

This code sample demonstrates how to completely clear the cache.

{"const LRU = require('lru-cache');
const cache = new LRU();
cache.set('key', 'value');
cache.reset();"}

Other packages similar to lru-cache

Readme

Source

lru cache

A cache object that deletes the least-recently-used items.

Usage:

var LRU = require("lru-cache")
  , options = { max: 500
              , length: function (n) { return n * 2 }
              , dispose: function (key, n) { n.close() }
              , maxAge: 1000 * 60 * 60 }
  , cache = LRU(options)
  , otherCache = LRU(50) // sets just the max size

cache.set("key", "value")
cache.get("key") // "value"

cache.reset()    // empty the cache

If you put more stuff in it, then items will fall out.

If you try to put an oversized thing in it, then it'll fall out right away.

Options

  • max The maximum size of the cache, checked by applying the length function to all values in the cache. Not setting this is kind of silly, since that's the whole purpose of this lib, but it defaults to Infinity.
  • maxAge Maximum age in ms. Items are not pro-actively pruned out as they age, but if you try to get an item that is too old, it'll drop it and return undefined instead of giving it to you.
  • length Function that is used to calculate the length of stored items. If you're storing strings or buffers, then you probably want to do something like function(n){return n.length}. The default is function(n){return 1}, which is fine if you want to store n like-sized things.
  • dispose Function that is called on items when they are dropped from the cache. This can be handy if you want to close file descriptors or do other cleanup tasks when items are no longer accessible. Called with key, value. It's called before actually removing the item from the internal cache, so if you want to immediately put it back in, you'll have to do that in a nextTick or setTimeout callback or it won't do anything.
  • stale By default, if you set a maxAge, it'll only actually pull stale items out of the cache when you get(key). (That is, it's not pre-emptively doing a setTimeout or anything.) If you set stale:true, it'll return the stale value before deleting it. If you don't set this, then it'll return undefined when you try to get a stale entry, as if it had already been deleted.

API

  • set(key, value, maxAge)

  • get(key) => value

    Both of these will update the "recently used"-ness of the key. They do what you think. max is optional and overrides the cache max option if provided.

  • peek(key)

    Returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

    (If you find yourself using this a lot, you might be using the wrong sort of data structure, but there are some use cases where it's handy.)

  • del(key)

    Deletes a key out of the cache.

  • reset()

    Clear the cache entirely, throwing away all values.

  • has(key)

    Check if a key is in the cache, without updating the recent-ness or deleting it for being stale.

  • forEach(function(value,key,cache), [thisp])

    Just like Array.prototype.forEach. Iterates over all the keys in the cache, in order of recent-ness. (Ie, more recently used items are iterated over first.)

  • keys()

    Return an array of the keys in the cache.

  • values()

    Return an array of the values in the cache.

  • length()

    Return total length of objects in cache taking into account length options function.

  • itemCount()

    Return total quantity of objects currently in cache. Note, that stale (see options) items are returned as part of this item count.

Keywords

FAQs

Last updated on 30 Jun 2015

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