jjNford / html5-cache

Smart HTML5 localStorage caching

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HTML5 Cache

A local storage wrapper that acts as a time to live cache.

How To Use

  1. Include the cache script in your project:

    <html>
    ...
    ...
    <body>
    	...
    	...
    	
    	<script src="libs/html5-cache/cache.js"></script>
    	
    	...
    	...
    </body>
    </html>
  2. Use window.cache to access the cache:

    window.cache.setEnabled(true);
    window.cache.setSmart(true);
    window.cache.setTtl(10000);
    
    var data = requestDataFromApi();
    window.cache.setItem("api_data", data);
    ...
    ...
    ...
    
    data = window.cache.getItem("api_data");
    
    if(data.data == null || data.expired == "true") {
    	data = requestDataFromApi();
    }
    ...	
    ...
    ...

    If smart caching is enabled, expired data will be returned with data.expired = true. If smart caching is disabled, then null will be returned if the time to live (ttl) has expired.

  3. Include attribution to library.

API

clear()

Clears the cache.

--

getItem(key)

Retrieves an item from the cache.

{
  "data": data
  "expired": true|false
  "time": time-cached
  "ttl": time-to-live
}

--

isEnabled()

Determines if the cache is enabled.

--

isSmart()

Determines if the cache is smart.

--

removeItem(key)

Removes the data with the given key from the cache.

--

setEnabled(key)

Enable or disable the cache.

--

setItem(key, data)

Saves the given data under the given key to the cache.

--

setSmart(enabled)

Enables or disables the cache.

--

setTtl(ttl)

Sets the time to live for items saved to the cache.

About

Smart HTML5 localStorage caching

License:Other


Languages

Language:JavaScript 100.0%