Giter Site home page Giter Site logo

kwertop / persistent-node-cache Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 1.0 220 KB

Point-in-time recovery module for in-memory node-cache with asynchronous persistence

Home Page: https://www.npmjs.com/package/persistent-node-cache

License: MIT License

JavaScript 32.58% TypeScript 67.42%
cache disk-storage in-memory node persistent point-in-time recovery caching fast typescript

persistent-node-cache's Introduction

persistent-node-cache Tests

Light weight persistent cache backed by node-cache. It persists in-memory cache periodically on disk while also saving every write command in an append-only manner to guarantee minimal data loss during application restarts.

Quick Start

Install

npm i persistent-node-cache

Usage

Initialize Cache

import { PersistentNodeCache } from "persistent-node-cache";

const cache = new PersistentNodeCache("mycache", 1000);

Options

  • cacheName: string (required) the name of the cache. Used for recovering/restoring cache from disk.
  • period: number (default: 1 second) interval at which cache backup is saved
  • dir: string (default: home dir of user) directory where backup files will be created
  • opts: object standard options of node-cache package
  • serializer: CacheSerializer (default: JSON serializer) custom serializer for persisting data on disk

CacheSerializer Type

type CacheSerializer = {
    serialize: Function;
    deserialize: Function;
}

Cache Operations

All cache operations are similar to those of node-cache package since it's an extension of implementation of node-cache. Please refer to node-cache docs for an extensive list of all operations supported.

Store a key (SET):

cache.set(key, val, [ttl])

Sets a key-value pair. Defining ttl is optional.

cache.set("mykey", "myval", 1000);    //true

Retrieve a key (GET):

Get a key-value pair.

cache.get("mykey");    //myval

Delete a key (DEL):

Delete a key from cache. Returns the number of entries deleted.

cache.del("mykey");    //1

Restore/Recover Cache

The cacheName field should be passed to tell which cache to restore. If dir field was passed during cache initialization previously, it should be passed during recovery as well to locate the backup files.

const cache = new PersistentNodeCache("mycache");
cache.recover();

cache.get("mykey");    //myval

Use Custom Serializer

import { PersistentNodeCache, CacheSerializer } from "persistent-node-cache";

const customSerializer: CacheSerializer = {
    serialize: (item: any) => {
        return Buffer.from(Buffer.from(JSON.stringify(item)).toString('base64') + '\n');
    },
    deserialize: (bf: Buffer) => {
        return JSON.parse(Buffer.from(bf.toString().trim(), 'base64').toString());
    }
};

const cache = new PersistentNodeCache("mycache", 0, '', {}, customSerializer);

Benchmarks

Benchmark figures for get/set operations (on an Apple M2, 8 GB RAM system)

get set
persistent-node-cache 21,074,363 ops/sec 623,668 ops/sec
node-cache 15,939,686 ops/sec 8,862,257 ops/sec
persistent-cache 8,670,967 ops/sec 550 ops/sec

persistent-node-cache's People

Contributors

kwertop avatar rahulwinzo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

persistent-node-cache's Issues

`recover` fails if .backup file doesn't exist

recover fails if the .backup file doesn't exist, with an error Error: ENOENT: no such file or directory, open '{path}.backup'. This can occur when the server restarts before the first backup. With the default 1s backup interval, this probably will never happen, but with a longer interval, it definitely can. I guess this can be solved by either handling the missing .backup file during recover or by creating an empty file on the first start, as is the case for .append.

By the way, great package! It's exactly what I needed, and I'm surprised it's not more popular

Unnecessary rewrites of the backup file

The backup file seems to be rewritten on every interval. I assume it should be possible to track whether any data changed and skip writing backup file if there are no changes.

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.