Git Product home page Git Product logo

fscache's Introduction

DEPRECATED!

A much better option is to use Levelup & RocksDB

version 0.0.1

FSCache

FSCache is a filesystem based caching utility. It stores values based on key/value. Key and Value must both be JSON serializable.

Where should I use this

Since FSCache writes to disk (obviously), you should not use this tool as a frequently accessed data store for high traffic websites/web apis. FSCache is better used for command line tools, infrequently accessed configurations, or any project where you need a lightweight key/val store and performance for high traffic is not a concern.

Though the api contains both sync and async versions for accessing the cache, reading/writing to disk is an expensive process.

Install

npm install fscache

API

As typical in node, there is a async and sync version of each method.

Creating a new cache

This method creates a new directory that you specify which will act as your cache storage. It returns a Cache object for accessing the cache.

/*
 * @param {Number} ttl - how long to store a value. null will store forever
 *		default: null
 * @param {String} dir - Where to store data
 */

var Cache = require("fscache");

// Async
Cache.create(50, dir, function (err, cache) {
	// Do stuff with cache	
});

// Sync
var cache = Cache.createSync(1000, dir);

Put

Store data

/*
 * @visibility private
 * @param {Mixed} key - A JSON stringifiable value 
 * @param {Mixed} val - a JSON stringifiable value
 */
 
 // Async
 cache.put({id: 1}, "cached", function(err), function () {
 });
 
 cache.putSync({id: 2}, {cached: "data"});

Get

Get a stored value

/*
 * @param {Mixed} key - A JSON stringifiable key that is used to lookup
 *                      up a cached file
 */
 
 // Async
 cache.get({id: 1}, function (err, val) {
     if (err) throw err;
     console.log(val); // "cached";
 });
 
 // Sync
 var val = cache.get({id: 2});
 console.log(val); // {cached: "data"}
 

Del

/*
 * @param {Mixed} key - A JSON stringifiable key that is used to lookup
 *                      up a cached file
 */
 
 // Async
 cache.del({id:1}, function (err) {
     if (err) throw err;
 });
 
 // Sync
 cache.del({id: 2});

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.