Local storage manager. Provides handy methods to interact with local storage in a cross-browser compatible way.
By default, the cache manager uses store.js to manipulate items in the
window.localStorage
object.
The value of the key which is passed as a
parameter to get/set/add
methods is prefixed with <env>:<appId>:
string
to avoid clashes with other environments and applications running off the
same domain name and port. You can set environment and application ID in
the configuration file.
Members
-
<inner> store
-
Storage provider.
Default: store.js. See https://github.com/marcuswestin/store.js.
Methods
-
<inner> Cache(cfg)
-
Parameters:
Name Type Description cfg
Object The configuration object.
Properties
Name Type Description appId
string Application identifier.
env
string Application environment. Possible values are 'dev', 'test', and 'prod'.
uniqueKey
string Key used to prevent local storage values from leaking to other application instances.
-
<inner> clean()
-
Removes non-critical values to free up space. It should be called whenever local storage quota is exceeded. You can listen to the
cache:clean
event (passes callback as argument) in order to register keys to preserve after clean. Keys that are not vital should not be preserved during a cleanup.Example:
({ initialize: function(options) { Events.on('cache:clean', function(callback) { callback([ 'my_important_cache_key', 'my_other_important_key', ]) }); }, });
Fires:
- cache:clean
-
<inner> cut(key)
-
Deletes an item from local storage.
Parameters:
Name Type Description key
string Item key.
-
<inner> cutAll( [all])
-
Deletes all items from local storage.
By default, this method deletes all items for the current app and environment. Pass
true
to this method to remove all items.Parameters:
Name Type Argument Default Description all
boolean <optional>
false Flag indicating if all items must be deleted from local storage.
-
<inner> get(key)
-
Gets an item from local storage.
Parameters:
Name Type Description key
string Item key.
Returns:
Item with the given key.
- Type
- number | boolean | string | Array | Object
-
<inner> has(key)
-
Checks if the given item exists in local storage.
Parameters:
Name Type Description key
string Item key.
Returns:
true
ifkey
exists in local storage;false
otherwise.- Type
- boolean
-
<inner> init()
-
Initializes the local storage manager.
-
<inner> set(key, value)
-
Puts an item into local storage.
Parameters:
Name Type Description key
string Item key.
value
number | boolean | string | Array | Object Item to put.