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 = ;expiredStorage; // <-- will expire after 60 seconds
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 = ;
Or create with different type of storage ('storage' must implement the following: setItem, getItem, removeItem, clear):
// create from custom storage class// note: in addition to the basic api functions the storage should also be iterable, meaning you can do `for (key in storage)`.// if that's not possible, please provide a 'keys()' function that will return a list of key names in your storage class.expiredStorage = storage;
Basics
Set item with expiration time (in seconds):
// this item will live for 60 seconds, eg one minute.expiredStorage;
Or you can set items without expiration time (in which case they will behave just like a normal storage item):
expiredStorage;
Fetch item (if expired, will remove it and return null):
var item = expiredStorage;
Extended API
ExpiredStorage comes with some extra functions to get data:
// get object time to live (in seconds). this will not remove the item, even if expired:var timeLeft = expiredStorage; // check if item is expired:var isExpired = expiredStorage; // get list of keys (if includeExpired is true, will include expired keys that were not yet deleted)var keys = expiredStorage; // will return a dictionary with value, time left, and if expired, without removing the item (even if expired).var data = expiredStorage;
Plus, you can update item expiration time without changing its content:
// update "test" expiration time to be 100 seconds from current time.expiredStorage;
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:
// remove all expired items and return a list with their keysvar expiredKeys = expiredStorage;
Or you can just clear everything, including keys that were not created with Expired Storage:
expiredStorageclear;
JSON get / set
ExpiredStorage comes with two nice-to-have functions to quickly set and get JSON values:
// write 'someObject' as a JSON stringified text with expiration of 60 secondsexpiredStorage; // get value and try to JSON parse itvar someObject = expiredStorage;
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:
// create a custom storage class for testingvar testStorage = // internal storage _storage: {} // basic API { return this_storagekey; } { this_storagekey = val; } { delete this_storagekey; } { this_storage = {}; } // you can implement keys() function that will be used to retrieve storage keys. { var ret = ; for var key in this_storage if this_storage ret; return ret; }; // use our custom storage with Expired StorageexpiredStorage = testStorage;
License
Expired Storage uses the permissive MIT License.
Cheers.