Local Storage , Supported in HTML5, to store data locally in browser by key value pair, up to 5 mb
here are little example that can show you , how to use this library
About Library:
* you will need jquery for this
You can use this library to store data in local storage of browser, if it is supported,
It extend the local storage function of browser , and provide a way to you to interact with storage based on your need,
it can be used as BROWSER CACHE FOR YOUR SERVER SIDE API DATA, that you hit frequently.
This API can store data for a period of time, that is expiry time, and can delete it after that time,
a function does this automatically. from that you can check if you need to hit api , or not.
You can append data to an existing storage.
How To test what is going on.
Steps:-
* open this page in chrome.
* open developer console
* Select Resource Tab
* expand local storage from left
* click on page name, it will storage in right side.
List
- isStorageSupport: check if browser support local storage
- storeObj: Storage Object
- handleStorage: automatically handle all things after setup
- setExpireTime: To Set Expire Time
- getExpireTime: To Set Expire Time
- setStorage: Store data in Local Storage
- getStorage: Get Data From Local Storage
- isKeyExist: check if key exist in local storage
- getKeyTimeStamp: Get Key Stamp Of Item , when it was saved
- isExpired: check with expiry time
- addTimeStampInJson: if you want to explicitly add time stamp in json
- addItemValueByKey: append data by key value , already stored in local Storage
- getItemValueByKey: Retrieve item value by key
- clearStorageByKey : Clear Storage By Key
- clearStorage: Clear All
- storageSize: get size of storage
- logDetail: Log everything on console
- setDebug: if true Log everything on console, by default false;
let say our data in json is data = {"a":1,"b":{"b1":"1","b2":2},"c":3} and we want to operate it with our function let say storage key is "EXAMPLE"
Let's have a storage obj
for debug set
ls.setDebug(true); // it will log in console.
By default expiry time is 1800 second;
Data Should be In JSON Format;
To RUN Example , simply open debugger console panel in window and copy paste that code , and run it.
isStorageSupportif(ls.isStorageSupport()){ alert(true); }else{ alert(false); }storeObj
console.log(ls.storeObj);will return storage object. handleStorage this function is most desired function to handle local storage.
ls.setDebug(true); var key = "EXAMPLE"; var data = {"a":1,"b":{"b1":"1","b2":2},"c":3}; var addTimeStamp = true; var option = '';//{doStore: true, itemValuePair:false, other:false}; // itemValuePair is another object in json format to store with that key. ls.handleStorage(key, data, addTimeStamp, option); Note:- value passed in other will not be stored in local storage.will save the data in local storage. setExpireTime this function is used to set expiry time globally
ls.setExpireTime(1200); // in seconds, by default 1800 secondsgetExpireTime this function is used to get expiry time
console.log(ls.getExpireTime()); // in seconds, by default 1800 secondssetStorage this function is used to set storage, if you do not want to use handleStorage() function.
ls.setDebug(true); var key = "EXAMPLE"; var data = {"a":1,"b":{"b1":"1","b2":2},"c":3}; var addTimeStamp = true; var itemValuePair= false; // itemValuePair is another object in json format to store with that key. ls.setStorage(key, data, addTimeStamp,itemValuePair);getStorage this function is used to get stored data by key
ls.setDebug(true); var key = "EXAMPLE"; console.log(ls.getStorage(key));isKeyExist this function is used to check if stored key exist in local storage
ls.setDebug(true); var key = "EXAMPLE"; console.log(ls.isKeyExist(key));getKeyTimeStamp this function is used to get time stamp value at which data was saved
ls.setDebug(true); var key = "EXAMPLE"; console.log(ls.getKeyTimeStamp(key));isExpired this function is used to check if key data is expired with respect to expiry time
ls.setDebug(true); var key = "EXAMPLE"; var eT = 1200; //it is optional , if blank then check global expiry time var clearKey =false; // by default true, if expired then clear the data from storage. console.log(ls.isExpired(key, eT, clearKey));addTimeStampInJson this function is used to manually add timeStamp in json data
ls.setDebug(true); var data = '{"a":1}'; console.log(ls.addTimeStampInJson(data));addItemValueByKey this function is used to manually add item value pair in stored data
ls.setDebug(true); var storeKey = 'EXAMPLE'; var item ="b"; var value = 2; console.log(ls.addItemValueByKey(storeKey,item, value));getItemValueByKey this function is used to get item value data
ls.setDebug(true); var storeKey = 'EXAMPLE'; var item ="b"; console.log(ls.getItemValueByKey(storeKey,item) );clearStorageByKey this function is used to clear data from local storage through key
ls.setDebug(true); var storeKey = 'EXAMPLE'; console.log(ls.clearStorageByKey(storeKey) );clearStorage this function is used to clear all storage
console.log(ls.clearStorage() );storageSize this function is used to calculate storage size , but it is slow , not recommended to use
console.log(ls.storageSize() );logDetail this function is used to log Detail on console if debug is true, but by default debug is false;
console.log(ls.logDetail("any data") );setDebug this function is used to set debug true or false, that will handle logDetail function,
console.log(ls.setDebug(true) );