|
| 1 | +import { logger } from "./logger"; |
| 2 | + |
| 3 | +// ============================================================================= |
| 4 | +// Cache Store Interface |
| 5 | +// ============================================================================= |
| 6 | + |
| 7 | +/** |
| 8 | + * Interface for a hash-based cache store. |
| 9 | + * Implementations must support hash get/set and key expiration. |
| 10 | + */ |
| 11 | +export interface CacheStore { |
| 12 | + hgetall(key: string): Promise<Record<string, string>>; |
| 13 | + hset(key: string, values: Record<string, string>): Promise<void>; |
| 14 | + expire(key: string, seconds: number): Promise<void>; |
| 15 | +} |
| 16 | + |
| 17 | +// ============================================================================= |
| 18 | +// Redis Store |
| 19 | +// ============================================================================= |
| 20 | + |
| 21 | +class RedisStore implements CacheStore { |
| 22 | + private client: import("ioredis").default; |
| 23 | + |
| 24 | + constructor(url: string) { |
| 25 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 26 | + const Redis = require("ioredis") as typeof import("ioredis").default; |
| 27 | + this.client = new Redis(url); |
| 28 | + } |
| 29 | + |
| 30 | + async hgetall(key: string): Promise<Record<string, string>> { |
| 31 | + return this.client.hgetall(key); |
| 32 | + } |
| 33 | + |
| 34 | + async hset(key: string, values: Record<string, string>): Promise<void> { |
| 35 | + await this.client.hset(key, values); |
| 36 | + } |
| 37 | + |
| 38 | + async expire(key: string, seconds: number): Promise<void> { |
| 39 | + await this.client.expire(key, seconds); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// ============================================================================= |
| 44 | +// File Store |
| 45 | +// ============================================================================= |
| 46 | + |
| 47 | +import * as fs from "fs"; |
| 48 | +import * as path from "path"; |
| 49 | + |
| 50 | +class FileStore implements CacheStore { |
| 51 | + private dir: string; |
| 52 | + |
| 53 | + constructor(dir: string) { |
| 54 | + this.dir = dir; |
| 55 | + if (!fs.existsSync(dir)) { |
| 56 | + fs.mkdirSync(dir, { recursive: true }); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private filePath(key: string): string { |
| 61 | + // Encode key to a safe filename |
| 62 | + const safeKey = encodeURIComponent(key); |
| 63 | + return path.join(this.dir, `${safeKey}.json`); |
| 64 | + } |
| 65 | + |
| 66 | + private read(key: string): { data: Record<string, string>; expiresAt?: number } | null { |
| 67 | + const fp = this.filePath(key); |
| 68 | + if (!fs.existsSync(fp)) return null; |
| 69 | + |
| 70 | + try { |
| 71 | + const raw = JSON.parse(fs.readFileSync(fp, "utf-8")); |
| 72 | + |
| 73 | + // Check expiration |
| 74 | + if (raw.expiresAt && Date.now() > raw.expiresAt) { |
| 75 | + fs.unlinkSync(fp); |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + return raw; |
| 80 | + } catch { |
| 81 | + return null; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private write(key: string, entry: { data: Record<string, string>; expiresAt?: number }): void { |
| 86 | + const fp = this.filePath(key); |
| 87 | + fs.writeFileSync(fp, JSON.stringify(entry), "utf-8"); |
| 88 | + } |
| 89 | + |
| 90 | + async hgetall(key: string): Promise<Record<string, string>> { |
| 91 | + const entry = this.read(key); |
| 92 | + return entry?.data ?? {}; |
| 93 | + } |
| 94 | + |
| 95 | + async hset(key: string, values: Record<string, string>): Promise<void> { |
| 96 | + const existing = this.read(key); |
| 97 | + const merged = { ...(existing?.data ?? {}), ...values }; |
| 98 | + this.write(key, { data: merged, expiresAt: existing?.expiresAt }); |
| 99 | + } |
| 100 | + |
| 101 | + async expire(key: string, seconds: number): Promise<void> { |
| 102 | + const existing = this.read(key); |
| 103 | + if (!existing) return; |
| 104 | + this.write(key, { ...existing, expiresAt: Date.now() + seconds * 1000 }); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// ============================================================================= |
| 109 | +// Factory |
| 110 | +// ============================================================================= |
| 111 | + |
| 112 | +/** |
| 113 | + * Creates the cache store based on environment variables. |
| 114 | + * |
| 115 | + * CACHE_PROVIDER selects the backend: |
| 116 | + * - "redis" (default when REDIS_URL is set): uses Redis via ioredis |
| 117 | + * - "file": uses JSON files on disk at CACHE_DIR (defaults to .passmark-cache) |
| 118 | + * - "none": disables caching entirely |
| 119 | + * |
| 120 | + * For backwards compatibility, if CACHE_PROVIDER is not set: |
| 121 | + * - If REDIS_URL is set → uses Redis |
| 122 | + * - Otherwise → caching is disabled (null) |
| 123 | + */ |
| 124 | +function createCacheStore(): CacheStore | null { |
| 125 | + const provider = process.env.CACHE_PROVIDER?.toLowerCase(); |
| 126 | + |
| 127 | + if (provider === "none") { |
| 128 | + logger.warn("Cache provider set to 'none'. Caching is disabled."); |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + if (provider === "file") { |
| 133 | + const dir = process.env.CACHE_DIR || ".passmark-cache"; |
| 134 | + logger.info(`Using file-based cache at: ${dir}`); |
| 135 | + return new FileStore(dir); |
| 136 | + } |
| 137 | + |
| 138 | + if (provider === "redis" || (!provider && process.env.REDIS_URL)) { |
| 139 | + if (!process.env.REDIS_URL) { |
| 140 | + logger.warn("CACHE_PROVIDER is 'redis' but REDIS_URL is not set. Caching is disabled."); |
| 141 | + return null; |
| 142 | + } |
| 143 | + logger.info("Using Redis cache."); |
| 144 | + return new RedisStore(process.env.REDIS_URL); |
| 145 | + } |
| 146 | + |
| 147 | + if (provider) { |
| 148 | + logger.warn(`Unknown CACHE_PROVIDER '${provider}'. Caching is disabled.`); |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + // No CACHE_PROVIDER and no REDIS_URL |
| 153 | + logger.warn( |
| 154 | + "No cache provider configured. Set CACHE_PROVIDER=redis|file|none or REDIS_URL. " + |
| 155 | + "Step caching, global placeholders, and project data are disabled.", |
| 156 | + ); |
| 157 | + return null; |
| 158 | +} |
| 159 | + |
| 160 | +export const cache: CacheStore | null = createCacheStore(); |
0 commit comments