Cachejs is a javascript library that uses LocalStorage and SessionStorage for cache datas on your application.
Web browser with LocalStorage and SessionStorage :
- Chrome 5+
- Firefox 3.5+
- IE 8+
- Safari 4+
- Opera 10.50+
- Safari Mobile iOS 3.2
- Android 2.1+
Download this repository and copy files on your project (not recommended)
Load the lib :
<html>
<head>
<script src="/lib-path/cachejs/dist/cache.min.js"></script>
</head>
</html>
Store data in local storage
Cachejs.Local.set('key', 'local');
Store data in session storage
Cachejs.Session.set('key', 'session');
Get data in local storage
Cachejs.Session.get('key'); // return 'local'
Get data in session storage
Cachejs.Session.get('key'); // return 'session'
The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the browsing session ends—that is, when the browser is closed.
The sessionStorage property allows you to access a session Storage object. sessionStorage is similar to Window.localStorage, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.
Cachejs.Local
and Cachejs.Session
have access to the same functions.
Add the key to the storage, or update that key's value if it already exists
set(key, value, expiration = null, readOnly = false)
- {string} key : name of the key you want to create/updat
- {mixed} value : value you want to give the key you are creating/updating (string, int, array, object...)
- {mixed} expire : (int) the data expires in x seconds || (falsy) no expiration
- {mixed} readOnly : (true) prohibit modification || (falsy)
Boolean : (true) success || (false) fail
- String, int...
// String
Cachejs.Local.set('key', 'value');
// Array
Cachejs.Local.set('key', [1,2,3]);
// Object
Cachejs.Local.set('key', {fistname: 'James', lastname: 'Bond'});
// With expiration
Cachejs.Local.set('key', 'value', 3600); // data expires in 1 hour
// ReadOnly
Cachejs.Local.set('key', 'value', null, true); // return true
Cachejs.Local.set('key', 'change'); // return false
Cachejs.Local.get('key'); // return 'value'
Get only value, remove if expired
get(key)
- {string} key
Mixed : the value OR null if the data has expired or not exist
Cachejs.Local.set('key', 'value');
Cachejs.Local.get('key'); // 'value'
Cachejs.Session.get('key'); // null
Cachejs.Local.set('key', 'value', 1);
// sleep 2 secondes or more
Cachejs.Local.get('key'); // null
Remove data if don't readonly
remove(key)
- {string} key
Boolean : (true) success || (false) fail
Cachejs.Local.set('key', 'value');
Cachejs.Local.get('key'); // 'value'
Cachejs.Local.remove('key'); // true
Cachejs.Local.get('key'); // null
// ReadOnly
Cachejs.Local.set('key', 'value', null, true);
Cachejs.Local.get('key'); // 'value'
Cachejs.Local.remove('key'); // false
Cachejs.Local.get('key'); // 'value'
Remove all datas
clear()
true
Cachejs.Local.set('key', 'value');
Cachejs.Local.set('key2', 'value', null, true);
Cachejs.Local.clear(); // true
Cachejs.Local.get('key'); // null
Cachejs.Local.get('key2'); // null
Indicates whether the value has expired
isExpired(key)
- {string} key
Boolean : (true) is expired || (false) is not expired
// No expiration
Cachejs.Local.set('key', 'value');
Cachejs.Local.isExpired('key'); // false
// No expired
Cachejs.Local.set('key', 'value', 999999);
Cachejs.Local.isExpired('key'); // false
// Expired
Cachejs.Local.set('key', 'value', 1);
// sleep 1 second or more
Cachejs.Local.isExpired('key'); // true
Indicates whether the value is readonly
isReadonly(key)
- {string} key
Boolean : (true) is read only || (false) is not read only
// No readonly
Cachejs.Local.set('key', 'value');
Cachejs.Local.isReadonly('key'); // false
// is readonly
Cachejs.Local.set('key', 'value', null, true);
Cachejs.Local.isReadonly('key'); // true