OhMyCache is a javascript library that uses LocalStorage and SessionStorage for cache datas on your application without dependencies
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+
npm install OhMyCache
bower install OhMyCache
Download this repository and copy files on your project (not recommended)
import {Local, Session} from 'OhMyCache'
// quick use
Local.set('key', 'val')
Session.set('key', 'val')
var OhMyCache = require('OhMyCache')
// quick use
OhMyCache.Local.set('key', 'val')
OhMyCache.Session.set('key', 'val')
<html>
<head>
<!-- npm -->
<script src="node_modules/ohmycache/dist/bundle.js"></script>
<!-- bower -->
<script src="bower_components//ohmycache/dist/bundle.js"></script>
<!-- other method -->
<script src="lib-path/ohmycache/dist/bundle.js"></script>
</head>
</html>
Store data in local storage
OhMyCache.Local.set('key', 'local')
Store data in session storage
OhMyCache.Session.set('key', 'session')
Get data in local storage
OhMyCache.Local.get('key') // return 'local'
Get data in session storage
OhMyCache.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.
OhMyCache.Local
and OhMyCache.Session
share the same API, 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, options)
- {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 item expires in x seconds // default = false
- {mixed} readonly : (true) prohibit modification // default = false
Boolean : (true) success || (false) fail
// String
OhMyCache.Local.set('key', 'value')
// Array
OhMyCache.Local.set('key', [1,2,3])
// Object
OhMyCache.Local.set('key', {fistname: 'James', lastname: 'Bond'})
// With expiration
OhMyCache.Local.set('key', 'value', {expire: 3600}) // data expires in 1 hour
// ReadOnly
OhMyCache.Local.set('key', 'value', {readonly: true}) // return true
OhMyCache.Local.set('key', 'change') // return false
OhMyCache.Local.get('key') // return 'value'
Get the value, remove if expired
get(key)
- {string} key
Mixed : the value OR null if the data has expired or not exist
OhMyCache.Local.set('key', 'value')
OhMyCache.Local.get('key') // 'value'
OhMyCache.Session.get('key') // null
OhMyCache.Local.set('key', 'value', {expire: 1})
// sleep 2 secondes or more
OhMyCache.Local.get('key') // null
Get all items, remove all expired items
getAll()
Object : all items {key: value, key2: value2...}
OhMyCache.Local.set('k1', 'v1')
OhMyCache.Local.set('k2', 'v2', {readonly: true})
OhMyCache.Local.set('k3', 'v3', {expire: 1})
// sleep 2 secondes or more
OhMyCache.Local.getAll() // {k1: 'v1', k2: 'v2'}
Return all item's keys, doen't remove expired items
keys()
Array : all item's keys
OhMyCache.Local.set('k1', 'v1')
OhMyCache.Local.set('k2', 'v2', {readonly: true})
OhMyCache.Local.set('k3', 'v3', {expire: 1})
// sleep 2 secondes or more
OhMyCache.Local.keys() // ['k1', 'k2', 'k3']
Remove item if don't readonly
remove(key)
- {string} key
Boolean : (true) success || (false) fail
OhMyCache.Local.set('key', 'value')
OhMyCache.Local.get('key') // 'value'
OhMyCache.Local.remove('key') // true
OhMyCache.Local.get('key') // null
// ReadOnly
OhMyCache.Local.set('key', 'value', {readonly: true})
OhMyCache.Local.get('key') // 'value'
OhMyCache.Local.remove('key') // false
OhMyCache.Local.get('key') // 'value'
Remove all items
clear(onlyExpired)
- {boolean} onlyExpired : if true remove only expired items else remove all items, default false
Boolean : success
OhMyCache.Local.set('key', 'value')
OhMyCache.Local.set('key2', 'value', {readonly: true})
OhMyCache.Local.clear() // true
OhMyCache.Local.get('key') // null
OhMyCache.Local.get('key2') // null
Indicates whether the value has expired
isExpired(key)
- {string} key
Boolean : (true) is expired || (false) is not expired
// No expiration
OhMyCache.Local.set('key', 'value')
OhMyCache.Local.isExpired('key') // false
// No expired
OhMyCache.Local.set('key', 'value', {expire: 999999})
OhMyCache.Local.isExpired('key') // false
// Expired
OhMyCache.Local.set('key', 'value', {expire: 1})
// sleep 1 second or more
OhMyCache.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
OhMyCache.Local.set('key', 'value')
OhMyCache.Local.isReadonly('key') // false
// is readonly
OhMyCache.Local.set('key', 'value', {readonly: true})
OhMyCache.Local.isReadonly('key') // true