Small JS lib for caching:
- Use array as key for storing cache instead of string ( {blogId:1,type:'view'} )
- Clear cache using context (clear blog posts of an author)
- Support localStorage, array, cookies (WIP, only localStorage and array are available at the moment)
See the project homepage.
Using Bower:
bower install cache-js
Or grab the file source (minified).
Save cache:
cacheJS.set(key
,value
,ttl
,context
)
cacheJS.set({blogId:1,type:'view},'<h1>Blog 1</h1>');
cacheJS.set({blogId:2,type:'view},'<h1>Blog 2</h1>', null, {author:'hoangnd'});
cacheJS.set({blogId:3,type:'view},'<h1>Blog 3</h1>', 3600, {author:'hoangnd',categoryId:2});
Retrieve cache:
cacheJS.get(key
)
cacheJS.get({blogId: 1,type: 'view'});
Flush cache
cacheJS.removeByKey(key
)
cacheJS.removeByContext(context
)
cacheJS.removeByKey({blogId: 1,type: 'view'});
cacheJS.removeByKey({blogId: 2,type: 'view'});
cacheJS.removeByContext({author:'hoangnd'});
Switch provider
cacheJS.use(providerName
)
cacheJS.use('array');
cacheJS.use('array').set({blogId:1},'<h1>Blog 1</h1>')};
Event listener
cacheJS.on(event
,callback
)
cacheJS.unsubscribe(event
,callback
)
var listener = function(objectAdded){
// do something
};
cacheJS.on('cacheAdded', listener);
cacheJS.on('cacheRemoved', function(){
// do something
});
cacheJS.unsubscribe('cacheAdded', listener);
Supported events:
- cacheAdded
- cacheRemoved
For advanced usage, see the documentation.
Start with docs/INDEX.md
.
MIT. See LICENSE.txt
in this directory.
Inspired by https://github.com/sonata-project/cache