npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cloud-cache

v1.0.3

Published

[![Build Status](https://travis-ci.org/blockai/cloud-cache.svg?branch=master)](https://travis-ci.org/blockai/cloud-cache)

Downloads

117

Readme

cloud-cache

Build Status

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

Features

Install

npm install --save cloud-cache

Requires Node v6+

Usage

See ./test directory for usage examples.

Setting up the client

import cloudCache from 'cloud-cache'
const cache = cloudCache(blobStore [, opts])
  • blobStore: blobStore abstract-blob-store instance
  • opts.keyPrefix: String cloudcache/ global key prefix that will be automatically prepended to all keys

Promise API

All methods return promises.

cache.get(key) Get a key.

  • key: String the key to get

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.

  • key: String the key to set
  • value: Mixed Buffer or any JSON compatible value.
  • opts.ttl: Number, Infinity Time to live: how long the data needs to be stored measured in seconds
cache.set('foo', 'bar', { ttl: 60 * 60 })

cache.del(key) Delete a key.

  • key: String the key to delete
cache.del('key')

cache.getOrSet(key, getValueFn [, opts]) Returns cached value, storing and returning value on cache misses.

  • key: String the key to get
  • getValueFn: Function function to evaluate on cache misses
  • opts: Object same as cache.set
  • opts.refresh: Boolean false forces a cache miss

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())
))

Stream API

cache.getStream(key)

  • key: String the key to read

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])

  • key: String the key to set
  • opts: Object same as cache.set

Returns a Writable Stream.

Alias: cache.sets(key)

resizeImage('/tmp/avatar.png').pipe(cache.setStream('olalonde/avatar.png'))

cache.getOrSetStream(key, getStreamFn [, opts])

  • key: String the key to get
  • getStreamFn: Function Read Stream returning function that will be called on cache misses.
  • opts: Object same as cache.getOrSet

Returns a Readable Stream.

Important:

  • The stream returned by getStreamFn might not be cached if the returned read stream is not fully consumed (e.g. by piping it).
  • A finish event is fired to indicate that the stream was completely saved to the cache.
cache.getOrSetStream('olalonde/avatar.png', () => resizeImage('/tmp/avatar.png')).pipe(req)

Error Handling

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)
})

Errors

  • CloudCacheError this base class is inherited by the errors below
  • KeyNotExistsError thrown/emitted when trying to get a non existent / expired key. Exposes a key property

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 // true

How it works

Cloud-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.

Partial Writes / Durability

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.