Skip to content

Commit

Permalink
fix(redis): add ttl to all redis keys and by default set it to 8 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Nov 15, 2023
1 parent 7d85dea commit df0e4f4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ services:
- TVAL_GRAPHQL_HOST=${GRAPHQL_HOST:-core}
- TVAL_GRAPHQL_PORT=${GRAPHQL_PORT:-4000}
- TVAL_ARNS_ROOT_HOST=${ARNS_ROOT_HOST:-}
depends_on:
- core
- observer

core:
image: ghcr.io/ar-io/ar-io-core:latest
Expand Down Expand Up @@ -55,6 +58,7 @@ services:
- CONTRACT_ID=${CONTRACT_ID:-}
- CHAIN_CACHE_TYPE=${CHAIN_CACHE_TYPE:-redis}
- REDIS_CACHE_URL=${REDIS_CACHE_URL:-redis://redis:6379}
- REDIS_CACHE_TTL_SECONDS=${REDIS_CACHE_TTL_SECONDS:-r}
depends_on:
- redis

Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ export const REDIS_CACHE_URL = env.varOrDefault(
'REDIS_CACHE_URL',
'redis://localhost:6379',
);
export const REDIS_CACHE_TTL_SECONDS = +env.varOrDefault(
'REDIS_CACHE_TTL_SECONDS',
`${60 * 60 * 8}`, // 8 hours by default
);
1 change: 1 addition & 0 deletions src/lib/kvstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const getKvBufferStore = ({
case 'redis': {
return new RedisKvStore({
redisUrl: config.REDIS_CACHE_URL,
ttlSeconds: config.REDIS_CACHE_TTL_SECONDS,
log,
});
}
Expand Down
17 changes: 15 additions & 2 deletions src/store/redis-kv-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ import { KVBufferStore } from '../types.js';
export class RedisKvStore implements KVBufferStore {
private client: RedisClientType;
private log: winston.Logger;
private ttlSeconds: number;

constructor({ log, redisUrl }: { log: winston.Logger; redisUrl: string }) {
constructor({
log,
redisUrl,
ttlSeconds,
}: {
log: winston.Logger;
redisUrl: string;
ttlSeconds: number;
}) {
this.log = log.child({ class: this.constructor.name });
this.ttlSeconds = ttlSeconds;
this.client = createClient({
url: redisUrl,
});
Expand Down Expand Up @@ -58,6 +68,9 @@ export class RedisKvStore implements KVBufferStore {
}

async set(key: string, buffer: Buffer): Promise<void> {
await this.client.set(key, buffer);
// set the key with a TTL for every key
await this.client.set(key, buffer, {
EX: this.ttlSeconds,
});
}
}

0 comments on commit df0e4f4

Please sign in to comment.