1.0.0 • Published 7 years ago

@honeo/lru-cache v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

lru-cache

なにこれ

Map実装のかんたんLRU Cacheモジュール。

使い方

$ npm i @honeo/lru-cache
const LRUCache = require('@honeo/lru-cache');
const cache = new LRUCache();

cache.set('foo', 'bar');
cache.get('foo'); // "bar"

for(let [key, value] of cache){
	key+value; // "foobar"
}

API

Mapを継承している。

new LRUCache(option)

インスタンスを作る。

const cache = new LRUCache({
	capacity: 2501,
	expire: 1000*60*60 // 1h
});

option

keytypedefaultdescription
capacitynumberInfinityキャッシュの最大数
expirenumberInfinityキャッシュの期限ms

expireについて

Lazy Expiration実装のため、すぐにGCさせたい場合は明示的に#delete()などで要素を削除する必要がある。また#sizeは期限切れの要素を含んだ数を返す。

LRUCache#set(key, value , expire)

引数1をkeyとして引数2のvalueをキャッシュする。
自身を返す。

cache.set('hoge', 'hogehoge');

// expire: 1m
cache.set('fuga', 'fugafuga', 1000*60);

LRUCache#capacity

インスタンスの最大キャッシュ数を取得・設定する。

const number = cache.capacity;

// Max: 1000
cache.capacity = 1000;

LRUCache#expire

インスタンスの標準キャッシュ期限を取得・設定する。

const number = cache.expire;

// expire: 1h
cache.expire = 1000*60*60;