Giter Club home page Giter Club logo

ls-cache's Introduction

ls-cache

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: [https://github.com/pamelafox/lscache]

Methods

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


lscache.set

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

Arguments

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

lscache.get

Retrieves specified value from localStorage, if not expired.

Arguments

  1. key (string)

Returns

Object : The stored value.


lscache.keys

Gets list of all keys in bucket

Returns

Array : Key names


lscache.remove

Removes a value from localStorage.

Arguments

  1. key (string)

lscache.flush

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


lscache.flushRecursive

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


lscache.createBucket

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

Arguments

  1. bucket (string)

Usage

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

alert(lscache.get('greeting'));

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

lscache.remove('greeting');

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

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:

alert(lscache.get('data').name);

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: http://www.quirksmode.org/dom/html5.html

ls-cache's People

Contributors

ansman avatar fiveminuteargument avatar grassick avatar hongymagic avatar jcbanks avatar joesondow avatar krunkosaurus avatar mattpowell avatar mckamey avatar pamelafox avatar svperfecta avatar tebriel avatar

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.