Class: OfflineCache

QuickNav

OfflineCache

Offline Persistence Toolkit implementation of the standard Cache API. In additional to functionalities provided by the standard Cache API, this OfflineCache also interacts with shredding methods for more fine-grain caching. In addition to the Cache API, the OfflineCache also supports the clear() function to clear the cache.

Constructor

new OfflineCache(name, persistencestore)

Parameters:
Name Type Description
name String name of the cache
persistencestore Object instance for cache storage

Methods

add(request) → {Promise}

Takes a request, retrieves it and adds the resulting response object to the cache.
Parameters:
Name Type Description
request Request The request object to fetch for response and be cached.
Returns:
returns a Promise that is resolved when the reponse is retrieved and request/response is cached.
Type
Promise
Example
OfflineCache.add(request).then(function(){
// Your Code Here
});

clear() → {Promise.<Boolean>}

Clear the cache.
Returns:
Returns a promise that resolves to true when the cache is cleared. Will resolve to false if not all cache items were able to be cleared.
Type
Promise.<Boolean>
Example
OfflineCache.clear().then(function(isCleared){
// isCleared is a Boolean value
// Your Code here
});

delete(request, options) → {Promse.<Boolean>}

Delete the all the entries in the cache that matches the passed-in request with the specified options.
Parameters:
Name Type Argument Description
request Request The request object to match against
options {ignoreSearch: boolean, ignoreMethod: boolean, ignoreVary: boolean} <optional>
Options to control the matching operation
  • options.ignoreSearch A Boolean that specifies whether to ignore the query string in the url. For example, if set to true the ?value=bar part of http://foo.com/?value=bar would be ignored when performing a match. It defaults to false.
  • options.ignoreMethod A Boolean that, when set to true, prevents matching operations from validating the Request http method (normally only GET and HEAD are allowed.) It defaults to false.
  • options.ignoreVary A Boolean that when set to true tells the matching operation not to perform VARY header matching — i.e. if the URL matches you will get a match regardless of whether the Response object has a VARY header. It defaults to false.
Returns:
Finds the Cache entry whose key is the request, and if found, deletes the Cache entry and returns a Promise that resolves to true. If no Cache entry is found, it returns a Promise that resolves to false.
Type
Promse.<Boolean>
Example
OfflineCache.delete(request).then(function(result){
// result is a boolean that based on if the .delete was able to to find and remove a cached entry.
});

getName() → {string}

Retrieve the name of the cache object.
Returns:
Returns the name of the cache object.
Type
string
Example
// Assume the cache name is "employee"
OfflineCache.getName() // returns "employee"

hasMatch(request, options) → {Promise.<Boolean>}

Checks if a match to this request with the specified options exist in the cache or not. This is an optimization over match because we don't need to query out the shredded data to fill the response body.
Parameters:
Name Type Argument Description
request Request The request object to match against
options {ignoreSearch: boolean, ignoreMethod: boolean, ignoreVary: boolean} <optional>
Options to control the matching operation
  • options.ignoreSearch A Boolean that specifies whether to ignore the query string in the url. For example, if set to true the ?value=bar part of http://foo.com/?value=bar would be ignored when performing a match. It defaults to false.
  • options.ignoreMethod A Boolean that, when set to true, prevents matching operations from validating the Request http method (normally only GET and HEAD are allowed.) It defaults to false.
  • options.ignoreVary A Boolean that when set to true tells the matching operation not to perform VARY header matching — i.e. if the URL matches you will get a match regardless of whether the Response object has a VARY header. It defaults to false.
Returns:
Returns a promise that resolves to true if a match exist while false otherwise.
Type
Promise.<Boolean>
Example
OfflineCache.hasMatch(request).then(function(matchExists){
//your code here
});

keys(request, options) → {Promise.<Array>}

Retrieves all the keys in this cache.
Parameters:
Name Type Argument Description
request Request <optional>
The request object to match against
options {ignoreSearch: boolean, ignoreMethod: boolean, ignoreVary: boolean} <optional>
Options to control the matching operation
  • options.ignoreSearch A Boolean that specifies whether to ignore the query string in the url. For example, if set to true the ?value=bar part of http://foo.com/?value=bar would be ignored when performing a match. It defaults to false.
  • options.ignoreMethod A Boolean that, when set to true, prevents matching operations from validating the Request http method (normally only GET and HEAD are allowed.) It defaults to false.
  • options.ignoreVary A Boolean that when set to true tells the matching operation not to perform VARY header matching — i.e. if the URL matches you will get a match regardless of whether the Response object has a VARY header. It defaults to false.
Returns:
Returns a promise that resolves to an array of Cache keys.
Type
Promise.<Array>
Examples
// with a request object passed in, it will return all keys related that that request
OfflineCache.keys(request).then(function(keyArray){
//keyArray will only contain key that match the request
});
// without a request object, it will return all keys in the cache
OfflineCache.keys().then(function(allKeys){
// allKeys contains every key stored in the cache
});

match(a, options) → {Promise.<(Response|undefined)>}

Find the first response in this Cache object that match the request with the options.
Parameters:
Name Type Argument Description
a Request request object to match against
options {ignoreSearch: boolean, ignoreMethod: boolean, ignoreVary: boolean} <optional>
Options to control the matching operation
  • options.ignoreSearch - A Boolean that specifies whether to ignore the query string in the url. For example, if set to true the ?value=bar part of http://foo.com/?value=bar would be ignored when performing a match. It defaults to false.
  • options.ignoreMethod - A Boolean that, when set to true, prevents matching operations from validating the Request http method (normally only GET and HEAD are allowed.) It defaults to false.
  • options.ignoreVary - A Boolean that when set to true tells the matching operation not to perform VARY header matching — i.e. if the URL matches you will get a match regardless of whether the Response object has a VARY header. It defaults to false.
Returns:
Returns a Promise that resolves to the Response associated with the first matching request in the Cache object. If no match is found, the Promise resolves to undefined.
Type
Promise.<(Response|undefined)>
Examples
OfflineCache.match(request).then(function(response){
// Your code here
});
OfflineCache.match(request,{ignoreSearch:true}).then(function(response){
// Your code here
});

matchAll(request, options) → {Promise.<Array.<Response>>}

Finds all responses whose request matches the passed-in request with the specified options.
Parameters:
Name Type Argument Description
request Request The request object to match against
options {ignoreSearch: boolean, ignoreMethod: boolean, ignoreVary: boolean} <optional>
Options to control the matching operation
  • options.ignoreSearch A Boolean that specifies whether to ignore the query string in the url. For example, if set to true the ?value=bar part of http://foo.com/?value=bar would be ignored when performing a match. It defaults to false.
  • options.ignoreMethod A Boolean that, when set to true, prevents matching operations from validating the Request http method (normally only GET and HEAD are allowed.) It defaults to false.
  • options.ignoreVary A Boolean that when set to true tells the matching operation not to perform VARY header matching — i.e. if the URL matches you will get a match regardless of whether the Response object has a VARY header. It defaults to false.
Returns:
Returns a Promise that resolves to an array of response objects whose request matches the passed-in request.
Type
Promise.<Array.<Response>>
Example
OfflineCache.matchAll(request).then(function(responseArray){
// Your code here
});

put(request, response) → {Promise}

Add the request/response pair into the cache.
Parameters:
Name Type Description
request Request Request object of the pair
response Response Response object of the pair
Returns:
Returns a promise when the request/response pair is cached.
Type
Promise
Example
OfflineCache.put(request,response).then(function(){
// response has been cached
// your code here
});