Skip to content

Commit 2b79988

Browse files
committed
Setter ttl
1 parent 1b4afd5 commit 2b79988

File tree

7 files changed

+27
-28
lines changed

7 files changed

+27
-28
lines changed

.env.development

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ NODE_ENV=development
22
ENV=localhost
33
SERVICE_SECRET=dummyToken
44
DOCKER_HOST_ADDRESS=host.docker.internal
5-
PAGE_CACHE_DIR=./.next/page-cache
65
IMAGE_CACHE_DIR=./.next/image-cache
76
ADMIN_ORIGIN=http://localhost:8080
87
APP_ORIGIN=http://localhost:3000

.env.prod-local

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ NODE_ENV=production
22
ENV=localhost
33
SERVICE_SECRET=dummyToken
44
DOCKER_HOST_ADDRESS=host.docker.internal
5-
PAGE_CACHE_DIR=./.next/page-cache
65
IMAGE_CACHE_DIR=./.next/image-cache
76
ADMIN_ORIGIN=http://localhost:8080
87
APP_ORIGIN=http://localhost:3000

.github/workflows/build-image.yml

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ jobs:
9494
INNLOGGINGSSTATUS_URL=${{ inputs.INNLOGGINGSSTATUS_URL }}
9595
NAVNO_API_URL=${{ inputs.NAVNO_API_URL }}
9696
ASSET_PREFIX=https://cdn.nav.no/personbruker/nav-enonicxp-frontend
97-
PAGE_CACHE_DIR=/tmp/pages
9897
IMAGE_CACHE_DIR=/tmp/images
9998
EOF
10099
- name: Nextjs cache

server/cache/custom-cache-handler.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import FileSystemCache from 'next/dist/server/lib/incremental-cache/file-system-
22
import { LRUCache } from 'lru-cache';
33
import { CacheHandlerValue } from 'next/dist/server/lib/incremental-cache';
44
import { nodeFs } from 'next/dist/server/lib/node-fs-methods';
5-
import { redisClient } from './redis';
5+
import { RedisCache } from './redis';
66

77
type FileSystemCacheContext = ConstructorParameters<typeof FileSystemCache>[0];
88

99
const CACHE_TTL_24_HOURS = 3600 * 24 * 1000;
1010

11-
const isrMemoryCache = new LRUCache<string, CacheHandlerValue>({
11+
export const redisCache = new RedisCache({ ttl: CACHE_TTL_24_HOURS });
12+
13+
const localCache = new LRUCache<string, CacheHandlerValue>({
1214
max: 1000,
1315
ttl: CACHE_TTL_24_HOURS,
1416
ttlResolution: 1000,
@@ -33,9 +35,7 @@ export default class CustomFileSystemCache extends FileSystemCache {
3335
public async get(...args: Parameters<FileSystemCache['get']>) {
3436
const [key] = args;
3537

36-
console.log(`Getting from cache: ${key}`);
37-
38-
const foundData = await redisClient.get(key);
38+
const foundData = await redisCache.get(key);
3939

4040
if (!foundData?.value) {
4141
return null;
@@ -69,26 +69,26 @@ export default class CustomFileSystemCache extends FileSystemCache {
6969
public async set(...args: Parameters<FileSystemCache['set']>) {
7070
const [key, data] = args;
7171

72-
console.log(`Storing in cache: ${key}`);
73-
7472
const cacheItem: CacheHandlerValue = {
7573
value: data,
7674
lastModified: Date.now(),
7775
};
7876

79-
redisClient.set(key, cacheItem);
77+
redisCache
78+
.set(key, cacheItem)
79+
.then((result) => console.log(`Set key ${key} result`, result));
8080

8181
// isrMemoryCache.set(key, { value: data, lastModified: Date.now() });
8282
// return super.set(...args);
8383
}
8484

8585
public async clearGlobalCache() {
86-
isrMemoryCache.clear();
86+
localCache.clear();
8787
return true;
8888
}
8989

9090
public async deleteGlobalCacheEntry(path: string) {
9191
const pagePath = path === '/' ? '/index' : path;
92-
return isrMemoryCache.delete(pagePath);
92+
return localCache.delete(pagePath);
9393
}
9494
}

server/cache/redis.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ type AppEnv = typeof process.env.ENV;
55

66
const clientOptions: { [key in AppEnv]?: RedisClientOptions } = {
77
localhost: {
8-
url: process.env.REDIS_URI_PAGECACHE_DEV1,
9-
username: process.env.REDIS_USERNAME_PAGECACHE_DEV1,
10-
password: process.env.REDIS_PASSWORD_PAGECACHE_DEV1,
8+
url: process.env.REDIS_URI_PAGECACHE,
9+
username: process.env.REDIS_USERNAME_PAGECACHE,
10+
password: process.env.REDIS_PASSWORD_PAGECACHE,
1111
},
1212
dev1: {
1313
url: process.env.REDIS_URI_PAGECACHE_DEV1,
@@ -16,10 +16,15 @@ const clientOptions: { [key in AppEnv]?: RedisClientOptions } = {
1616
},
1717
};
1818

19-
class RedisClient {
19+
type ConstructorProps = {
20+
ttl: number;
21+
};
22+
23+
export class RedisCache {
2024
private readonly client: ReturnType<typeof createClient>;
25+
private readonly ttl: number;
2126

22-
constructor() {
27+
constructor({ ttl }: ConstructorProps) {
2328
const options = clientOptions[process.env.ENV];
2429

2530
if (!options) {
@@ -28,6 +33,8 @@ class RedisClient {
2833
);
2934
}
3035

36+
this.ttl = ttl;
37+
3138
this.client = createClient(options)
3239
.on('connect', () => {
3340
console.log('Redis client connected');
@@ -70,12 +77,8 @@ class RedisClient {
7077
}
7178

7279
public async set(key: string, data: CacheHandlerValue) {
73-
console.log(`Setting data for ${key}`);
74-
75-
const result = await this.client.set(key, JSON.stringify(data));
76-
77-
console.log(`Set key ${key} result`, result);
80+
return this.client.set(key, JSON.stringify(data), {
81+
EX: this.ttl,
82+
});
7883
}
7984
}
80-
81-
export const redisClient = new RedisClient();

server/server-setup.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { setCacheKey } from './req-handlers/set-cache-key';
88
import { handleInvalidateAllReq } from './req-handlers/invalidate-all';
99
import { handleGetPendingResponses } from './req-handlers/pending-responses';
1010
import { serverSetupDev } from './server-setup-dev';
11-
import { redisClient } from './cache/redis';
11+
import { redisCache } from './cache/custom-cache-handler';
1212

1313
// Set the no-cache header on json files from the incremental cache to ensure
1414
// data requested during client side navigation is always validated if cached
@@ -28,7 +28,7 @@ export const serverSetup = async (expressApp: Express, nextApp: NextServer) => {
2828
const nextServer = await getNextServer(nextApp);
2929
const currentBuildId = getNextBuildId(nextServer);
3030

31-
await redisClient.init();
31+
await redisCache.init();
3232

3333
console.log(`Current build id: ${currentBuildId}`);
3434

src/nodeenv.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ declare global {
1212
SERVICE_SECRET: string;
1313
REVALIDATOR_PROXY_ORIGIN: string;
1414
INNLOGGINGSSTATUS_URL: string;
15-
PAGE_CACHE_DIR: string;
1615
IMAGE_CACHE_DIR: string;
1716
FAILOVER_ORIGIN: string;
1817
IS_FAILOVER_INSTANCE: string;

0 commit comments

Comments
 (0)