Code Monkey home page Code Monkey logo

node-cache-manager-fs-binary's Introduction

NPM

cache-manager-fs-stream

Node Cache Manager store for Filesystem with binary data

The Filesystem store for the node-cache-manager module, storing binary data as separate files, returning them as readable streams or buffers. This should be convenient for caching binary data and sending them as streams to a consumer, e.g. res.send(). The library caches on disk arbitrary data, but values of an object under the special key binary is stored as separate files.

Node.js versions

Works with versions upper than 16.x

Installation

    npm install cache-manager-fs-stream --save

Features

  • limit maximum size on disk
  • refill cache on startup (in case of application restart)
  • returns binary data as buffers or readable streams
  • can store buffers inside the single cache file

Single store

// node cachemanager
import { caching } from 'cache-manager';
// storage for the cachemanager
import { DiskStoreCache, create } from 'cache-manager-fs-stream';
// initialize caching on disk
const diskstore = create({
  ttl: 60 * 60 /* seconds */,
  maxsize: 1000 * 1000 * 1000 /* max size in bytes on disk */,
  path: 'diskcache',
  binaryEncode: true, // File will be binary encoded (serialized with v8)
  zip: true, // use zip to compress the cache files
});

const diskCache = await caching(diskstore);

// ...
var cacheKey = 'userImageWatermarked:' + user.id + ':' + image.id;
var ttl = 60 * 60 * 24 * 7 * 1000; // in ms

// wrapper function, see more examples at node-cache-manager
const result = diskCache.wrap(
  cacheKey,
  // called if the cache misses in order to generate the value to cache
  async () => {
    // get the image from the database
    return value; // value can be object, Buffer, Stream
  },
  // Options, see node-cache-manager for more examples
  { ttl: ttl },
);

Options

options for store initialization

// default values

// time to live in seconds
options.ttl = 60;
// path for cached files
options.path = 'cache/';
// prevent filling of the cache with the files from the cache-directory
options.preventfill = false;
// if true the main cache files will be zipped (not the binary ones)
options.zip = false;
// maximum size of the cache in bytes
options.maxsize = 1000 * 1000 * 1000;
// if true, the binary files are encoded with v8
options.binaryEncode = false;

Tests

To run tests:

    npm test

License

cache-manager-fs-stream is licensed under the MIT license.

Credits

Based on https://github.com/sheershoff/node-cache-manager-fs-binary

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.