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の第三引数を指定すれば有効期限も設定できる。


Related #js

AWS SESの受信メールを暗号化してs3に保存しLambdaで読み込む

jsの場合、複合処理を独自実装する必要がある

Next.jsのPreview Mode時はAnalyticsを無効化する

cookieの__next_preview_dataキーで判定

Next.js 11

手軽にアップグレードできた

VSCode Remoteで新規ファイルを作った際にTypescriptのPathが認識されない問題

「Ctrl + Shift + P」でコマンドパレットを開いて「Reload Window」を実行