Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including ls-cache with all npm packages installed. Try it out:

var lsCache = require("ls-cache")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

ls-cache v0.2.3

Browserify-friendly LocalStorage cache with object support and expiring entries


This is a library that emulates memcache functions using HTML5 localStorage, so that you can cache data on the client and associate an expiration time with each piece of data. If the localStorage limit (~5MB) is exceeded, it tries to create space by removing the items that are closest to expiring anyway. If localStorage is not available at all in the browser, the library degrades by simply not caching and all cache requests return null.

Entries which have no expiry are never removed automatically from the cache.

Entries in the cache are optionally stored in a hierarchy of buckets, each of which is independant of the others.

Based on: []


The library exposes methods: set(), get(), remove(), flush(), flushRecursive(), createBucket() and keys().


Stores the value in localStorage. Expires after specified number of minutes.


  1. key (string)
  2. value (Object)
  3. time (number: optional)


Retrieves specified value from localStorage, if not expired.


  1. key (string)


Object : The stored value.


Gets list of all keys in bucket


Array : Key names


Removes a value from localStorage.


  1. key (string)


Removes all ls-cache items in current bucket from localStorage without affecting other data.


Removes all ls-cache items in current bucket from localStorage and all sub-buckets


Creates a sub-bucket which is completely independent. However, its expirable items may be cleared to make room for more keys.


  1. bucket (string)


The interface should be familiar to those of you who have used memcache, and should be easy to understand for those of you who haven't.

For example, you can store a string for 2 minutes using lscache.set():

lscache.set('greeting', 'Hello World!', 2);

You can then retrieve that string with lscache.get():


You can remove that string from the cache entirely with lscache.remove():


You can remove all items from the cache entirely with lscache.flush():


The library also takes care of serializing objects, so you can store more complex data:

lscache.set('data', {'name': 'Pamela', 'age': 26}, 2);

And then when you retrieve it, you will get it back as an object:


If you have multiple instances of ls-cache running on the same domain, you can partition data in a certain bucket via:

bucket = lscache.createBucket("something");
bucket.set('response', '...', 2);
lscache.set('path', '...', 2);
lscache.flush(); //only removes 'path' which was set in the root bucket, not the sub-bucket

Buckets are nestable:

bucket = lscache.createBucket("firstlevel");
bucket2 = bucket.createBucket("secondlevel");

Browser Support

The lscache library should work in all browsers where localStorage is supported. A list of those is here:

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free