# 2q-cache

> Imlementation of 2Q Cache Algorithm

Latest version **1.2.1** (published 2019-12-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install 2q-cache
pnpm add 2q-cache
yarn add 2q-cache
bun add 2q-cache
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2019-12-14 |
| First published | 2019-12-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 79 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | ArtemBorisov |
| Maintainers | artem_borisov |
| Keywords | cache, 2Q, 2Q cache, 2Q-cache, 2Q Algorithm, lru, LRU, MRU, LFU, mru, lfu, 2Q Cache, Double Queues, Double Queue, node-cache, node cache, cache module |

## Links

- npm: https://www.npmjs.com/package/2q-cache
- Repository: https://github.com/artmBrisov/2q-cache
- Homepage: https://github.com/artmBrisov/2q-cache#readme
- Issues: https://github.com/artmBrisov/2q-cache/issues
- npm.io page: https://npm.io/package/2q-cache

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.2.1 (latest) — 2019-12-14
- 1.2.0 — 2019-12-14
- 1.1.1 — 2019-12-12
- 1.1.0 — 2019-12-12
- 1.0.4 — 2019-12-10
- 1.0.3 — 2019-12-10
- 1.0.2 — 2019-12-10
- 1.0.1 — 2019-12-10
- 1.0.0 — 2019-12-10

## README

# 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:

```javascript
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

```javascript
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:

```typescript
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:

```javascript
    {
        buckets : {
            in : {
                currentSize : 10,
                maxSize : 20
            },
            out : {
                currentSize : 0,
                maxSize : 60
            },
        },
        main : {
            currentSize : 0,
            maxSize : 20
        }
    }
```

## ROADMAP

* `Any ideas?`

---
_Source: https://npm.io/package/2q-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
