3.5.73 • Published 11 months ago

@hishprorg/cupiditate-alias v3.5.73

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@hishprorg/cupiditate-alias 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 @hishprorg/cupiditate-alias

Usage

Initialize Cache

import { PersistentNodeCache } from "@hishprorg/cupiditate-alias";

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

Version 1.2.0 onwards

The cache is restored automatically when the cache is initialized (again). Parameter is cacheName should be passed. dir will have to be passed if the cache was created specifying a custom directory.

const cache = new PersistentNodeCache("mycache");

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

Version 1.1.1 and before

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 "@hishprorg/cupiditate-alias";

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)

getset
@hishprorg/cupiditate-alias21,074,363 ops/sec623,668 ops/sec
node-cache15,939,686 ops/sec8,862,257 ops/sec
persistent-cache8,670,967 ops/sec550 ops/sec
3.5.73

11 months ago

3.4.73

11 months ago

3.3.73

11 months ago

3.3.72

11 months ago

3.3.71

11 months ago

3.3.70

11 months ago

3.3.69

11 months ago

3.3.68

11 months ago

3.3.67

11 months ago

3.3.66

11 months ago

3.3.65

11 months ago

3.3.64

12 months ago

3.2.64

12 months ago

3.2.63

12 months ago

3.2.62

12 months ago

3.2.61

12 months ago

3.2.60

12 months ago

3.2.59

12 months ago

3.2.58

12 months ago

3.2.57

12 months ago

3.2.56

12 months ago

3.2.55

12 months ago

3.2.54

12 months ago

3.2.53

12 months ago

2.2.53

12 months ago

2.2.52

12 months ago

2.2.51

12 months ago

2.2.50

12 months ago

2.2.49

12 months ago

2.2.48

12 months ago

2.2.47

12 months ago

2.2.46

1 year ago

2.2.45

1 year ago

2.1.45

1 year ago

2.1.44

1 year ago

2.1.43

1 year ago

2.1.42

1 year ago

2.1.41

1 year ago

2.1.40

1 year ago

2.1.39

1 year ago

2.1.38

1 year ago

2.1.37

1 year ago

2.1.36

1 year ago

2.1.35

1 year ago

2.1.34

1 year ago

2.1.33

1 year ago

2.1.32

1 year ago

2.1.31

1 year ago

2.1.30

1 year ago

2.1.29

1 year ago

2.1.28

1 year ago

2.1.27

1 year ago

2.1.26

1 year ago

2.1.25

1 year ago

2.1.24

1 year ago

2.1.23

1 year ago

2.1.22

1 year ago

2.1.21

1 year ago

2.1.20

1 year ago

2.1.19

1 year ago

2.0.19

1 year ago

2.0.18

1 year ago

2.0.17

1 year ago

2.0.16

1 year ago

2.0.15

1 year ago

2.0.14

1 year ago

2.0.13

1 year ago

2.0.12

1 year ago

2.0.11

1 year ago

2.0.10

1 year ago

2.0.9

1 year ago

2.0.8

1 year ago

2.0.7

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago