[Open Source] encapsulates the localstorage and html5localstorage of html5.

Source: Internet
Author: User

[Open Source] encapsulates the localstorage and html5localstorage of html5.

Project name: web-storage-cache

Project address: https://github.com/WQTeam/web-storage-cache

API Description: https://github.com/WQTeam/web-storage-cache/blob/master/README_zh_CN.md

I am working on mobile development at the front end and want to use localstorage for data caching. It is found that localstorage only stores simple string key-value pairs. However, it is often necessary to workJSON. parseAndJSON. stringifyIn many scenarios, you need to add a timeout value. I have read some open-source projects and encapsulated localstorage, but none of them fully composite business scenarios. So I wrote one myself.

I don't know if there is any active community like github in China. I feel that the Open Source atmosphere in China is not strong.

Usage:

 

Var wsCache = new WebStorageCache (); // cache string 'wqteam' to 'username'. The timeout value is 100 seconds. set ('username', 'wqteam', {exp: 100}); // timeout deadline: 2015 5 18wsCache. set ('username', 'wqteam', {exp: new Date ('1970 5 18')}); // obtain the value of 'username' in the cache wsCache. get ('username'); // cache a simple js object. The serialization method is JSON by default. stringify. You can configure serializer when initializing wsCache. serializewsCache. set ('user', {name: 'wu', organization: 'wqteam'}); // read a simple js object in the cache-The deserialization method is JSON by default. parse. You can configure serializer when initializing wsCache. deserializevar user = wsCache. get ('user'); alert (user. name + 'belongs to '+ user. organization); // Delete 'username' wsCache in the cache. delete ('username'); // manually delete all expired CacheItem, wsCache. deleteAllExpires (); // clear all cached wsCache on the client. clear (); // set a new timeout value for an existing (non-timeout) cache value. WsCache. touch ('username', 1); // If the cache does not contain a cache with the key username2, add username2. Otherwise, do not do wsCache. add ('username2', 'wqteam', {exp: 1}); // If the cache contains a cache with the key as username, replace it with the new value. Otherwise, do not perform wsCache. replace ('username', 'new wqteam', {exp: 1}); // check whether the storage selected as the cache is supported by your browser. // If the WebStorageCache API call is not supported, no action will be taken. WsCache. isSupported ();

  

APIConstructor
Var wsCache = new WebStorageCache ({// [Optional] 'localstore', 'sessionstore', window. localStorage, window. sessionStorage // or other Storage instances that implement [storage API. // The default value is 'localstore '. storage: 'localstorage', // [Optional] type Number, public timeout event settings. The default value is infinite exp: Infinity });

  

IsSupported

Check whether the storage selected as the cache is supported by your browser. If the WebStorageCache API call method is not supported, no action is required.

WsCache. isSupported (); // return value Boolean.

  

Set

Insert data into the cache.

// Key [required] must be of the String type. // Value [required] supports the JSON. parse type. NOTE: If it is undefined, the delete (key) operation is executed. // Options [Optional] js object, which contains two attributes: exp and force. // {// Type Number. Timeout time, in seconds. The default value is infinite. // Exp: 100, // type Boolean. True: when the maximum capacity is exceeded, you cannot continue data insertion. First, clear the expired content in the cache and then try to insert the data. The default value is true. // Force: true //} wsCache. set (key, value, options );

  

Get

No timeout data in the cache is obtained based on the key. Returns the values of the String, Boolean, PlainObject, and Array types.

// Key [required] String type. If the corresponding value of this key has expired, the delete (key) operation is performed and null is returned. WsCache. get (key );

  

Delete

Deletes the value in the cache based on the key.

wsCache.delete(key);

  

DeleteAllExpires

Delete all timeout values stored through WebStorageCache in the cache.

wsCache.deleteAllExpires();

  

Clear

Clear all values in the cache. Note: This method will clear values not inserted using WebStorageCache. Recommended:deleteAllExpires.

wsCache.clear();

  

Touch

Set a new timeout value based on the cache value with the key as the existing (not time-out) and the current time as the benchmark.

// Key [required] String type // exp [required] number unit: Second js object contains the exp attribute (New timeout time starting from the current time) wsCache. touch (key, exp: 1 );

  

Add

Insert based on the key. If the value corresponding to the key does not exist or times out, insert the value. Otherwise, do nothing. Note: If the value is not inserted through WebStorageCache, it will also be regarded as an invalid value and will still be executed.addOperation

wsCache.add(key, value, options);

  

Replace

Insert based on the key. If the value corresponding to the key exists and does not time out, insert the value. Otherwise, do nothing.
Note: The timeout value is reset based on the current time.

wsCache.replace(key, value, options);

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.