Offline data cache (offline.js)
offline.js enables access to recently retrieved data when the application becomes disconnected.
It can also act as a simple expiring data cache, or as a wrapper for localStorage access.
This library does not depend on anything else besides DOM Storage.
If localStorage is not available, then it will simply not ever hold anything in cache. Nothing needs to change from the client perspective.
Primary Usage Pattern
To achieve both an expiring cache (for recently requested data) as well as an offline data cache, this is the pattern to integrate into your remote data access.
To achieve just an offline data cache, skip step 1 or set expiry to zero.
- If
offline.fresh(key), then returnoffline.get(key). - Otherwise, execute remote API call.
- If remote API call was successful, store
offline.set(key, value, expiry)and returnvalue. - If remote API call failed (e.g.,
status === 0), attemptoffline.get(key). - If
valueis missing, notify caller of offline/remote error.
lscache
This library began as a fork of the excellent lscache by Pamela Fox.
While trying to implement the offline usage pattern, it became clear that some breaking changes needed to be made to the interface.
Rather than confuse the two libraries, offline.js deviates from lscache.js to solve a related but slightly different problem:
- Additional methods were added to allow insight into freshness of cached items and the expiring of items without removing.
offline.get()does not automatically remove items by default.
There is an optional argument to achieve that effect.- Data stored without expiry times are given an expiry of
MAX_DATE/2.
This enables expunging non-expiring values by insertion time but they still effectively never expire. - The storage format has been altered to make a little easier to read with smaller keys and expiry values.
- All data values are JSON encoded (including strings, unlike in lscache) to avoid ambiguity with JSON-like string values.
- The additional space required is more than made up for by shorter keys and expiry values.
- The concept of "buckets" has been removed in favor of being able to
flush()items with a prefix.
The purpose is the same but the usage is simpler and less error prone.
Online / Offline Events
Initially, this was going to incorporate hooks for HTML 5 Online/Offline events.
Ultimately, this seemed to only be partially useful and potentially broken.
Even in browsers where navigator.onLine is implemented to spec, it doesn't apply to the situation where the remote API is unavailable.
The decision has therefore been made to allow the application to decide what is considered "offline".
Typically, this is implemented as a status of 0 as opposed to the server response of the 400-500 range.
Methods
value = offline.get(key, expunge)
Retrieves a previously stored value, optionally removing stale data.
Arguments
stringkey: the storage keybooleanexpunge (optional): if should auto-remove expired items
Returns
object|array|string|number|boolean|null: the storage value
offline.set(key, value, expiry)
Stores the key-value pair with a freshness time limit.
Arguments
stringkey: the storage keyobject|array|string|number|boolean|nullvalue: the storage valuenumberexpiry (optional): the number of seconds before value is stale
isFresh = offline.fresh(key)
Tests the expiration of the data. Returns false if the value is stale or missing.
Arguments
stringkey: the storage key
Returns
boolean: if the value has expired or is missing
offline.expire(key)
If still fresh then marks the data as now expired.
Arguments
stringkey: the storage key
offline.remove(key)
Removes the data stored at key.
Arguments
stringkey: the storage key
offline.flush(prefix)
Removes all data with the given key prefix.
Arguments
stringprefix (optional): the storage key
offline.enableWarnings(enabled)
Enables or disables log warnings on quota or insertion errors.
Arguments
booleanenabled: true to enable warnings
hasLocalStorage = offline.supported()
Tests the support of localStorage.
Returns
boolean: if localStorage is supported.
Unit Tests
Run the unit tests.
License
offline.js is licensed under the Apache License, Version 2.0.
Copyright © 2013, Stephen M. McKamey.
offline.js is a derivative work of lscache which is also licensed under the Apache License, Version 2.0.
Copyright © 2011, Pamela Fox.