cache-blob-store
cache-blob-store is
designed for caching online resources (mainly images) for offline web
apps. It uses an
abstract-blob-store
compatible store for storing blobs (images) and implements and
abstract-blob-store compatible interface itself. On top of the
abstract-blob-store API it adds additional helper methods for
downloading a resource to the store and creating an
ObjectUrl
for an image in the cache
var cache = new Cache(opts)
Creates a new instance of a cache-blob-store.
Parameters
optsObjectopts.urlTransformfunction Transforms the url before storing as a key. Default transform removes protocol and numbered sub-domains (for map tiles which are duplicated across numbered subdomains)opts.storeObject Instance of an abstract-blob-store. Defaults to idb-content-addressable-blob-store in the browser and content-addressable-blob-store in node / electron (data stored under./cache-store). Content-addressable stores are preferable, but it should work with any abstract-blob-storeopts.metaDbObject Levelup db - defaults to level-js in the browser, in node / electron saves db to./cache-metadb
Caching helper methods
cache.download(opts, cb)
Download a resource and store it in the cache
Parameters
urlstringcbFunction called witherr, metadata
cache.getObjectURL(url, cb)
Returns an ObjectUrl for an image in the cache
Parameters
urlstring Url to the image resourcecbFunction called witherr, urlwhereurlis an URL representing the image object in the cache. Should be revoked withURL.revokeObjectUrl(url)when no longer needed (i.e. when the Image has loaded)
cache.get(url, cb)
Get a resource (e.g. image) from the cache
Parameters
urlstring Url to the resourcecbFunction called witherr, blobwhereblobwill havetypecontent-type defined from the URL file extension if an image, or metadatacontent-typestored when the resource was added to the cache
cache.put(opts, blob, cb)
Store a resource (e.g. image) in the cache
Parameters
optsstring or Object String url key, or options object with requiredkeyand optional metadata.content-typemetadata will be parsed from theblobopts.keystring url key of the resource to cache
blobBlob Image / blob datacbFunction called witherr, metadatawhen finished
abstract-blob-store compatible methods
cache.createReadStream(opts)
Open a read stream to a blob in the store for url key
Parameters
optsstring or Object String url key, or options object withkeyproperty
cache.createWriteStream(opts, cb)
Create a writable stream to add a new blob to the store. Expects a string
url key for the resource, or an object with required property key and
optional metadata from resource headers, such as content-type (jpg, png
and webp content types are guessed from their extension)
Parameters
optsstring or Object String url key, or options object with requiredkeyand optional metadataopts.keystring url key of the resource to cache
cbFunction Called when stream has finished writing witherr, metadata
cache.exists(opts, cb)
Check if opts.key exists with cb(err, exists)
Parameters
optsstring or Object String key or object with propertykeycbFunctioncb(err, exists)whereexistsis boolean
cache.remove(opts, cb)
Remove an image from the cache for url
Parameters
optsstring or Object String url key, or options object with requiredkeyand optional metadata.content-typemetadata will be parsed from theblobopts.keystring url key of the resource to cache
cbFunction called witherr
======================
See it on GitHub
Return to Code

