| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Node.js caching library with pluggable backing store via abstract-blob-store. Streaming support makes it particularly useful for caching larger values like resized/cropped images or transcoded videos.
Table of Contents
npm install --save cloud-cacheRequires Node v6+
See ./test directory for usage examples.
import cloudCache from 'cloud-cache'
const cache = cloudCache(blobStore [, opts])All methods return promises.
cache.get(key) Get a key.
Throws a KeyNotExistsError error if the key doesn't exist or value has expired.
cache.get('key')cache.set(key, value [, opts]) Stores a new value in the cache.
cache.set('foo', 'bar', { ttl: 60 * 60 })cache.del(key) Delete a key.
cache.del('key')cache.getOrSet(key, getValueFn [, opts]) Returns cached value, storing and returning value on cache misses.
The arguments are the same as cache.set, except that value must be a function or a promise returning function that evaluates / resolves to a valid cache.set value. The function will only be evaluated on cache misses.
cache.getOrSet('google.com', () => (
fetch('http://google.com/').then(body => body.text())
))cache.getStream(key)
Returns a Readable Stream.
Emits a KeyNotExistsError error if the key doesn't exist or value has expired.
Alias: cache.gets(key)
cache.getStream('olalonde/avatar.png').pipe(req)cache.setStream(key [, opts])
Returns a Writable Stream.
Alias: cache.sets(key)
resizeImage('/tmp/avatar.png').pipe(cache.setStream('olalonde/avatar.png'))cache.getOrSetStream(key, getStreamFn [, opts])
Returns a Readable Stream.
Important:
cache.getOrSetStream('olalonde/avatar.png', () => resizeImage('/tmp/avatar.png')).pipe(req)The streams returned by cache may emit error events. We recommend using pipe() from the mississippi module to avoid unhandled errors and make sure the cache stream closes properly if the destination has an error.
e.g.:
import { pipe } from 'mississippi'
// ...
pipe(cache.getOrSetStream('key', getReadStream), req, (err) => {
if (err) return next(err)
})The error classes can be accessed through import or as a property on the cache object, e.g.:
import { CloudCacheError, KeyNotExistsError } from 'cloud-cache'
// ...
cache.CloudCacheError === CloudCacheError // true
cache.KeyNotExistsError === KeyNotExistsError // true
KeyNotExistsError instanceof CloudCacheError // trueCloud-cache encodes each cached value as a file stored on a storage provider (S3, file system, etc.). The files start with a small JSON header which contains metadata (e.g. creation time, ttl, data type, etc.), followed by a newline character (\n) and finally, the actual cached value. Values are encoded as JSON, except for buffers or streams which are stored as raw bytes.
This means that cached values aren't very useful to applications which are unaware of the header.
If you are caching transformed images to S3 for example, you couldn't reference the S3 URL directly from an HTML image tag for example (because the browser wouldn't know it needs to ignore everything before the first newline character).
You could however serve the images from a Node.js HTTP server and use the stream API to stream the image from S3 (e.g. cache.gets('olalonde/avatar.png').pipe(res)).
Cloud-cache evicts expired values on read which means that expired values will remain stored as long as they are not read.
Cloud-cache does not guarantee that set operations will be atomic and instead delegates that responsibility to the underlying store implementation. If the underlying store doesn't guarantee atomic writes, partial writes can happen (e.g. if the process crashes in the middle of a write). For example, fs-blob-store will happily write half of a stream to the file system. s3-blob-store, on the other hand, will only write a stream which has been fully consumed.
| Back | FazBrowse Home | New Git URL |