set cache ttl and default ttl

This commit is contained in:
miloschwartz
2026-03-20 17:52:07 -07:00
parent f643abf19a
commit 6c2c620c99
5 changed files with 35 additions and 14 deletions

View File

@@ -24,23 +24,31 @@ setInterval(() => {
*/ */
class AdaptiveCache { class AdaptiveCache {
private useRedis(): boolean { private useRedis(): boolean {
return redisManager.isRedisEnabled() && redisManager.getHealthStatus().isHealthy; return (
redisManager.isRedisEnabled() &&
redisManager.getHealthStatus().isHealthy
);
} }
/** /**
* Set a value in the cache * Set a value in the cache
* @param key - Cache key * @param key - Cache key
* @param value - Value to cache (will be JSON stringified for Redis) * @param value - Value to cache (will be JSON stringified for Redis)
* @param ttl - Time to live in seconds (0 = no expiration) * @param ttl - Time to live in seconds (0 = no expiration; omit = 3600s for Redis)
* @returns boolean indicating success * @returns boolean indicating success
*/ */
async set(key: string, value: any, ttl?: number): Promise<boolean> { async set(key: string, value: any, ttl?: number): Promise<boolean> {
const effectiveTtl = ttl === 0 ? undefined : ttl; const effectiveTtl = ttl === 0 ? undefined : ttl;
const redisTtl = ttl === 0 ? undefined : (ttl ?? 3600);
if (this.useRedis()) { if (this.useRedis()) {
try { try {
const serialized = JSON.stringify(value); const serialized = JSON.stringify(value);
const success = await redisManager.set(key, serialized, effectiveTtl); const success = await redisManager.set(
key,
serialized,
redisTtl
);
if (success) { if (success) {
logger.debug(`Set key in Redis: ${key}`); logger.debug(`Set key in Redis: ${key}`);
@@ -48,7 +56,9 @@ class AdaptiveCache {
} }
// Redis failed, fall through to local cache // Redis failed, fall through to local cache
logger.debug(`Redis set failed for key ${key}, falling back to local cache`); logger.debug(
`Redis set failed for key ${key}, falling back to local cache`
);
} catch (error) { } catch (error) {
logger.error(`Redis set error for key ${key}:`, error); logger.error(`Redis set error for key ${key}:`, error);
// Fall through to local cache // Fall through to local cache
@@ -120,9 +130,14 @@ class AdaptiveCache {
} }
// Some Redis deletes failed, fall through to local cache // Some Redis deletes failed, fall through to local cache
logger.debug(`Some Redis deletes failed, falling back to local cache`); logger.debug(
`Some Redis deletes failed, falling back to local cache`
);
} catch (error) { } catch (error) {
logger.error(`Redis del error for keys ${keys.join(", ")}:`, error); logger.error(
`Redis del error for keys ${keys.join(", ")}:`,
error
);
// Fall through to local cache // Fall through to local cache
deletedCount = 0; deletedCount = 0;
} }
@@ -195,7 +210,9 @@ class AdaptiveCache {
*/ */
async flushAll(): Promise<void> { async flushAll(): Promise<void> {
if (this.useRedis()) { if (this.useRedis()) {
logger.warn("Adaptive cache flushAll called - Redis flush not implemented, only local cache will be flushed"); logger.warn(
"Adaptive cache flushAll called - Redis flush not implemented, only local cache will be flushed"
);
} }
localCache.flushAll(); localCache.flushAll();
@@ -239,7 +256,9 @@ class AdaptiveCache {
getTtl(key: string): number { getTtl(key: string): number {
// Note: This only works for local cache, Redis TTL is not supported // Note: This only works for local cache, Redis TTL is not supported
if (this.useRedis()) { if (this.useRedis()) {
logger.warn(`getTtl called for key ${key} but Redis TTL lookup is not implemented`); logger.warn(
`getTtl called for key ${key} but Redis TTL lookup is not implemented`
);
} }
const ttl = localCache.getTtl(key); const ttl = localCache.getTtl(key);
@@ -255,7 +274,9 @@ class AdaptiveCache {
*/ */
keys(): string[] { keys(): string[] {
if (this.useRedis()) { if (this.useRedis()) {
logger.warn("keys() called but Redis keys are not included, only local cache keys returned"); logger.warn(
"keys() called but Redis keys are not included, only local cache keys returned"
);
} }
return localCache.keys(); return localCache.keys();
} }

View File

@@ -70,7 +70,7 @@ async function getLatestOlmVersion(): Promise<string | null> {
tags = tags.filter((version) => !version.name.includes("rc")); tags = tags.filter((version) => !version.name.includes("rc"));
const latestVersion = tags[0].name; const latestVersion = tags[0].name;
olmVersionCache.set("latestOlmVersion", latestVersion); olmVersionCache.set("latestOlmVersion", latestVersion, 3600);
return latestVersion; return latestVersion;
} catch (error: any) { } catch (error: any) {

View File

@@ -71,7 +71,7 @@ async function getLatestOlmVersion(): Promise<string | null> {
tags = tags.filter((version) => !version.name.includes("rc")); tags = tags.filter((version) => !version.name.includes("rc"));
const latestVersion = tags[0].name; const latestVersion = tags[0].name;
olmVersionCache.set("latestOlmVersion", latestVersion); olmVersionCache.set("latestOlmVersion", latestVersion, 3600);
return latestVersion; return latestVersion;
} catch (error: any) { } catch (error: any) {

View File

@@ -55,7 +55,7 @@ async function getLatestNewtVersion(): Promise<string | null> {
tags = tags.filter((version) => !version.name.includes("rc")); tags = tags.filter((version) => !version.name.includes("rc"));
const latestVersion = tags[0].name; const latestVersion = tags[0].name;
await cache.set("latestNewtVersion", latestVersion); await cache.set("latestNewtVersion", latestVersion, 3600);
return latestVersion; return latestVersion;
} catch (error: any) { } catch (error: any) {
@@ -180,7 +180,7 @@ registry.registerPath({
method: "get", method: "get",
path: "/org/{orgId}/sites", path: "/org/{orgId}/sites",
description: "List all sites in an organization", description: "List all sites in an organization",
tags: [OpenAPITags.Site], tags: [OpenAPITags.Org, OpenAPITags.Site],
request: { request: {
params: listSitesParamsSchema, params: listSitesParamsSchema,
query: listSitesSchema query: listSitesSchema

View File

@@ -201,7 +201,7 @@ export async function inviteUser(
); );
} }
await cache.set(email, attempts + 1); await cache.set("regenerateInvite:" + email, attempts + 1, 3600);
const inviteId = existingInvite[0].inviteId; // Retrieve the original inviteId const inviteId = existingInvite[0].inviteId; // Retrieve the original inviteId
const token = generateRandomString( const token = generateRandomString(