node-cacheを使ってメモリにキャッシュする
redisやmemcacheを入れるまではいかないけど、シンプルなキャッシュの仕組みがほしい時にメモリにキャッシュするという手がある。
node-cacheを使うと手軽にオブジェクトキャッシュの仕組みが導入できた。
import NodeCache from 'node-cache';
const storageCache = new NodeCache();
export const getStorageCache = (cacheKey) => {
return storageCache.get(cacheKey);
};
export const setStorageCache = (cacheKey, content) => {
storageCache.set(cacheKey, content);
};
setの第三引数を指定すれば有効期限も設定できる。