A local storage wrapper that acts as a time to live cache.
-
Include the cache script in your project:
<html> ... ... <body> ... ... <script src="libs/html5-cache/cache.js"></script> ... ... </body> </html>
-
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. -
Include attribution to library.
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.