Skip to content

Commit 2dee97b

Browse files
authored
Merge pull request #1707 from navikt/dev-redis-common
Bruker en felles redis-instans for dev1 og dev2
2 parents d1f803d + a391e0f commit 2dee97b

File tree

6 files changed

+18
-45
lines changed

6 files changed

+18
-45
lines changed

.nais/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ spec:
3030
leaderElection: true
3131
redis:
3232
- instance: {{ redis.instance }}
33-
access: readwrite
33+
access: admin
3434
ingresses:
3535
{{#each ingresses as |url|}}
3636
- {{url}}

.nais/vars/vars-dev1.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ replicas:
2121
redis:
2222
plan: hobbyist
2323
project: nav-dev
24-
instance: pagecache-dev1
24+
instance: pagecache
2525
endpointId: f20f5b48-18f4-4e2a-8e5f-4ab3edb19733

.nais/vars/vars-dev2.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ replicas:
2121
redis:
2222
plan: hobbyist
2323
project: nav-dev
24-
instance: pagecache-dev2
24+
instance: pagecache
2525
endpointId: f20f5b48-18f4-4e2a-8e5f-4ab3edb19733

nodeenv.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ declare global {
1818
NAVNO_API_URL: string;
1919
ASSET_PREFIX: string;
2020
TELEMETRY_URL: string;
21+
NEXT_PHASE: string;
22+
REDIS_URI_PAGECACHE: string;
23+
REDIS_USERNAME_PAGECACHE: string;
24+
REDIS_PASSWORD_PAGECACHE: string;
2125
}
2226
}
2327

server/src/cache/page-cache-handler.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ const CACHE_TTL_24_HOURS_IN_MS = 3600 * 24 * 1000;
88

99
const TTL_RESOLUTION_MS = 60 * 1000;
1010

11-
export const redisCache =
12-
process.env.ENV === 'localhost' && !process.env.REDIS_URI_PAGECACHE
13-
? new RedisCacheDummy()
14-
: new RedisCache({ ttl: CACHE_TTL_24_HOURS_IN_MS });
11+
export const redisCache = !process.env.REDIS_URI_PAGECACHE
12+
? new RedisCacheDummy()
13+
: new RedisCache({ ttl: CACHE_TTL_24_HOURS_IN_MS });
1514

1615
const localCache = new LRUCache<string, CacheHandlerValue>({
1716
max: 2000,

server/src/cache/redis.ts

+8-38
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,11 @@ import { createClient, RedisClientOptions } from 'redis';
22
import { CacheHandlerValue } from 'next/dist/server/lib/incremental-cache';
33
import { logger } from 'srcCommon/logger';
44

5-
type AppEnv = typeof process.env.ENV;
6-
7-
const clientOptions: Record<AppEnv, RedisClientOptions> = {
8-
localhost: {
9-
url: process.env.REDIS_URI_PAGECACHE,
10-
username: process.env.REDIS_USERNAME_PAGECACHE,
11-
password: process.env.REDIS_PASSWORD_PAGECACHE,
12-
},
13-
prod: {
14-
url: process.env.REDIS_URI_PAGECACHE,
15-
username: process.env.REDIS_USERNAME_PAGECACHE,
16-
password: process.env.REDIS_PASSWORD_PAGECACHE,
17-
},
18-
dev1: {
19-
url: process.env.REDIS_URI_PAGECACHE_DEV1,
20-
username: process.env.REDIS_USERNAME_PAGECACHE_DEV1,
21-
password: process.env.REDIS_PASSWORD_PAGECACHE_DEV1,
22-
},
23-
dev2: {
24-
url: process.env.REDIS_URI_PAGECACHE_DEV2,
25-
username: process.env.REDIS_USERNAME_PAGECACHE_DEV2,
26-
password: process.env.REDIS_PASSWORD_PAGECACHE_DEV2,
27-
},
5+
const clientOptions: RedisClientOptions = {
6+
url: process.env.REDIS_URI_PAGECACHE,
7+
username: process.env.REDIS_USERNAME_PAGECACHE,
8+
password: process.env.REDIS_PASSWORD_PAGECACHE,
9+
socket: { keepAlive: 5000, connectTimeout: 10000 },
2810
} as const;
2911

3012
interface IRedisCache {
@@ -45,21 +27,9 @@ export class RedisCache implements IRedisCache {
4527
private keyPrefix: string = '';
4628

4729
constructor({ ttl }: ConstructorProps) {
48-
const env = process.env.ENV;
49-
const options = clientOptions[env];
50-
51-
if (!options) {
52-
throw Error(
53-
`Redis client options were not defined for the current app environment: ${env}`
54-
);
55-
}
56-
5730
this.ttl = ttl;
5831

59-
this.client = createClient({
60-
...options,
61-
socket: { keepAlive: 5000, connectTimeout: 10000 },
62-
})
32+
this.client = createClient(clientOptions)
6333
.on('connect', () => {
6434
logger.info('Redis client connected');
6535
})
@@ -76,11 +46,11 @@ export class RedisCache implements IRedisCache {
7646
logger.error(`Redis client error: ${err}`);
7747
});
7848

79-
logger.info(`Created redis client with url ${options.url}`);
49+
logger.info(`Created redis client with url ${clientOptions.url}`);
8050
}
8151

8252
public async init(keyPrefix: string) {
83-
this.keyPrefix = keyPrefix;
53+
this.keyPrefix = `${process.env.ENV}:${keyPrefix}`;
8454

8555
return this.client.connect().then(() => {
8656
logger.info(

0 commit comments

Comments
 (0)