Catalog npm
Back to packageRELEASE DETAILS

1.0.4

npm

2q-cache

Imlementation of 2Q Cache Algorithm

2q2q algorithm2q cache2q-cachecachedouble queuedouble queueslfulrumrucache modulenode cachenode-cache
Published10 Dec 2019
Direct dependencies1
Transitive dependenciesNot calculated

README

Current package documentation

2Q cache

Simple and configurable cache library for Node.js and Browser that implements 2Q Cache algorithm (http://www.vldb.org/conf/1994/P439.PDF) This algorithm tries to preserve the most used elements, so it demonstrates better cache hit than other caches,for example, LRU (Least Recently Used) cache.

Installation:

npm i 2q-cache

Usage:

const Cache2Q = require('2q-cache');

let cache = new Cache2Q(400); //sets max count of cache elems
/*
    20% of size will be "in" FIFO bucket,
    60% - "out" FIFO bucket,
    20% - "main" LRU cache

    OR

    let cache = new Cache2Q[400, 440, 200] - you also can define manually sizes of each section in cache.
    First element of array is size of "in" FIFO bucket,
    Second - size of "out" FIFO bucket,
    Third - size of "main" LRU cache
*/

Example with options

const Cache2Q = require('2q-cache');

let options = {
    ttl : 1000 * 60 * 60, //default time to delete elements from cache : number
    stringKeys : true, // If stringKeys are true, keys of cache elements will be convert to string by JSON.stringify
    ttlInMain : false, //you can disable time to delete in 'main' section of cache. It can be useful, because in main                           //section stored most requested cache elements
    mainStorageType : 'lfu' //Type of main storage. It can be 'lru' - Least Recently Used,
                            //'mru' - Most Recently used,
                            //'lfu' - Least-Frequently used
}

let cache = new Cache2Q(400, options); //or ([100,200,250], options);

By default

  • cache ttl (lifetime) equals 0, so elements will not be removed by timeout
  • stringKeys are false, so new elements are saving "as is", without using JSON.stringify

You also can use Typescript version of 2Q cache module:

import {Cache2Q, CacheException} from '2q-cache/ts/cache2Q';

let cache : Cache2Q = new Cache2Q(400); 

API

  • set(key : any, value : any, ttl : number = 0 (optional))

    Sets new pair (key, value) with lifetime (optional) if key exists, it updates value and ttl parameter ttl overrides default ttl settings

  • mset(Array<object {key : any, value : any, ttl : number = 0}>)

    the same as 'set', but for array of new elements

  • get(key) => value

    Returns value by presented key if key doesn't exists, returns null

  • mget(Array<key : any>) : Array<values : any>

    the same as 'get', but for array of keys

  • has(key) => boolean

    Returns true if key exists, otherwise returns false

  • mhas(Array<key : any>) : Array<boolean>

    the same as 'has', but for array of keys

  • delete(key : any) => boolean

    deletes pair (key, value) from cache. If element doesn't exists, returns false.

  • mdel(keys : Array<any>) : Array<boolean>

    the same as 'del', but for array of keys

  • resetTtl(key : any, ttl : number)

    resets (overrides) ttl for element by presented key.

  • mresetTtl(keys : Array<object {key : any, ttl : number}>)

    the same as 'resetTtl', but for array of keys

  • setDefaultTtl(ttl : number)

    sets default ttl.

  • setStringifyKeys(param : boolean)

    overrides default setting for stringify keys. if stringify keys are true, keys will be converted to string by JSON.stringify

  • clear()

    Clears cache

  • alloc(size : number | Array<number>)

    Sets new size for each cache section. Parameter same as constructor parameter. Doesn't allow make max section size smaller

  • allocUnsafe(size : number | Array<number>)

    Sets new size for each cache section. Parameter same as constructor parameter . Allow make max section size smaller. If new max size smaller than current size on some cache section, older elements will be removed.

  • getSize()

    Returns object like this:

    {
        buckets : {
            in : {
                currentSize : 10,
                maxSize : 20
            },
            out : {
                currentSize : 0,
                maxSize : 60
            },
        },
        main : {
            currentSize : 0,
            maxSize : 20
        }
    }

ROADMAP

  • Any ideas?

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 1

Native requirements
1 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
mocha
Environment
{
  "local_name": "mocha",
  "alias": false,
  "peer_metadata": null
}
^6.2.2development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-d19NBtXhHgjQWYJWteY6dqWi10wZKsGDeppWbuNO63LFWmo/FG3NsAhdG2ZRjZOYOyGqLTxMtaICpOjxSTM10A==",
  "shasum": "46112c055185f1687a41962b3152cd612b90c258",
  "tarball": "https://registry.npmjs.org/2q-cache/-/2q-cache-1.0.4.tgz",
  "fileCount": 15,
  "unpackedSize": 39232,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJd73vnCRA9TVsSAnZWagAAk3MP/3S9A3/Xv1UAgdHetmp0\nNfW6maJiR2bAjivbHLsFYcxWXecnVh83yQjrBzQQVFSoiY2ZCudUQL/m9U+c\n4HZDI4ZSQiGMiC/ufoUjFbzzfWPrbA6N3oOz1/mAY2Y1RrhfhKKHtGaT9iz1\nwu1FyVyhr9Vl2Y4/d1fFjIVDT4DMm/MZW+z7XNTiuXfKXrQGdBpYb4PzacGJ\nIHAy7ySzul4jSRjuhGj3mWpOruFPsHpXqVUk9UUqrL4zcHgSAWlT6lnfsQZq\n7HOxlKJogLlp4smI7b/iS51JbKnhRSAKxd1quTnoPpUE+LDN1QtMXiJ9JRWr\nltMOxbXsPQlKthBM/uuHpaBjgc21ZyrqbuV/bSW/EFn9fk4qU7etmceHyLVQ\nYMWYRXKXRhBeD6vIm+4SvbatumbvJvb9lbotF/YCopf5Lf4Sy7BTIIqKFvj9\ntcF7qyOUdkUOb+y6SBHpRoi3ncqbbRKxhn45SH6zhfBBfY41eZsz4ix3du++\n7tu40urKZR5Uz+RD90vkk/f5WFvoGEBTMg6BysfkD/2uITI2ugtPGaF9ZR5e\n26AXD0PqDYSU9gP4q0ZNX6Wav3YvlJO5JBqT97hgHv+q4N2KOzmQWe0le+tH\nU/ucyMsx0dH2roQHSQVhaqP8MsXLUhggzv6ZHUoGsdqK6xfXjEp2ExNstvAc\nhUJ1\r\n=wkkh\r\n-----END PGP SIGNATURE-----\r\n",
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEUCIAICxq7kDRnWOq5R5lDRbLvi/juLKg5Do6MUGgADU9uKAiEA2jfl3sMqL60GrHm+PqbTmbPvMz/onPr+NGkPIIvOg90="
    }
  ]
}

Repository

ACCESSIBLE
artmbrisov/2q-cache

Shared repository data · GitHub

License detected in repository
Not collected
Last push
14 Dec 2019
Archived
No
Contributor identities
— · partial collection

Public sponsor counts have not been collected. These signals belong to the repository owner.

Downloads & popularity

1count
Gauge · observed snapshots20 Oct 2026
10.50
2026-10-20: 1 · observed 2026-10-20T21:06:21.3689445Z
20 Oct20 Oct

github · 1 observations shown. Missing days are unknown.

View observed values
Date / source periodValueObserved at
2026-10-2012026-10-20T21:06:21.3689445Z

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:18 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:d5b816988e526a0709bd1991b3d1fb1d64bce195126bb417eba2f6f360ea0b30

Other collected versions

1.2.114 Dec1.2.014 Dec1.1.112 Dec1.1.012 Dec1.0.310 Dec1.0.210 DecView all collected versions →