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

cache-manager-fs-binary

v1.0.4

Published

file system store for node cache manager with binary data as files

Downloads

1,426

Readme

Build Status

node-cache-manager-fs-binary

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 4, 5 and iojs.

Installation

    npm install cache-manager-fs-binary --save

Usage examples

Here are examples that demonstrate how to implement the Filesystem cache store.

Features

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

Single store

// node cachemanager
var cacheManager = require('cache-manager');
// storage for the cachemanager
var fsStore = require('cache-manager-fs-binary');
// initialize caching on disk
var diskCache = cacheManager.caching({
    store: fsStore,
    options: {
        reviveBuffers: true,
        binaryAsStream: true,
        ttl: 60 * 60 /* seconds */,
        maxsize: 1000 * 1000 * 1000 /* max size in bytes on disk */,
        path: 'diskcache',
        preventfill: true
    }
});

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

// wrapper function, see more examples at node-cache-manager
diskCache.wrap(cacheKey,
    // called if the cache misses in order to generate the value to cache
    function (cacheCallback) {
        var image; // buffer that will be saved to separate file
        var moreData; // string that will be saved to a separate file
        var userLastVisit; // Date
        var signature; // small binary data to store inside as buffer

        // ... generating the image

        // now returning value to cache and process further
        cacheCallback(err,
// Some JSONable object. Note that null and undefined values not stored.
// One can redefine isCacheableValue method to tweak the behavior.
            {
                binary: {
// These will be saved to separate files and returned as buffers or
// readable streams depending on the cache settings.
// **NB!** The initial values will be changed to buffers or readable streams
// for the sake of simplicity, usability and lowering the memory footprint.
// Check that the keys are suitable as parts of filenames.
                    image: image,
                    someOtherBinaryData: moreData
                },
// Other data are saved into the main cache file
                someArbitraryValues: {
                    eg: userLastVisit
                },
                someSmallBinaryValues: {
// While buffer data could be saved to the main file, it is strongly
// discouraged to do so for large buffers, since they are stored in JSON
// as Array of bytes. Use wisely, do the benchmarks, mind inodes, disk
// space and performance balance.
                    pgpSignatureBufferForm: signature
                }
            });
    },
// Options, see node-cache-manager for more examples
    {ttl: ttl},
// Do your work on the cached or freshly generated and cached value.
// Note, that result.binary.image will come in readable stream form
// in the result, if binaryAsStream is true
    function (err, result) {

        res.writeHead(200, {'Content-Type': 'image/jpeg'});
        var image = result.binary.image;

        image.pipe(res);

        var usedStreams = ['image'];
        // you have to do the work to close the unused files
        // to prevent file descriptors leak
        for (var key in result.binary) {
            if (!result.binary.hasOwnProperty(key))continue;
            if (usedStreams.indexOf(key) < 0
                && result.binary[key] instanceof Stream.Readable) {
                if(typeof result.binary[key].close === 'function') {
                    result.binary[key].close(); // close the stream (fs has it)
                }else{
                    result.binary[key].resume(); // resume to the end and close
                }
            }
        }
    }
)

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;
    // callback fired after the initial cache filling is completed
    options.fillcallback = null;
    // if true the main cache files will be zipped (not the binary ones)
    options.zip = false;
    // if true buffers not from binary key are returned from cache as buffers,
    // not objects
    options.reviveBuffers = false;
    // if true, data in the binary key are returned as StreamReadable and 
    // (**NB!**) the source object will also be changed. 
    // You have to do the work for closing the files if you do not read them,
    // see example.
    options.binaryAsStream = false;

Tests

To run tests:

    npm test

Code Coverage

To run Coverage:

    npm run coverage

License

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

Credits

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