cache.ts 891 B

1234567891011121314151617181920212223242526272829303132
  1. import type { Cache as ACDCache } from "async-cache-dedupe";
  2. import { createCache } from "async-cache-dedupe";
  3. import { stringify, parse } from "superjson";
  4. import { getRedis } from "./config/server";
  5. const transformer = {
  6. serialize: stringify,
  7. deserialize: parse,
  8. };
  9. /**
  10. * - API routes will be cached for 1 hour
  11. * - Cached function will be cached for 10 minutes,
  12. * If the function is called within 1 hour, it will
  13. * still be served from the cache, but also fetch the latest data
  14. */
  15. export const DEFAULT_NEXT_FETCH_TTL = 3600; // 1 hour
  16. export const DEFAULT_REDIS_CACHE_TTL = 60 * 10; // 10 minutes
  17. export const DEFAULT_REDIS_CACHE_STALE = 3600; // 1 hour
  18. export const redisCache: ACDCache = createCache({
  19. transformer,
  20. stale: DEFAULT_REDIS_CACHE_STALE,
  21. ttl: DEFAULT_REDIS_CACHE_TTL,
  22. storage: {
  23. type: "redis",
  24. options: {
  25. client: getRedis(),
  26. },
  27. },
  28. });