updated cache to support setting ttl

This commit is contained in:
Boki 2025-06-10 19:24:32 -04:00
parent a49cb9ae98
commit d6ca9fe93c
3 changed files with 336 additions and 7 deletions

View file

@ -144,17 +144,92 @@ export class RedisCache implements CacheProvider {
);
}
async set<T>(key: string, value: T, ttl?: number): Promise<void> {
await this.safeExecute(
async set<T>(key: string, value: T, options?: number | {
ttl?: number;
preserveTTL?: boolean;
onlyIfExists?: boolean;
onlyIfNotExists?: boolean;
getOldValue?: boolean;
}): Promise<T | null> {
return this.safeExecute(
async () => {
const fullKey = this.getKey(key);
const expiry = ttl ?? this.defaultTTL;
const serialized = typeof value === 'string' ? value : JSON.stringify(value);
await this.redis.setex(fullKey, expiry, serialized);
this.logger.debug('Cache set', { key, ttl: expiry });
// Handle backward compatibility - if options is a number, treat as TTL
const config = typeof options === 'number' ? { ttl: options } : (options || {});
let oldValue: T | null = null;
// Get old value if requested
if (config.getOldValue) {
const existingValue = await this.redis.get(fullKey);
if (existingValue !== null) {
try {
oldValue = JSON.parse(existingValue) as T;
} catch {
oldValue = existingValue as unknown as T;
}
}
}
// Handle preserveTTL logic
if (config.preserveTTL) {
const currentTTL = await this.redis.ttl(fullKey);
if (currentTTL === -2) {
// Key doesn't exist
if (config.onlyIfExists) {
this.logger.debug('Set skipped - key does not exist and onlyIfExists is true', { key });
return oldValue;
}
// Set with default or specified TTL
const ttl = config.ttl ?? this.defaultTTL;
await this.redis.setex(fullKey, ttl, serialized);
this.logger.debug('Cache set with new TTL (key did not exist)', { key, ttl });
} else if (currentTTL === -1) {
// Key exists but has no expiry - preserve the no-expiry state
await this.redis.set(fullKey, serialized);
this.logger.debug('Cache set preserving no-expiry', { key });
} else {
// Key exists with TTL - preserve it
await this.redis.setex(fullKey, currentTTL, serialized);
this.logger.debug('Cache set preserving existing TTL', { key, ttl: currentTTL });
}
} else {
// Standard set logic with conditional operations
if (config.onlyIfExists && config.onlyIfNotExists) {
throw new Error('Cannot specify both onlyIfExists and onlyIfNotExists');
}
if (config.onlyIfExists) {
// Only set if key exists (XX flag)
const ttl = config.ttl ?? this.defaultTTL;
const result = await this.redis.set(fullKey, serialized, 'EX', ttl, 'XX');
if (result === null) {
this.logger.debug('Set skipped - key does not exist', { key });
return oldValue;
}
} else if (config.onlyIfNotExists) {
// Only set if key doesn't exist (NX flag)
const ttl = config.ttl ?? this.defaultTTL;
const result = await this.redis.set(fullKey, serialized, 'EX', ttl, 'NX');
if (result === null) {
this.logger.debug('Set skipped - key already exists', { key });
return oldValue;
}
} else {
// Standard set
const ttl = config.ttl ?? this.defaultTTL;
await this.redis.setex(fullKey, ttl, serialized);
}
this.logger.debug('Cache set', { key, ttl: config.ttl ?? this.defaultTTL });
}
return oldValue;
},
undefined,
null,
'set'
);
}
@ -251,4 +326,79 @@ export class RedisCache implements CacheProvider {
return ready;
}
// Enhanced convenience methods
async update<T>(key: string, value: T): Promise<T | null> {
return this.set(key, value, { preserveTTL: true, getOldValue: true });
}
async setIfExists<T>(key: string, value: T, ttl?: number): Promise<boolean> {
const result = await this.set(key, value, { ttl, onlyIfExists: true });
return result !== null || await this.exists(key);
}
async setIfNotExists<T>(key: string, value: T, ttl?: number): Promise<boolean> {
const oldValue = await this.set(key, value, { ttl, onlyIfNotExists: true, getOldValue: true });
return oldValue === null; // Returns true if key didn't exist before
}
async replace<T>(key: string, value: T, ttl?: number): Promise<T | null> {
return this.set(key, value, { ttl, onlyIfExists: true, getOldValue: true });
}
// Atomic update with transformation
async updateField<T>(key: string, updater: (current: T | null) => T, ttl?: number): Promise<T | null> {
return this.safeExecute(
async () => {
const fullKey = this.getKey(key);
// Use Lua script for atomic read-modify-write
const luaScript = `
local key = KEYS[1]
-- Get current value and TTL
local current_value = redis.call('GET', key)
local current_ttl = redis.call('TTL', key)
-- Return current value for processing
return {current_value, current_ttl}
`;
const [currentValue, currentTTL] = await this.redis.eval(
luaScript,
1,
fullKey
) as [string | null, number];
// Parse current value
let parsed: T | null = null;
if (currentValue !== null) {
try {
parsed = JSON.parse(currentValue) as T;
} catch {
parsed = currentValue as unknown as T;
}
}
// Apply updater function
const newValue = updater(parsed);
// Set the new value with appropriate TTL logic
if (ttl !== undefined) {
// Use specified TTL
await this.set(key, newValue, ttl);
} else if (currentTTL === -2) {
// Key didn't exist, use default TTL
await this.set(key, newValue);
} else {
// Preserve existing TTL
await this.set(key, newValue, { preserveTTL: true });
}
return parsed;
},
null,
'updateField'
);
}
}

View file

@ -1,6 +1,12 @@
export interface CacheProvider {
get<T>(key: string): Promise<T | null>;
set<T>(key: string, value: T, ttl?: number): Promise<void>;
set<T>(key: string, value: T, options?: number | {
ttl?: number;
preserveTTL?: boolean;
onlyIfExists?: boolean;
onlyIfNotExists?: boolean;
getOldValue?: boolean;
}): Promise<T | null>;
del(key: string): Promise<void>;
exists(key: string): Promise<boolean>;
clear(): Promise<void>;
@ -18,6 +24,31 @@ export interface CacheProvider {
* Check if the cache is currently ready
*/
isReady(): boolean;
// Enhanced cache methods
/**
* Update value preserving existing TTL
*/
update?<T>(key: string, value: T): Promise<T | null>;
/**
* Set value only if key exists
*/
setIfExists?<T>(key: string, value: T, ttl?: number): Promise<boolean>;
/**
* Set value only if key doesn't exist
*/
setIfNotExists?<T>(key: string, value: T, ttl?: number): Promise<boolean>;
/**
* Replace existing key's value and TTL
*/
replace?<T>(key: string, value: T, ttl?: number): Promise<T | null>;
/**
* Atomically update field with transformation function
*/
updateField?<T>(key: string, updater: (current: T | null) => T, ttl?: number): Promise<T | null>;
}
export interface CacheOptions {