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
opts
Objectopts.urlTransform
function 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.store
Object 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.metaDb
Object 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
url
stringcb
Function called witherr, metadata
cache.getObjectURL(url, cb)
Returns an ObjectUrl for an image in the cache
Parameters
url
string Url to the image resourcecb
Function called witherr, url
whereurl
is 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
url
string Url to the resourcecb
Function called witherr, blob
whereblob
will havetype
content-type defined from the URL file extension if an image, or metadatacontent-type
stored when the resource was added to the cache
cache.put(opts, blob, cb)
Store a resource (e.g. image) in the cache
Parameters
opts
string or Object String url key, or options object with requiredkey
and optional metadata.content-type
metadata will be parsed from theblob
opts.key
string url key of the resource to cache
blob
Blob Image / blob datacb
Function called witherr, metadata
when finished
abstract-blob-store compatible methods
cache.createReadStream(opts)
Open a read stream to a blob in the store for url key
Parameters
opts
string or Object String url key, or options object withkey
property
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
opts
string or Object String url key, or options object with requiredkey
and optional metadataopts.key
string url key of the resource to cache
cb
Function Called when stream has finished writing witherr, metadata
cache.exists(opts, cb)
Check if opts.key
exists with cb(err, exists)
Parameters
opts
string or Object String key or object with propertykey
cb
Functioncb(err, exists)
whereexists
is boolean
cache.remove(opts, cb)
Remove an image from the cache for url
Parameters
opts
string or Object String url key, or options object with requiredkey
and optional metadata.content-type
metadata will be parsed from theblob
opts.key
string url key of the resource to cache
cb
Function called witherr
======================
See it on GitHub
Return to Code