loco-cache
v1.0.2
Published
cache data using browser session storage
Downloads
27
Readme
loco-cache - front-end caching using browser local storage
Usage
- React -
npm install loco-cache
- CDN - Vist loco-cache CDN
Purpose
Sometimes a website's performance can be greatly improved if commonly used data is cached on the front end. Most browsers provide APIs to allow storing data
on session or domain level. loco-cache
provides a robust abstraction on top of the storage APIs to make caching simple. It provides the following features:
- Caching with expiration - each key can be automatically expired
- Sweep - automatically removes old entries to free up storage space
- Collision prevention - keys are prefixed to avoid name collision with other packages
- Optional compression - supports optional compression to save storage space
Documents
Examples
loco-cache
API is intuitive yet customizable.
Example 1: basic example using .get()
and .set()
var cache = require('loco-cache')()
// assume we have to obtain a list of countries via AJAX to populate a dropdown
// before the AJAX call, check the cache first
var countries = cache.get('countries')
if (countries == null) {
// not in cache, maek the AJAX call instead, then store in cache
countries = await ajax('/api/countries').data
cache.set('countries', countries)
}
Example 2: customize with options
var cache = require('loco-cache')({
prefix: "acme",
expiresIn: 1200, // each key expires in 1200 seconds
storage: 'local' // use local storage
})
// only customization is different. everything below stays the same
var countries = cache.get('countries')
if (countries == null) {
// not in cache, maek the AJAX call instead, then store in cache
countries = await ajax('/api/countries').data
cache.set('countries', countries)
}
Cheatsheet
Simple cheatsheet to get you going. For detailed documentation please visit Docs
var cache = require('loco-cache')(options)
options
- options to customize the cache. If null, default values are usedprefix
- prefix used for each key to avoid name collision. default: locoexpiresIn
- how many seconds to keep a key in cache. default: 600storage
- session or local storage. default: session
cache.get(key)
key
- needs to be a string, cannot be null- returns the stored object
cache.set(key, obj)
key
- needs to be a string, cannot be nullobj
- a simple object to store