10599a4e1e
### What? I copied the contents of `cache-handler.js` and tried it, and got an error `"Socket already opened"`. ref: https://github.com/caching-tools/next-shared-cache/issues/284 It should be fixed so that it doesn't matter if it is copied. ### Why? Because `Socket already opened`, so a condition is needed before `await client.connect()`. ### How? Add environment variable(`process.env.REDIS_AVAILABLE`) check. ref: https://caching-tools.github.io/next-shared-cache/configuration/opt-out-cache-on-build Co-authored-by: akfm.sato <01047245@CF0286.local>
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
const { IncrementalCache } = require("@neshca/cache-handler");
|
|
const createRedisCache = require("@neshca/cache-handler/redis-stack").default;
|
|
const createLruCache = require("@neshca/cache-handler/local-lru").default;
|
|
const { createClient } = require("redis");
|
|
|
|
const client = createClient({
|
|
url: process.env.REDIS_URL ?? "redis://localhost:6379",
|
|
});
|
|
|
|
client.on("error", (error) => {
|
|
console.error("Redis error:", error.message);
|
|
});
|
|
|
|
IncrementalCache.onCreation(async () => {
|
|
// read more about TTL limitations https://caching-tools.github.io/next-shared-cache/configuration/ttl
|
|
function useTtl(maxAge) {
|
|
const evictionAge = maxAge * 1.5;
|
|
|
|
return evictionAge;
|
|
}
|
|
|
|
let redisCache;
|
|
|
|
if (process.env.REDIS_AVAILABLE) {
|
|
await client.connect();
|
|
|
|
redisCache = await createRedisCache({
|
|
client,
|
|
useTtl,
|
|
});
|
|
}
|
|
|
|
const localCache = createLruCache({
|
|
useTtl,
|
|
});
|
|
|
|
return {
|
|
cache: [redisCache, localCache],
|
|
// read more about useFileSystem limitations https://caching-tools.github.io/next-shared-cache/configuration/use-file-system
|
|
useFileSystem: false,
|
|
};
|
|
});
|
|
|
|
module.exports = IncrementalCache;
|