2q-cache 中文文档教程
2Q cache
用于实现 2Q 缓存算法的 Node.js 和浏览器的简单且可配置的缓存库 (http://www.vldb.org/conf/1994/P439.PDF) 该算法试图保留最常使用的元素,因此它比其他缓存(例如 LRU(最近最少使用)缓存)表现出更好的缓存命中率。
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
*/
带有选项的示例
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);
默认情况下
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
您还可以使用 2Q 缓存模块的 Typescript 版本:
import {Cache2Q, CacheException} from '2q-cache/ts/cache2Q';
let cache : Cache2Q = new Cache2Q(400);
API
code>set(key : any, value : any, ttl : number = 0 (optional))
< 有生命周期(可选) 如果 key 存在,它更新 value 和 ttl 参数 ttl 覆盖默认
ttl
的
设置代码>获取(密钥)=> value
通过提供的键返回值 不存在,则
null
返回
如果键 ) => boolean
则返回 true,否则返回 false
键
存在
如果 删除(键:任何)=> boolean
从缓存中删除对 (key, value)。 如果元素不存在,则返回 false。
mdel(keys : Array
) : Array 与 'del' 相同,但对于键数组
resetTtl(key : any, ttl : number)
通过提供的密钥重置(覆盖)元素的 ttl。
mresetTtl(keys : Array
与 'resetTtl' 相同,但对于键数组
setDefaultTtl(ttl : number)
设置默认 ttl。
setStringifyKeys(param : boolean)
覆盖字符串化键的默认设置。 如果 stringify 键为真,键将被 JSON.stringify 转换为字符串
clear()
清除缓存
alloc(size : number | Array
) 设置新的大小每个缓存部分。 参数与构造函数参数相同。 不允许使最大部分大小更小
allocUnsafe(size : number | Array
) 为每个缓存部分设置新的大小。 参数与构造函数参数相同。 允许使最大截面尺寸更小。 如果某些缓存部分的新最大大小小于当前大小,则将删除旧元素。
getSize()
返回对象如下:
{
buckets : {
in : {
currentSize : 10,
maxSize : 20
},
out : {
currentSize : 0,
maxSize : 60
},
},
main : {
currentSize : 0,
maxSize : 20
}
}
ROADMAP
Any ideas?
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?