Code Monkey home page Code Monkey logo

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

Node Cache Manager store for Filesystem

Build Status dependencies Status npm package node

A Filesystem store for the node-cache-manager module

Installation

npm install cache-manager-fs-hash --save

Features

  • Saves anything that is JSON.stringify-able to disk
  • Buffers are saved as well (if they reach a certain size they will be stored to separate files)
  • Works well with the cluster module

Usage example

Here is an example that demonstrates how to implement the Filesystem cache store.

const cacheManager = require('cache-manager');
const fsStore = require('cache-manager-fs-hash');

const diskCache = cacheManager.caching({
    store: fsStore,
    options: {
        path: 'diskcache', //path for cached files
        ttl: 60 * 60,      //time to life in seconds
        subdirs: true,     //create subdirectories to reduce the
                           //files in a single dir (default: false)
        zip: true,         //zip files to save diskspace (default: false)
    }
});


(async () => {

    await diskCache.set('key', 'value');
    console.log(await diskCache.get('key')); //"value"
    await diskCache.del('key');
    console.log(await diskCache.get('key')); //undefined


    console.log(await getUserCached(5)); //{id: 5, name: '...'}
    console.log(await getUserCached(5)); //{id: 5, name: '...'}

    await diskCache.reset();

    function getUserCached(userId) {
        return diskCache.wrap(userId /* cache key */, function () {
            return getUser(userId);
        });
    }

    async function getUser(userId) {
        return {id: userId, name: '...'};
    }

})();

How it works

The filename is determined by the md5 hash of the key. (The key is also saved in the file to detect hash collisions. In this case it will just return a cache miss). Writing is performed with .lock files so that multiple instances of the library (e.g. using the cluster module) do not interfere with one another.

Tests

npm test

License

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

node-cache-manager-fs-hash's People

Contributors

rolandstarke avatar dschau avatar

Watchers

James Cloos avatar  avatar

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.