Appcacher
Create and manage your cache with Appcacher.
NOTE: Appcacher only works for caching GET requests.
Install
Install with npm:
$ npm install appcacher
Quickstart
; const app = 'my-pokemon-cache' 60 * 60 * 1000; // keep the cache for 1 hourapp; const url = 'https://pokeapi.co/api/v2/pokemon/1';const req = url; app; //or app; { const cachedResponse = await app; console; // will console result from the cache which came from the pokeApi. } ;
API
Constructor
Params
name
{String}: Name for the cache.ttl
{Number}: Time to live for the cache.
Example
const app = 'my-pokemon-cache';
.create
Creates a cache.
Params
returns
{Object}: A Promise that resolves to the requested Cache object.
Example
app;
.add
Add request
to cache. You can either use the url
or the object
of the request.
Params
req
{String|Object}: The url of the request to be cached, or the request object to be cached. For example:const req = new Request(url)
Example
app;// orconst otherRequestParams = {}; // like headers, method etcconst req = 'https://request/to/cache' otherRequestParamsapp;
.addAll
Add multiple requests
to cache. You can either use the urls
or the objects
of the request.
Params
req
{Array}: The Array of urls of the request to be cached, or the request objects to be cached.
Example
app;// orconst otherRequestParams = {}; // like headers, method etcconst req1 = 'https://request/to/cache/1' otherRequestParamsconst req2 = 'https://request/to/cache/2' {}const req3 = 'https://request/to/cache/3' {}app;
.get
Return the response of request
from cache. You can use the url
or the object
of the request.
Params
req
{String|Object}: The url of the request to be matched, or the request object.returns
{Object}: 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.
Example
let cachedResponse; cachedResponse = app;// orconst otherRequestParams = {}; // like headers, method etcconst req = 'https://request/to/cache' otherRequestParamscachedResponse = app;console; // API response.
.getAll
Return all responses of a matching request
from cache. You can use the url
or the object
of the request.
Params
req
{Array}: The url of the request to be matched, or the request object.options
{Object}: (Optional params) An options object allowing you to set specific control options for the matching performed. The available options are given below. Default is an empty object.returns
{Object}: Returns a Promise that resolves to the array of all matching responses. If no match is found, the Promise resolves to undefined.
Optional Params
ignoreSearch
{Boolean}: A Boolean that specifies whether the matching process should ignore the query string in the url. It defaults to false.ignoreMethod
{Boolean}: A Boolean that, when set to true, prevents matching operations from validating the Request http method.ignoreVary
{Boolean}: 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 the Response object having a VARY header or not. It defaults to false.
Example
let cachedResponse; cachedResponse = app;// orconst otherRequestParams = {}; // like headers, method etcconst req = '/cache' otherRequestParamscachedResponse = app;console; // API responses Array.
.remove
Removes a specific request from the cache. You can use the url
or the object
of the request.
Params
req
{String|Object}: The url of the request to be removed, or the request object to be removed.returns
{Object}: a Promise that resolves to true if the cache entry is removed, or false otherwise.
Example
app;// orconst otherRequestParams = {}; // like headers, method etcconst req = 'https://request/to/cache' otherRequestParamsapp;
.delete
Deletes the entire cache from the browser.
Params
returns
{Object}: a Promise that resolves to true if the cache is deleted, or false otherwise.
Example
app;
.put
Allows key/value to be added to the current Cache object. Note that put
will overwrite any key/value pair previously stored in the cache that matches the request.
Params
req
{String|Object}: The url of the request to add, or the request object to be added.resp
{Object}: The response of the request to add, for ex:new Response('{"foo": "bar"}'))
. If you don't want to pass anything in response param, useadd()
.
Example
app;// orconst otherRequestParams = {}; // like headers, method etcconst req = 'https://request/to/cache' otherRequestParamsconst options = headers: 'Content-Type': 'application/json' const resp = foo: 'bar' options;app;
.has
Check whether your cache is present or not
Params
cacheName
{String}: The name of the cache to check.returns
{Boolean}: returns true if the cache is present, or false otherwise.
Example
app; // returns true
.getAllCacheNames
Returns an array with the names of all caches present. This can be useful if you want to iterate over the entries in all caches.
Params
returns
{Array}: list with the names of all caches present.
Example
; // returns ['my-pokemon-cache']
.getStorageQouta[EXPERIMENTAL]
Returns the size, in bytes, the total qouta and the used qouta.
Params
returns
{Object}: list with the usage and total qouta with details.
Example
; // returns {quota: 239400581529, usage: 725290, usageDetails: {caches: 721920, indexedDB: 3370}}
License
Copyright © 2020, Gautam Pahuja. Released under the MIT License.