Skip to content

Commit

Permalink
Migrate from Redis to Valkey
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksson-daniel committed Feb 5, 2025
1 parent 1ea6b2b commit 61694be
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion nais/nais.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spec:
min: 2
max: 4
cpuThresholdPercentage: 80
redis:
valkey:
- instance: obo-cache
access: readwrite
liveness:
Expand Down
14 changes: 7 additions & 7 deletions server/src/auth/cache/cache-gauge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import { Counter, Gauge, Histogram } from 'prom-client';

const labelNames = ['hit'] as const;

export const redisCacheGauge = new Counter({
name: 'obo_redis_cache',
help: 'Number of requests to the Redis OBO cache. "hit" is the type of hit: "miss", "invalid", "hit" or "expired".',
export const persistentCacheGauge = new Counter({
name: 'obo_persistent_cache',
help: 'Number of requests to the persistent OBO cache. "hit" is the type of hit: "miss", "invalid", "hit" or "expired".',
labelNames,
registers: [proxyRegister],
});

export const redisCacheSizeGauge = new Gauge({
name: 'obo_redis_cache_size',
help: 'Number of OBO tokens in the Redis cache.',
export const persistentCacheSizeGauge = new Gauge({
name: 'obo_persistent_cache_size',
help: 'Number of OBO tokens in the persistent cache.',
registers: [proxyRegister],
});

export const memoryCacheGauge = new Counter({
name: 'obo_cache',
help: 'Number of requests to the OBO cache. "hit" is the type of hit: "miss", "redis", "hit", or "expired".',
help: 'Number of requests to the OBO cache. "hit" is the type of hit: "miss", "persistent", "hit", or "expired".',
labelNames,
registers: [proxyRegister],
});
Expand Down
6 changes: 3 additions & 3 deletions server/src/auth/cache/cache.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { createHash } from 'node:crypto';
import { OboMemoryCache } from '@app/auth/cache/memory-cache';
import { OboRedisCache } from '@app/auth/cache/redis-cache';
import { OboValkeyCache } from '@app/auth/cache/persistent-cache';
import { optionalEnvString } from '@app/config/env-var';

const REDIS_URI = optionalEnvString('REDIS_URI_OBO_CACHE');
const REDIS_USERNAME = optionalEnvString('REDIS_USERNAME_OBO_CACHE');
const REDIS_PASSWORD = optionalEnvString('REDIS_PASSWORD_OBO_CACHE');

class OboTieredCache {
#oboRedisCache: OboRedisCache;
#oboRedisCache: OboValkeyCache;
#oboMemoryCache: OboMemoryCache | null = null;
#isReady = false;

constructor(redisUri: string, redisUsername: string, redisPassword: string) {
this.#oboRedisCache = new OboRedisCache(redisUri, redisUsername, redisPassword);
this.#oboRedisCache = new OboValkeyCache(redisUri, redisUsername, redisPassword);
this.#init();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { memoryCacheGauge, redisCacheGauge, redisCacheSizeGauge } from '@app/auth/cache/cache-gauge';
import { memoryCacheGauge, persistentCacheGauge, persistentCacheSizeGauge } from '@app/auth/cache/cache-gauge';
import type { TokenMessage } from '@app/auth/cache/types';
import { getLogger } from '@app/logger';
import { type RedisClientType, createClient } from 'redis';
import { type RedisClientType as ValkeyClientType, createClient } from 'redis';

const log = getLogger('obo-redis-cache');
const log = getLogger('obo-persistent-cache');

type TokenListener = (message: TokenMessage) => void;

const TOKEN_CHANNEL = 'obo-token';

export class OboRedisCache {
#client: RedisClientType;
#subscribeClient: RedisClientType;
export class OboValkeyCache {
#client: ValkeyClientType;
#subscribeClient: ValkeyClientType;

#listeners: TokenListener[] = [];

Expand Down Expand Up @@ -51,7 +51,7 @@ export class OboRedisCache {

#refreshCacheSizeMetric = async () => {
const count = await this.#client.dbSize();
redisCacheSizeGauge.set(count);
persistentCacheSizeGauge.set(count);
};

public async getAll(): Promise<TokenMessage[]> {
Expand Down Expand Up @@ -96,26 +96,26 @@ export class OboRedisCache {
const [token, ttl] = await Promise.all([this.#client.get(key), this.#client.ttl(key)]);

if (token === null || ttl === -2) {
redisCacheGauge.inc({ hit: 'miss' });
persistentCacheGauge.inc({ hit: 'miss' });

return null;
}

if (ttl === -1) {
redisCacheGauge.inc({ hit: 'invalid' });
persistentCacheGauge.inc({ hit: 'invalid' });
this.#client.del(key);
this.#refreshCacheSizeMetric();

return null;
}

if (ttl === 0) {
redisCacheGauge.inc({ hit: 'expired' });
persistentCacheGauge.inc({ hit: 'expired' });

return null;
}

redisCacheGauge.inc({ hit: 'hit' });
persistentCacheGauge.inc({ hit: 'hit' });
memoryCacheGauge.inc({ hit: 'redis' });

return { token, expiresAt: now() + ttl };
Expand Down

0 comments on commit 61694be

Please sign in to comment.