encapsulate other caches

This commit is contained in:
Hazel K 2024-08-03 13:54:59 -04:00
parent bc236a4bd2
commit b1f1e3eb0e

View File

@ -7,23 +7,23 @@ import * as Redis from 'ioredis';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
export class RedisKVCache<T> { export class RedisKVCache<T> {
private redisClient: Redis.Redis; private readonly lifetime: number;
private name: string; private readonly memoryCache: MemoryKVCache<T>;
private lifetime: number; private readonly fetcher: (key: string) => Promise<T>;
private memoryCache: MemoryKVCache<T>; private readonly toRedisConverter: (value: T) => string;
private fetcher: (key: string) => Promise<T>; private readonly fromRedisConverter: (value: string) => T | undefined;
private toRedisConverter: (value: T) => string;
private fromRedisConverter: (value: string) => T | undefined;
constructor(redisClient: RedisKVCache<T>['redisClient'], name: RedisKVCache<T>['name'], opts: { constructor(
lifetime: RedisKVCache<T>['lifetime']; private redisClient: Redis.Redis,
memoryCacheLifetime: number; private name: string,
fetcher: RedisKVCache<T>['fetcher']; opts: {
toRedisConverter: RedisKVCache<T>['toRedisConverter']; lifetime: RedisKVCache<T>['lifetime'];
fromRedisConverter: RedisKVCache<T>['fromRedisConverter']; memoryCacheLifetime: number;
}) { fetcher: RedisKVCache<T>['fetcher'];
this.redisClient = redisClient; toRedisConverter: RedisKVCache<T>['toRedisConverter'];
this.name = name; fromRedisConverter: RedisKVCache<T>['fromRedisConverter'];
},
) {
this.lifetime = opts.lifetime; this.lifetime = opts.lifetime;
this.memoryCache = new MemoryKVCache(opts.memoryCacheLifetime); this.memoryCache = new MemoryKVCache(opts.memoryCacheLifetime);
this.fetcher = opts.fetcher; this.fetcher = opts.fetcher;
@ -101,23 +101,23 @@ export class RedisKVCache<T> {
} }
export class RedisSingleCache<T> { export class RedisSingleCache<T> {
private redisClient: Redis.Redis; private readonly lifetime: number;
private name: string; private readonly memoryCache: MemorySingleCache<T>;
private lifetime: number; private readonly fetcher: () => Promise<T>;
private memoryCache: MemorySingleCache<T>; private readonly toRedisConverter: (value: T) => string;
private fetcher: () => Promise<T>; private readonly fromRedisConverter: (value: string) => T | undefined;
private toRedisConverter: (value: T) => string;
private fromRedisConverter: (value: string) => T | undefined;
constructor(redisClient: RedisSingleCache<T>['redisClient'], name: RedisSingleCache<T>['name'], opts: { constructor(
lifetime: RedisSingleCache<T>['lifetime']; private redisClient: Redis.Redis,
memoryCacheLifetime: number; private name: string,
fetcher: RedisSingleCache<T>['fetcher']; opts: {
toRedisConverter: RedisSingleCache<T>['toRedisConverter']; lifetime: number;
fromRedisConverter: RedisSingleCache<T>['fromRedisConverter']; memoryCacheLifetime: number;
}) { fetcher: RedisSingleCache<T>['fetcher'];
this.redisClient = redisClient; toRedisConverter: RedisSingleCache<T>['toRedisConverter'];
this.name = name; fromRedisConverter: RedisSingleCache<T>['fromRedisConverter'];
},
) {
this.lifetime = opts.lifetime; this.lifetime = opts.lifetime;
this.memoryCache = new MemorySingleCache(opts.memoryCacheLifetime); this.memoryCache = new MemorySingleCache(opts.memoryCacheLifetime);
this.fetcher = opts.fetcher; this.fetcher = opts.fetcher;
@ -297,11 +297,10 @@ export class MemoryKVCache<T> {
export class MemorySingleCache<T> { export class MemorySingleCache<T> {
private cachedAt: number | null = null; private cachedAt: number | null = null;
private value: T | undefined; private value: T | undefined;
private lifetime: number;
constructor(lifetime: MemorySingleCache<never>['lifetime']) { constructor(
this.lifetime = lifetime; private lifetime: number,
} ) {}
@bindThis @bindThis
public set(value: T): void { public set(value: T): void {