Expired Storage
Micro JS lib that provide local & session storage with expiration time.
How it works
The lib provide a single object that wraps localStorage (or any other class compatible with its API) and set items with additional key to store their expiration time.
When fetching data from storage, the lib will check for expiration and remove expired items.
Here's a quick example of using it:
expiredStorage = new ExpiredStorage();
expiredStorage.setItem("some_key", "some_val", 60);
Install
npm:
npm install expired-storage
bower:
bower install expired-storage
or simply take the files from dist/
(either minified or full) and include in any HTML page.
Usage
Lets see how we use Expired Storage..
Creating Expired Storage
Create ExpiredStorage using the localStorage (default):
expiredStorage = new ExpiredStorage();
Or create with different type of storage ('storage' must implement the following: setItem, getItem, removeItem, clear):
expiredStorage = new ExpiredStorage(storage);
Basics
Set item with expiration time (in seconds):
expiredStorage.setItem("test", "foobar", 60);
Or you can set items without expiration time (in which case they will behave just like a normal storage item):
expiredStorage.setItem("no_expire", "this will live forever");
Fetch item (if expired, will remove it and return null):
var item = expiredStorage.getItem("test");
Extended API
ExpiredStorage comes with some extra functions to get data:
var timeLeft = expiredStorage.getTimeLeft("test");
var isExpired = expiredStorage.isExpired("test");
var keys = expiredStorage.keys(includeExpired);
var data = expiredStorage.peek("test");
Plus, you can update item expiration time without changing its content:
expiredStorage.updateExpiration("test", 100);
Clear Items
Normally expired items will be cleared from storage when you try to fetch them.
However, if you want initiate cleanup to clear all expired keys, you can use the following:
var expiredKeys = expiredStorage.clearExpired();
Or you can just clear everything, including keys that were not created with Expired Storage:
expiredStorage.clear();
JSON get / set
ExpiredStorage comes with two nice-to-have functions to quickly set and get JSON values:
expiredStorage.setJson(key, someObject, 60);
var someObject = expiredStorage.getJson(key);
Custom Storage
Expired Storage support any storage class that implements the following functions: getItem(), setItem(), removeItem(), clear().
The following example shows how to create a custom storage class that uses dictionary internally, and use it with Expired Storage:
var testStorage = {
_storage: {},
getItem: function(key) {
return this._storage[key];
},
setItem: function(key, val) {
this._storage[key] = val;
},
removeItem: function(key) {
delete this._storage[key];
},
clear: function() {
this._storage = {};
},
keys: function() {
var ret = [];
for (var key in this._storage) {
if (this._storage.hasOwnProperty(key)) {
ret.push(key);
}
}
return ret;
}
};
expiredStorage = new ExpiredStorage(testStorage);
License
Expired Storage uses the permissive MIT License.
Cheers.