@devbro/neko-cache 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/cache.mts","../src/providers/RedisCacheProvider.mts","../src/providers/FileCacheProvider.mts","../src/providers/MemoryCacheProvider.mts","../src/providers/MemcacheCacheProvider.mts","../src/providers/DisabledCacheProvider.mts"],"sourcesContent":["export * from './cache.mjs';\nexport * from './CacheProviderInterface.mjs';\nexport * from './providers/index.mjs';\n","import { JSONValue } from '@devbro/neko-helper';\nimport { CacheProviderInterface } from './CacheProviderInterface.mjs';\nimport { createHash } from 'crypto';\n\nexport type cacheOptions = {\n ttl?: number;\n};\n\nexport class Cache {\n constructor(private provider: CacheProviderInterface) {}\n\n async get<T>(key: JSONValue): Promise<T | undefined> {\n return this.provider.get(this.generateKey(key)) as Promise<T | undefined>;\n }\n\n async put(key: JSONValue, value: any, ttl?: number): Promise<void> {\n return this.provider.put(this.generateKey(key), value, ttl);\n }\n\n async delete(key: JSONValue): Promise<void> {\n return this.provider.delete(this.generateKey(key));\n }\n\n async has(key: JSONValue): Promise<boolean> {\n return this.provider.has(this.generateKey(key));\n }\n\n async remember<T>(\n key: JSONValue,\n callback: () => Promise<T>,\n options: cacheOptions = {}\n ): Promise<T> {\n options.ttl = options.ttl ?? 3600; // default TTL 1 hour\n\n const cached = await this.get<T>(key);\n if (cached) {\n return cached;\n }\n\n const result = await callback();\n await this.put(key, result, options.ttl);\n return result;\n }\n\n /**\n * Generates a cache key by serializing and concatenating the provided parts.\n * @param parts\n * @returns\n */\n generateKey(key: JSONValue): string {\n return createHash('md5').update(JSON.stringify(key)).digest('hex');\n }\n}\n","import { createClient, RedisClientOptions, RedisClientType } from 'redis';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\nimport { JSONValue, JSONObject } from '@devbro/neko-helper';\n\nexport class RedisCacheProvider implements CacheProviderInterface {\n private client: RedisClientType;\n private defaultTTL: number = 3600; // default TTL in seconds\n\n constructor(private config: RedisClientOptions) {\n this.client = this.createRedisClient();\n this.client.connect();\n }\n\n private createRedisClient(): any {\n let rc = createClient(this.config);\n return rc;\n }\n\n private async ensureConnection(): Promise<void> {\n if (!this.client.isOpen) {\n await this.client.connect();\n }\n }\n\n async get(key: string): Promise<JSONValue | JSONObject | undefined> {\n await this.ensureConnection();\n let rc = this.client.get(key);\n return rc.then((value) => {\n if (value === null || value === undefined) {\n return undefined;\n }\n return JSON.parse(value);\n });\n }\n\n async put(key: string, value: JSONValue | JSONObject, ttl?: number): Promise<void> {\n await this.ensureConnection();\n const serializedValue = JSON.stringify(value);\n ttl = ttl ?? this.defaultTTL;\n if (ttl && ttl > 0) {\n await this.client.setEx(key, ttl, serializedValue);\n } else {\n await this.client.set(key, serializedValue);\n }\n }\n\n async delete(key: string): Promise<void> {\n await this.ensureConnection();\n await this.client.del(key);\n }\n\n async has(key: string): Promise<boolean> {\n await this.ensureConnection();\n const result = await this.client.exists(key);\n return result === 1;\n }\n}\n","import * as fs from 'fs/promises';\nimport * as path from 'path';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\nimport { JSONObject, JSONValue } from '@devbro/neko-helper';\n\nexport interface FileCacheConfig {\n cacheDirectory?: string;\n defaultTTL?: number;\n cleanupInterval?: number;\n}\n\ninterface CacheItem {\n value: any;\n expiresAt?: number;\n createdAt: number;\n}\n\nexport class FileCacheProvider implements CacheProviderInterface {\n private config: FileCacheConfig = {\n cacheDirectory: path.join(process.cwd(), 'cache'),\n defaultTTL: 3600 * 1000,\n cleanupInterval: 300 * 1000,\n };\n\n private cleanupTimer?: NodeJS.Timeout;\n\n constructor(config: FileCacheConfig = {}) {\n this.config = { ...this.config, ...config };\n\n this.ensureCacheDirectory();\n this.startCleanupTimer();\n }\n\n private async ensureCacheDirectory(): Promise<void> {\n try {\n await fs.access(this.config.cacheDirectory!);\n } catch {\n await fs.mkdir(this.config.cacheDirectory!, { recursive: true });\n }\n }\n\n private startCleanupTimer(): void {\n if (this.config.cleanupInterval! > 0) {\n this.cleanupTimer = setInterval(() => {\n this.cleanupExpiredEntries().catch(console.error);\n }, this.config.cleanupInterval!);\n }\n }\n\n private stopCleanupTimer(): void {\n if (this.cleanupTimer) {\n clearInterval(this.cleanupTimer);\n this.cleanupTimer = undefined;\n }\n }\n\n private getFilePath(key: string): string {\n // Create a safe filename from the key\n const safeKey = key.replace(/[^a-z0-9]/gi, '_');\n return path.join(this.config.cacheDirectory!, `${safeKey}.json`);\n }\n\n private async cleanupExpiredEntries(): Promise<void> {\n try {\n const files = await fs.readdir(this.config.cacheDirectory!);\n const now = Date.now();\n\n for (const file of files) {\n if (file.endsWith('.json')) {\n const filePath = path.join(this.config.cacheDirectory!, file);\n try {\n const content = await fs.readFile(filePath, 'utf-8');\n const item: CacheItem = JSON.parse(content);\n\n if (item.expiresAt && item.expiresAt < now) {\n await fs.unlink(filePath);\n }\n } catch {\n // If file is corrupted, delete it\n await fs.unlink(filePath).catch(() => {});\n }\n }\n }\n } catch (error) {\n // Ignore cleanup errors\n }\n }\n\n async get(key: string): Promise<JSONObject | JSONValue | undefined> {\n const filePath = this.getFilePath(key);\n\n try {\n const content = await fs.readFile(filePath, 'utf-8');\n const item: CacheItem = JSON.parse(content);\n\n // Check if item has expired\n if (item.expiresAt && item.expiresAt < Date.now()) {\n await fs.unlink(filePath).catch(() => {});\n return undefined;\n }\n\n return item.value;\n } catch (error) {\n return undefined;\n }\n }\n\n async put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void> {\n const filePath = this.getFilePath(key);\n const now = Date.now();\n const effectiveTTL = ttl ?? this.config.defaultTTL!;\n\n const item: CacheItem = {\n value,\n createdAt: now,\n expiresAt: effectiveTTL > 0 ? now + effectiveTTL * 1000 : undefined,\n };\n\n this.ensureCacheDirectory();\n await fs.writeFile(filePath, JSON.stringify(item), 'utf-8');\n }\n\n async delete(key: string): Promise<void> {\n const filePath = this.getFilePath(key);\n\n try {\n await fs.unlink(filePath);\n } catch {\n // File doesn't exist, that's fine\n }\n }\n\n async has(key: string): Promise<boolean> {\n const value = await this.get(key);\n return value !== undefined;\n }\n}\n","import { JSONObject, JSONValue } from '@devbro/neko-helper';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\n\nexport interface MemoryCacheConfig {\n maxSize?: number;\n defaultTTL?: number;\n cleanupInterval?: number;\n}\n\ninterface CacheItem {\n value: any;\n expiresAt?: number;\n createdAt: number;\n lastAccessed: number;\n}\n\nexport class MemoryCacheProvider implements CacheProviderInterface {\n private cache = new Map<string, CacheItem>();\n private config: MemoryCacheConfig = {\n maxSize: 1000,\n defaultTTL: 3600,\n cleanupInterval: 600, // 10 minutes\n };\n\n private cleanupTimer?: NodeJS.Timeout;\n\n constructor(config: MemoryCacheConfig = {}) {\n this.config = { ...this.config, ...config };\n\n this.startCleanupTimer();\n }\n\n private startCleanupTimer(): void {\n if (this.config.cleanupInterval! > 0) {\n this.cleanupTimer = setInterval(() => {\n this.cleanupExpiredEntries();\n }, this.config.cleanupInterval! * 1000);\n }\n }\n\n private stopCleanupTimer(): void {\n if (this.cleanupTimer) {\n clearInterval(this.cleanupTimer);\n this.cleanupTimer = undefined;\n }\n }\n\n private cleanupExpiredEntries(): void {\n const now = Date.now();\n\n for (const [key, item] of this.cache.entries()) {\n if (item.expiresAt && item.expiresAt < now) {\n this.cache.delete(key);\n }\n }\n }\n\n private evictLRU(): void {\n if (this.cache.size <= this.config.maxSize!) {\n return;\n }\n\n // Find the least recently accessed item\n let oldestKey: string | null = null;\n let oldestTime = Date.now();\n\n for (const [key, item] of this.cache.entries()) {\n if (item.lastAccessed < oldestTime) {\n oldestTime = item.lastAccessed;\n oldestKey = key;\n }\n }\n\n if (oldestKey) {\n this.cache.delete(oldestKey);\n }\n }\n\n async get(key: string): Promise<JSONObject | JSONValue | undefined> {\n const item = this.cache.get(key);\n\n if (!item) {\n return undefined;\n }\n\n // Check if item has expired\n if (item.expiresAt && item.expiresAt < Date.now()) {\n this.cache.delete(key);\n return undefined;\n }\n\n // Update last accessed time for LRU\n item.lastAccessed = Date.now();\n\n return item.value;\n }\n\n async put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void> {\n const now = Date.now();\n const effectiveTTL = ttl ?? this.config.defaultTTL!;\n\n const item: CacheItem = {\n value,\n createdAt: now,\n lastAccessed: now,\n expiresAt: effectiveTTL > 0 ? now + effectiveTTL * 1000 : undefined,\n };\n\n this.cache.set(key, item);\n\n // Evict items if we exceed maxSize\n this.evictLRU();\n }\n\n async delete(key: string): Promise<void> {\n this.cache.delete(key);\n }\n\n async has(key: string): Promise<boolean> {\n const item = this.cache.get(key);\n\n if (!item) {\n return false;\n }\n\n // Check if item has expired\n if (item.expiresAt && item.expiresAt < Date.now()) {\n this.cache.delete(key);\n return false;\n }\n\n return true;\n }\n}\n","import { CacheProviderInterface } from \"@/CacheProviderInterface.mjs\";\nimport { JSONValue, JSONObject } from \"@devbro/neko-helper\";\nimport Memcached from \"memcached\";\n\nexport interface MemcachedConfig {\n location?: Memcached.Location;\n options?: Memcached.options;\n}\n\nexport class MemcacheCacheProvider implements CacheProviderInterface {\n private client: Memcached;\n private defaultTTL: number = 3600; // default TTL in seconds\n\n constructor(private config: MemcachedConfig = {}) {\n this.client = new Memcached(config.location || 'localhost:11211', config.options || {});\n }\n\n async get(key: string): Promise<JSONValue | JSONObject | undefined> {\n return new Promise((resolve, reject) => {\n this.client.get(key, (err: Error | undefined, data: any) => {\n if (err) {\n reject(err);\n return;\n }\n \n if (data === undefined || data === null) {\n resolve(undefined);\n return;\n }\n\n try {\n // Memcached automatically handles JSON serialization/deserialization\n // but we need to ensure we return the correct type\n resolve(typeof data === 'string' ? JSON.parse(data) : data);\n } catch (parseErr) {\n // If parsing fails, return the raw value\n resolve(data);\n }\n });\n });\n }\n\n async put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void> {\n return new Promise((resolve, reject) => {\n const serializedValue = JSON.stringify(value);\n const finalTTL = ttl ?? this.defaultTTL;\n\n this.client.set(key, serializedValue, finalTTL, (err: Error | undefined) => {\n if (err) {\n reject(err);\n return;\n }\n resolve();\n });\n });\n }\n\n async delete(key: string): Promise<void> {\n return new Promise((resolve, reject) => {\n this.client.del(key, (err: Error | undefined) => {\n if (err) {\n reject(err);\n return;\n }\n resolve();\n });\n });\n }\n\n async has(key: string): Promise<boolean> {\n return new Promise((resolve, reject) => {\n this.client.get(key, (err: Error | undefined, data: any) => {\n if (err) {\n reject(err);\n return;\n }\n resolve(data !== undefined && data !== null);\n });\n });\n }\n\n\n\n}","import { createClient, RedisClientOptions, RedisClientType } from 'redis';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\nimport { JSONValue, JSONObject } from '@devbro/neko-helper';\n\nexport class DisabledCacheProvider implements CacheProviderInterface {\n constructor(private config = {}) {}\n\n async get(key: string): Promise<JSONValue | JSONObject | undefined> {\n return undefined;\n }\n\n async put(key: string, value: JSONValue | JSONObject, ttl?: number): Promise<void> {}\n\n async delete(key: string): Promise<void> {}\n\n async has(key: string): Promise<boolean> {\n return false;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;ACEA,oBAA2B;AAMpB,IAAM,QAAN,MAAY;AAAA,EACjB,YAAoB,UAAkC;AAAlC;AAAA,EAAmC;AAAA,EATzD,OAQmB;AAAA;AAAA;AAAA,EAGjB,MAAM,IAAO,KAAwC;AACnD,WAAO,KAAK,SAAS,IAAI,KAAK,YAAY,GAAG,CAAC;AAAA,EAChD;AAAA,EAEA,MAAM,IAAI,KAAgB,OAAY,KAA6B;AACjE,WAAO,KAAK,SAAS,IAAI,KAAK,YAAY,GAAG,GAAG,OAAO,GAAG;AAAA,EAC5D;AAAA,EAEA,MAAM,OAAO,KAA+B;AAC1C,WAAO,KAAK,SAAS,OAAO,KAAK,YAAY,GAAG,CAAC;AAAA,EACnD;AAAA,EAEA,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MACV,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,OAAO,KAA4B;AACvC,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,WAAK,OAAO,IAAI,KAAK,CAAC,QAA2B;AAC/C,YAAI,KAAK;AACP,iBAAO,GAAG;AACV;AAAA,QACF;AACA,gBAAQ;AAAA,MACV,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,IAAI,KAA+B;AACvC,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,WAAK,OAAO,IAAI,KAAK,CAAC,KAAwB,SAAc;AAC1D,YAAI,KAAK;AACP,iBAAO,GAAG;AACV;AAAA,QACF;AACA,gBAAQ,SAAS,UAAa,SAAS,IAAI;AAAA,MAC7C,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAIF;;;AC/EO,IAAM,wBAAN,MAA8D;AAAA,EACnE,YAAoB,SAAS,CAAC,GAAG;AAAb;AAAA,EAAc;AAAA,EALpC,OAIqE;AAAA;AAAA;AAAA,EAGnE,MAAM,IAAI,KAA0D;AAClE,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,IAAI,KAAa,OAA+B,KAA6B;AAAA,EAAC;AAAA,EAEpF,MAAM,OAAO,KAA4B;AAAA,EAAC;AAAA,EAE1C,MAAM,IAAI,KAA+B;AACvC,WAAO;AAAA,EACT;AACF;","names":["Memcached"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/cache.mts","../src/providers/RedisCacheProvider.mts","../src/providers/FileCacheProvider.mts","../src/providers/MemoryCacheProvider.mts","../src/providers/MemcacheCacheProvider.mts","../src/providers/DisabledCacheProvider.mts"],"sourcesContent":["export * from './cache.mjs';\nexport * from './CacheProviderInterface.mjs';\nexport * from './providers/index.mjs';\n","import { JSONValue } from '@devbro/neko-helper';\nimport { CacheProviderInterface } from './CacheProviderInterface.mjs';\nimport { createHash } from 'crypto';\n\n/**\n * Options for cache operations.\n */\nexport type cacheOptions = {\n /** Time to live in seconds */\n ttl?: number;\n};\n\n/**\n * Cache class providing a unified interface for various cache providers.\n * Handles key generation, serialization, and common cache operations.\n */\nexport class Cache {\n /**\n * Creates a new Cache instance.\n * @param provider - The cache provider implementation to use\n */\n constructor(private provider: CacheProviderInterface) {}\n\n /**\n * Retrieves a value from the cache.\n * @template T - The expected type of the cached value\n * @param key - The cache key (can be string, number, object, or array)\n * @returns The cached value or undefined if not found or expired\n */\n async get<T>(key: JSONValue): Promise<T | undefined> {\n return this.provider.get(this.generateKey(key)) as Promise<T | undefined>;\n }\n\n /**\n * Stores a value in the cache.\n * @param key - The cache key (can be string, number, object, or array)\n * @param value - The value to cache\n * @param ttl - Time to live in seconds (optional)\n */\n async put(key: JSONValue, value: any, ttl?: number): Promise<void> {\n return this.provider.put(this.generateKey(key), value, ttl);\n }\n\n /**\n * Deletes a value from the cache.\n * @param key - The cache key to delete\n */\n async delete(key: JSONValue): Promise<void> {\n return this.provider.delete(this.generateKey(key));\n }\n\n /**\n * Checks if a key exists in the cache.\n * @param key - The cache key to check\n * @returns True if the key exists and has not expired, false otherwise\n */\n async has(key: JSONValue): Promise<boolean> {\n return this.provider.has(this.generateKey(key));\n }\n\n /**\n * Increments a numeric value in the cache atomically.\n * @param key - The cache key to increment\n * @param amount - The amount to increment by (default: 1)\n * @returns The new value after incrementing\n */\n async increment(key: JSONValue, amount: number = 1): Promise<number> {\n return this.provider.increment(this.generateKey(key), amount);\n }\n\n /**\n * Gets a value from cache or executes callback and caches the result.\n * This is useful for caching expensive operations.\n * @template T - The expected type of the value\n * @param key - The cache key\n * @param callback - Function to execute if cache miss occurs\n * @param options - Cache options including TTL (default: 3600 seconds)\n * @returns The cached value or the result of the callback\n */\n async remember<T>(\n key: JSONValue,\n callback: () => Promise<T>,\n options: cacheOptions = {}\n ): Promise<T> {\n options.ttl = options.ttl ?? 3600; // default TTL 1 hour\n\n const cached = await this.get<T>(key);\n if (cached) {\n return cached;\n }\n\n const result = await callback();\n await this.put(key, result, options.ttl);\n return result;\n }\n\n /**\n * Generates a cache key by serializing and hashing complex keys.\n * Simple string keys under 250 characters are returned as-is.\n * Complex keys (objects, arrays, long strings) are MD5 hashed.\n * @param key - The key to generate a cache key from\n * @returns A string suitable for use as a cache key\n */\n generateKey(key: JSONValue): string {\n if (typeof key === 'string' && key.length <= 250) {\n return key;\n }\n return createHash('md5').update(JSON.stringify(key)).digest('hex');\n }\n}\n","import { createClient, RedisClientOptions, RedisClientType } from 'redis';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\nimport { JSONValue, JSONObject } from '@devbro/neko-helper';\n\n/**\n * Redis-based cache provider that stores cache entries in a Redis server.\n * Provides distributed caching with automatic expiration support.\n */\nexport class RedisCacheProvider implements CacheProviderInterface {\n private client: RedisClientType;\n private defaultTTL: number = 3600; // default TTL in seconds\n\n /**\n * Creates a new RedisCacheProvider instance.\n * @param config - Redis client configuration options\n */\n constructor(private config: RedisClientOptions) {\n this.client = this.createRedisClient();\n this.client.connect();\n }\n\n /**\n * Creates a Redis client with the provided configuration.\n * @returns A Redis client instance\n */\n private createRedisClient(): any {\n let rc = createClient(this.config);\n return rc;\n }\n\n /**\n * Ensures the Redis client is connected before performing operations.\n */\n private async ensureConnection(): Promise<void> {\n if (!this.client.isOpen) {\n await this.client.connect();\n }\n }\n\n /**\n * Retrieves a value from the cache.\n * @param key - The cache key\n * @returns The cached value or undefined if not found\n */\n async get(key: string): Promise<JSONValue | JSONObject | undefined> {\n await this.ensureConnection();\n let rc = this.client.get(key);\n return rc.then((value) => {\n if (value === null || value === undefined) {\n return undefined;\n }\n return JSON.parse(value);\n });\n }\n\n /**\n * Stores a value in the cache.\n * @param key - The cache key\n * @param value - The value to cache\n * @param ttl - Time to live in seconds (optional)\n */\n async put(key: string, value: JSONValue | JSONObject, ttl?: number): Promise<void> {\n await this.ensureConnection();\n const serializedValue = JSON.stringify(value);\n ttl = ttl ?? this.defaultTTL;\n if (ttl && ttl > 0) {\n await this.client.setEx(key, ttl, serializedValue);\n } else {\n await this.client.set(key, serializedValue);\n }\n }\n\n /**\n * Deletes a value from the cache.\n * @param key - The cache key to delete\n */\n async delete(key: string): Promise<void> {\n await this.ensureConnection();\n await this.client.del(key);\n }\n\n /**\n * Checks if a key exists in the cache.\n * @param key - The cache key to check\n * @returns True if the key exists, false otherwise\n */\n async has(key: string): Promise<boolean> {\n await this.ensureConnection();\n const result = await this.client.exists(key);\n return result === 1;\n }\n\n /**\n * Increments a numeric value in the cache atomically using Redis INCRBY.\n * @param key - The cache key to increment\n * @param amount - The amount to increment by (default: 1)\n * @returns The new value after incrementing\n */\n async increment(key: string, amount: number = 1): Promise<number> {\n await this.ensureConnection();\n // Redis INCRBY is atomic\n return await this.client.incrBy(key, amount);\n }\n}\n","import * as fs from 'fs/promises';\nimport * as path from 'path';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\nimport { JSONObject, JSONValue } from '@devbro/neko-helper';\n\n/**\n * Configuration options for the file-based cache provider.\n */\nexport interface FileCacheConfig {\n /** Directory where cache files are stored (default: './cache') */\n cacheDirectory?: string;\n /** Default time to live in milliseconds (default: 3600000) */\n defaultTTL?: number;\n /** Interval in milliseconds for cleanup of expired entries (default: 300000) */\n cleanupInterval?: number;\n}\n\n/**\n * Represents a cached item stored in a file.\n */\ninterface CacheItem {\n /** The cached value */\n value: any;\n /** Timestamp when the item expires (milliseconds since epoch) */\n expiresAt?: number;\n /** Timestamp when the item was created (milliseconds since epoch) */\n createdAt: number;\n}\n\n/**\n * File-based cache provider that stores cache entries as JSON files.\n * Provides persistent caching with automatic cleanup of expired entries.\n */\nexport class FileCacheProvider implements CacheProviderInterface {\n private config: FileCacheConfig = {\n cacheDirectory: path.join(process.cwd(), 'cache'),\n defaultTTL: 3600 * 1000,\n cleanupInterval: 300 * 1000,\n };\n\n private cleanupTimer?: NodeJS.Timeout;\n\n /**\n * Creates a new FileCacheProvider instance.\n * @param config - Configuration options for the cache\n */\n constructor(config: FileCacheConfig = {}) {\n this.config = { ...this.config, ...config };\n\n this.ensureCacheDirectory();\n this.startCleanupTimer();\n }\n\n /**\n * Ensures the cache directory exists, creating it if necessary.\n */\n private async ensureCacheDirectory(): Promise<void> {\n try {\n await fs.access(this.config.cacheDirectory!);\n } catch {\n await fs.mkdir(this.config.cacheDirectory!, { recursive: true });\n }\n }\n\n /**\n * Starts the automatic cleanup timer for expired entries.\n */\n private startCleanupTimer(): void {\n if (this.config.cleanupInterval! > 0) {\n this.cleanupTimer = setInterval(() => {\n this.cleanupExpiredEntries().catch(console.error);\n }, this.config.cleanupInterval!);\n }\n }\n\n /**\n * Stops the automatic cleanup timer.\n */\n private stopCleanupTimer(): void {\n if (this.cleanupTimer) {\n clearInterval(this.cleanupTimer);\n this.cleanupTimer = undefined;\n }\n }\n\n /**\n * Generates a safe file path for the given cache key.\n * @param key - The cache key\n * @returns The full file path for the cache entry\n */\n private getFilePath(key: string): string {\n // Create a safe filename from the key\n const safeKey = key.replace(/[^a-z0-9]/gi, '_');\n return path.join(this.config.cacheDirectory!, `${safeKey}.json`);\n }\n\n /**\n * Removes all expired cache entries from the cache directory.\n */\n private async cleanupExpiredEntries(): Promise<void> {\n try {\n const files = await fs.readdir(this.config.cacheDirectory!);\n const now = Date.now();\n\n for (const file of files) {\n if (file.endsWith('.json')) {\n const filePath = path.join(this.config.cacheDirectory!, file);\n try {\n const content = await fs.readFile(filePath, 'utf-8');\n const item: CacheItem = JSON.parse(content);\n\n if (item.expiresAt && item.expiresAt < now) {\n await fs.unlink(filePath);\n }\n } catch {\n // If file is corrupted, delete it\n await fs.unlink(filePath).catch(() => {});\n }\n }\n }\n } catch (error) {\n // Ignore cleanup errors\n }\n }\n\n /**\n * Retrieves a value from the cache.\n * @param key - The cache key\n * @returns The cached value or undefined if not found or expired\n */\n async get(key: string): Promise<JSONObject | JSONValue | undefined> {\n const filePath = this.getFilePath(key);\n\n try {\n const content = await fs.readFile(filePath, 'utf-8');\n const item: CacheItem = JSON.parse(content);\n\n // Check if item has expired\n if (item.expiresAt && item.expiresAt < Date.now()) {\n await fs.unlink(filePath).catch(() => {});\n return undefined;\n }\n\n return item.value;\n } catch (error) {\n return undefined;\n }\n }\n\n /**\n * Stores a value in the cache.\n * @param key - The cache key\n * @param value - The value to cache\n * @param ttl - Time to live in seconds (optional)\n */\n async put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void> {\n const filePath = this.getFilePath(key);\n const now = Date.now();\n const effectiveTTL = ttl ?? this.config.defaultTTL!;\n\n const item: CacheItem = {\n value,\n createdAt: now,\n expiresAt: effectiveTTL > 0 ? now + effectiveTTL * 1000 : undefined,\n };\n\n this.ensureCacheDirectory();\n await fs.writeFile(filePath, JSON.stringify(item), 'utf-8');\n }\n\n /**\n * Deletes a value from the cache.\n * @param key - The cache key to delete\n */\n async delete(key: string): Promise<void> {\n const filePath = this.getFilePath(key);\n\n try {\n await fs.unlink(filePath);\n } catch {\n // File doesn't exist, that's fine\n }\n }\n\n /**\n * Checks if a key exists in the cache and has not expired.\n * @param key - The cache key to check\n * @returns True if the key exists and is not expired, false otherwise\n */\n async has(key: string): Promise<boolean> {\n const value = await this.get(key);\n return value !== undefined;\n }\n\n /**\n * Increments a numeric value in the cache atomically using file-based locking.\n * If the key doesn't exist or is expired, it starts from 0.\n * @param key - The cache key to increment\n * @param amount - The amount to increment by (default: 1)\n * @returns The new value after incrementing\n * @throws Error if lock cannot be acquired after maximum retries\n */\n async increment(key: string, amount: number = 1): Promise<number> {\n const filePath = this.getFilePath(key);\n\n // Use a lock file to ensure atomicity\n const lockPath = `${filePath}.lock`;\n\n // Simple file-based locking mechanism\n let lockAcquired = false;\n let retries = 0;\n const maxRetries = 50;\n\n while (!lockAcquired && retries < maxRetries) {\n try {\n // Try to create lock file exclusively\n await fs.writeFile(lockPath, '', { flag: 'wx' });\n lockAcquired = true;\n } catch {\n // Lock exists, wait a bit and retry\n await new Promise((resolve) => setTimeout(resolve, 10));\n retries++;\n }\n }\n\n if (!lockAcquired) {\n throw new Error('Failed to acquire lock for increment operation');\n }\n\n try {\n let currentValue = 0;\n let item: CacheItem | undefined;\n\n // Read current value\n try {\n const content = await fs.readFile(filePath, 'utf-8');\n const parsedItem = JSON.parse(content);\n\n // Check if item has expired\n if (parsedItem.expiresAt && parsedItem.expiresAt < Date.now()) {\n item = undefined;\n } else {\n item = parsedItem;\n currentValue = typeof parsedItem.value === 'number' ? parsedItem.value : 0;\n }\n } catch {\n // File doesn't exist or is corrupted, start from 0\n }\n\n // Calculate new value\n const newValue = currentValue + amount;\n\n // Write back with same TTL if it existed\n const now = Date.now();\n const newItem: CacheItem = {\n value: newValue,\n createdAt: item?.createdAt ?? now,\n expiresAt: item?.expiresAt,\n };\n\n await fs.writeFile(filePath, JSON.stringify(newItem), 'utf-8');\n\n return newValue;\n } finally {\n // Release lock\n try {\n await fs.unlink(lockPath);\n } catch {\n // Ignore errors when removing lock file\n }\n }\n }\n}\n","import { JSONObject, JSONValue } from '@devbro/neko-helper';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\n\n/**\n * Configuration options for the in-memory cache provider.\n */\nexport interface MemoryCacheConfig {\n /** Maximum number of items to store in cache (default: 1000) */\n maxSize?: number;\n /** Default time to live in seconds (default: 3600) */\n defaultTTL?: number;\n /** Interval in seconds to run cleanup of expired entries (default: 600) */\n cleanupInterval?: number;\n}\n\n/**\n * Represents a cached item with metadata.\n */\ninterface CacheItem {\n /** The cached value */\n value: any;\n /** Timestamp when the item expires (milliseconds since epoch) */\n expiresAt?: number;\n /** Timestamp when the item was created (milliseconds since epoch) */\n createdAt: number;\n /** Timestamp when the item was last accessed (milliseconds since epoch) */\n lastAccessed: number;\n}\n\n/**\n * In-memory cache provider with LRU eviction and automatic cleanup.\n * Stores cache entries in memory with support for TTL and size limits.\n */\nexport class MemoryCacheProvider implements CacheProviderInterface {\n private cache = new Map<string, CacheItem>();\n private config: MemoryCacheConfig = {\n maxSize: 1000,\n defaultTTL: 3600,\n cleanupInterval: 600, // 10 minutes\n };\n\n private cleanupTimer?: NodeJS.Timeout;\n\n /**\n * Creates a new MemoryCacheProvider instance.\n * @param config - Configuration options for the cache\n */\n constructor(config: MemoryCacheConfig = {}) {\n this.config = { ...this.config, ...config };\n\n this.startCleanupTimer();\n }\n\n /**\n * Starts the automatic cleanup timer for expired entries.\n */\n private startCleanupTimer(): void {\n if (this.config.cleanupInterval! > 0) {\n this.cleanupTimer = setInterval(() => {\n this.cleanupExpiredEntries();\n }, this.config.cleanupInterval! * 1000);\n }\n }\n\n /**\n * Stops the automatic cleanup timer.\n */\n private stopCleanupTimer(): void {\n if (this.cleanupTimer) {\n clearInterval(this.cleanupTimer);\n this.cleanupTimer = undefined;\n }\n }\n\n /**\n * Removes all expired entries from the cache.\n */\n private cleanupExpiredEntries(): void {\n const now = Date.now();\n\n for (const [key, item] of this.cache.entries()) {\n if (item.expiresAt && item.expiresAt < now) {\n this.cache.delete(key);\n }\n }\n }\n\n /**\n * Evicts the least recently used item if cache size exceeds maximum.\n */\n private evictLRU(): void {\n if (this.cache.size <= this.config.maxSize!) {\n return;\n }\n\n // Find the least recently accessed item\n let oldestKey: string | null = null;\n let oldestTime = Date.now();\n\n for (const [key, item] of this.cache.entries()) {\n if (item.lastAccessed < oldestTime) {\n oldestTime = item.lastAccessed;\n oldestKey = key;\n }\n }\n\n if (oldestKey) {\n this.cache.delete(oldestKey);\n }\n }\n\n /**\n * Retrieves a value from the cache.\n * @param key - The cache key\n * @returns The cached value or undefined if not found or expired\n */\n async get(key: string): Promise<JSONObject | JSONValue | undefined> {\n const item = this.cache.get(key);\n\n if (!item) {\n return undefined;\n }\n\n // Check if item has expired\n if (item.expiresAt && item.expiresAt < Date.now()) {\n this.cache.delete(key);\n return undefined;\n }\n\n // Update last accessed time for LRU\n item.lastAccessed = Date.now();\n\n return item.value;\n }\n\n /**\n * Stores a value in the cache.\n * @param key - The cache key\n * @param value - The value to cache\n * @param ttl - Time to live in seconds (optional)\n */\n async put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void> {\n const now = Date.now();\n const effectiveTTL = ttl ?? this.config.defaultTTL!;\n\n const item: CacheItem = {\n value,\n createdAt: now,\n lastAccessed: now,\n expiresAt: effectiveTTL > 0 ? now + effectiveTTL * 1000 : undefined,\n };\n\n this.cache.set(key, item);\n\n // Evict items if we exceed maxSize\n this.evictLRU();\n }\n\n /**\n * Deletes a value from the cache.\n * @param key - The cache key to delete\n */\n async delete(key: string): Promise<void> {\n this.cache.delete(key);\n }\n\n /**\n * Checks if a key exists in the cache and has not expired.\n * @param key - The cache key to check\n * @returns True if the key exists and is not expired, false otherwise\n */\n async has(key: string): Promise<boolean> {\n const item = this.cache.get(key);\n\n if (!item) {\n return false;\n }\n\n // Check if item has expired\n if (item.expiresAt && item.expiresAt < Date.now()) {\n this.cache.delete(key);\n return false;\n }\n\n return true;\n }\n\n /**\n * Increments a numeric value in the cache atomically.\n * If the key doesn't exist or is expired, it starts from 0.\n * @param key - The cache key to increment\n * @param amount - The amount to increment by (default: 1)\n * @returns The new value after incrementing\n */\n async increment(key: string, amount: number = 1): Promise<number> {\n const item = this.cache.get(key);\n const now = Date.now();\n\n let currentValue = 0;\n\n // Check if item exists and is not expired\n if (item) {\n if (item.expiresAt && item.expiresAt < now) {\n this.cache.delete(key);\n } else {\n // Get current value, ensure it's a number\n currentValue = typeof item.value === 'number' ? item.value : 0;\n }\n }\n\n // Calculate new value\n const newValue = currentValue + amount;\n\n // Store the new value with the same TTL if it existed\n const newItem: CacheItem = {\n value: newValue,\n createdAt: item?.createdAt ?? now,\n lastAccessed: now,\n expiresAt: item?.expiresAt,\n };\n\n this.cache.set(key, newItem);\n\n return newValue;\n }\n}\n","import { CacheProviderInterface } from '@/CacheProviderInterface.mjs';\nimport { JSONValue, JSONObject } from '@devbro/neko-helper';\nimport Memcached from 'memcached';\n\n/**\n * Configuration options for the Memcached cache provider.\n */\nexport interface MemcachedConfig {\n /** Memcached server location(s) */\n location?: Memcached.Location;\n /** Additional Memcached options */\n options?: Memcached.options;\n}\n\n/**\n * Memcached-based cache provider that stores cache entries in a Memcached server.\n * Provides distributed caching with automatic serialization and expiration.\n */\nexport class MemcacheCacheProvider implements CacheProviderInterface {\n private client: Memcached;\n private defaultTTL: number = 3600; // default TTL in seconds\n\n /**\n * Creates a new MemcacheCacheProvider instance.\n * @param config - Memcached configuration options\n */\n constructor(private config: MemcachedConfig = {}) {\n this.client = new Memcached(config.location || 'localhost:11211', config.options || {});\n }\n\n /**\n * Retrieves a value from the cache.\n * @param key - The cache key\n * @returns The cached value or undefined if not found\n */\n async get(key: string): Promise<JSONValue | JSONObject | undefined> {\n return new Promise((resolve, reject) => {\n this.client.get(key, (err: Error | undefined, data: any) => {\n if (err) {\n reject(err);\n return;\n }\n\n if (data === undefined || data === null) {\n resolve(undefined);\n return;\n }\n\n try {\n // Memcached automatically handles JSON serialization/deserialization\n // but we need to ensure we return the correct type\n resolve(typeof data === 'string' ? JSON.parse(data) : data);\n } catch (parseErr) {\n // If parsing fails, return the raw value\n resolve(data);\n }\n });\n });\n }\n\n /**\n * Stores a value in the cache.\n * @param key - The cache key\n * @param value - The value to cache\n * @param ttl - Time to live in seconds (optional)\n */\n async put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void> {\n return new Promise((resolve, reject) => {\n const serializedValue = JSON.stringify(value);\n const finalTTL = ttl ?? this.defaultTTL;\n\n this.client.set(key, serializedValue, finalTTL, (err: Error | undefined) => {\n if (err) {\n reject(err);\n return;\n }\n resolve();\n });\n });\n }\n\n /**\n * Deletes a value from the cache.\n * @param key - The cache key to delete\n */\n async delete(key: string): Promise<void> {\n return new Promise((resolve, reject) => {\n this.client.del(key, (err: Error | undefined) => {\n if (err) {\n reject(err);\n return;\n }\n resolve();\n });\n });\n }\n\n /**\n * Checks if a key exists in the cache.\n * @param key - The cache key to check\n * @returns True if the key exists, false otherwise\n */\n async has(key: string): Promise<boolean> {\n return new Promise((resolve, reject) => {\n this.client.get(key, (err: Error | undefined, data: any) => {\n if (err) {\n reject(err);\n return;\n }\n resolve(data !== undefined && data !== null);\n });\n });\n }\n\n /**\n * Increments a numeric value in the cache atomically using Memcached's native increment.\n * If the key doesn't exist, it is initialized with the increment amount.\n * @param key - The cache key to increment\n * @param amount - The amount to increment by (default: 1)\n * @returns The new value after incrementing\n */\n async increment(key: string, amount: number = 1): Promise<number> {\n return new Promise((resolve, reject) => {\n // Memcached incr are atomic operations\n this.client.incr(key, amount, (err: any, result: number | boolean) => {\n if (err) {\n reject(err);\n return;\n }\n\n // If key doesn't exist, result will be false\n if (result === false) {\n // Initialize the key with the amount value\n this.client.set(key, amount.toString(), this.defaultTTL, (setErr: Error | undefined) => {\n if (setErr) {\n reject(setErr);\n return;\n }\n resolve(amount);\n });\n } else {\n resolve(result as number);\n }\n });\n });\n }\n}\n","import { createClient, RedisClientOptions, RedisClientType } from 'redis';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\nimport { JSONValue, JSONObject } from '@devbro/neko-helper';\n\n/**\n * A cache provider that disables caching entirely.\n * All operations are no-ops, useful for testing or disabling cache in certain environments.\n */\nexport class DisabledCacheProvider implements CacheProviderInterface {\n /**\n * Creates a new DisabledCacheProvider instance.\n * @param config - Configuration object (currently unused)\n */\n constructor(private config = {}) {}\n\n /**\n * Always returns undefined (no caching).\n * @param key - The cache key\n * @returns Always undefined\n */\n async get(key: string): Promise<JSONValue | JSONObject | undefined> {\n return undefined;\n }\n\n /**\n * Does nothing (no caching).\n * @param key - The cache key\n * @param value - The value to cache\n * @param ttl - Time to live in seconds\n */\n async put(key: string, value: JSONValue | JSONObject, ttl?: number): Promise<void> {}\n\n /**\n * Does nothing (no caching).\n * @param key - The cache key to delete\n */\n async delete(key: string): Promise<void> {}\n\n /**\n * Always returns false (no caching).\n * @param key - The cache key to check\n * @returns Always false\n */\n async has(key: string): Promise<boolean> {\n return false;\n }\n\n /**\n * Returns the increment amount as if starting from 0 (no caching).\n * @param key - The cache key to increment\n * @param amount - The amount to increment by (default: 1)\n * @returns The increment amount\n */\n async increment(key: string, amount: number = 1): Promise<number> {\n // Disabled cache always returns the increment amount as if starting from 0\n return amount;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;ACEA,oBAA2B;AAcpB,IAAM,QAAN,MAAY;AAAA;AAAA;AAAA;AAAA;AAAA,EAKjB,YAAoB,UAAkC;AAAlC;AAAA,EAAmC;AAAA,EArBzD,OAgBmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAajB,MAAM,IAAO,KAAwC;AACnD,WAAO,KAAK,SAAS,IAAI,KAAK,YAAY,GAAG,CAAC;AAAA,EAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,IAAI,KAAgB,OAAY,KAA6B;AACjE,WAAO,KAAK,SAAS,IAAI,KAAK,YAAY,GAAG,GAAG,OAAO,GAAG;AAAA,EAC5D;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,OAAO,KAA+B;AAC1C,WAAO,KAAK,SAAS,OAAO,KAAK,YAAY,GAAG,CAAC;AAAA,EACnD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,IAAI,KAAkC;AAC1C,WAAO,KAAK,SAAS,IAAI,KAAK,YAAY,GAAG,CAAC;AAAA,EAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,UAAU,KAAgB,SAAiB,GAAoB;AACnE,WAAO,KAAK,SAAS,UAAU,KAAK,YAAY,GAAG,GAAG,MAAM;AAAA,EAC9D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,MAAM,SACJ,KACA,UACA,UAAwB,CAAC,GACb;AACZ,YAAQ,MAAM,QAAQ,OAAO;AAE7B,UAAM,SAAS,MAAM,KAAK,IAAO,GAAG;AACpC,QAAI,QAAQ;AACV,aAAO;AAAA,IACT;AAEA,UAAM,SAAS,MAAM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E;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYnE,MAAM,IAAI,KAA0D;AAClE,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,IAAI,KAAa,OAA+B,KAA6B;AAAA,EAAC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMpF,MAAM,OAAO,KAA4B;AAAA,EAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAO1C,MAAM,IAAI,KAA+B;AACvC,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,UAAU,KAAa,SAAiB,GAAoB;AAEhE,WAAO;AAAA,EACT;AACF;","names":["Memcached"]}
@@ -1,13 +1,48 @@
1
1
  import { CacheProviderInterface } from '../CacheProviderInterface.mjs';
2
2
  import { JSONValue, JSONObject } from '@devbro/neko-helper';
3
3
 
4
+ /**
5
+ * A cache provider that disables caching entirely.
6
+ * All operations are no-ops, useful for testing or disabling cache in certain environments.
7
+ */
4
8
  declare class DisabledCacheProvider implements CacheProviderInterface {
5
9
  private config;
10
+ /**
11
+ * Creates a new DisabledCacheProvider instance.
12
+ * @param config - Configuration object (currently unused)
13
+ */
6
14
  constructor(config?: {});
15
+ /**
16
+ * Always returns undefined (no caching).
17
+ * @param key - The cache key
18
+ * @returns Always undefined
19
+ */
7
20
  get(key: string): Promise<JSONValue | JSONObject | undefined>;
21
+ /**
22
+ * Does nothing (no caching).
23
+ * @param key - The cache key
24
+ * @param value - The value to cache
25
+ * @param ttl - Time to live in seconds
26
+ */
8
27
  put(key: string, value: JSONValue | JSONObject, ttl?: number): Promise<void>;
28
+ /**
29
+ * Does nothing (no caching).
30
+ * @param key - The cache key to delete
31
+ */
9
32
  delete(key: string): Promise<void>;
33
+ /**
34
+ * Always returns false (no caching).
35
+ * @param key - The cache key to check
36
+ * @returns Always false
37
+ */
10
38
  has(key: string): Promise<boolean>;
39
+ /**
40
+ * Returns the increment amount as if starting from 0 (no caching).
41
+ * @param key - The cache key to increment
42
+ * @param amount - The amount to increment by (default: 1)
43
+ * @returns The increment amount
44
+ */
45
+ increment(key: string, amount?: number): Promise<number>;
11
46
  }
12
47
 
13
48
  export { DisabledCacheProvider };
@@ -1,22 +1,55 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
  class DisabledCacheProvider {
4
+ /**
5
+ * Creates a new DisabledCacheProvider instance.
6
+ * @param config - Configuration object (currently unused)
7
+ */
4
8
  constructor(config = {}) {
5
9
  this.config = config;
6
10
  }
7
11
  static {
8
12
  __name(this, "DisabledCacheProvider");
9
13
  }
14
+ /**
15
+ * Always returns undefined (no caching).
16
+ * @param key - The cache key
17
+ * @returns Always undefined
18
+ */
10
19
  async get(key) {
11
20
  return void 0;
12
21
  }
22
+ /**
23
+ * Does nothing (no caching).
24
+ * @param key - The cache key
25
+ * @param value - The value to cache
26
+ * @param ttl - Time to live in seconds
27
+ */
13
28
  async put(key, value, ttl) {
14
29
  }
30
+ /**
31
+ * Does nothing (no caching).
32
+ * @param key - The cache key to delete
33
+ */
15
34
  async delete(key) {
16
35
  }
36
+ /**
37
+ * Always returns false (no caching).
38
+ * @param key - The cache key to check
39
+ * @returns Always false
40
+ */
17
41
  async has(key) {
18
42
  return false;
19
43
  }
44
+ /**
45
+ * Returns the increment amount as if starting from 0 (no caching).
46
+ * @param key - The cache key to increment
47
+ * @param amount - The amount to increment by (default: 1)
48
+ * @returns The increment amount
49
+ */
50
+ async increment(key, amount = 1) {
51
+ return amount;
52
+ }
20
53
  }
21
54
  export {
22
55
  DisabledCacheProvider
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/providers/DisabledCacheProvider.mts"],"sourcesContent":["import { createClient, RedisClientOptions, RedisClientType } from 'redis';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\nimport { JSONValue, JSONObject } from '@devbro/neko-helper';\n\nexport class DisabledCacheProvider implements CacheProviderInterface {\n constructor(private config = {}) {}\n\n async get(key: string): Promise<JSONValue | JSONObject | undefined> {\n return undefined;\n }\n\n async put(key: string, value: JSONValue | JSONObject, ttl?: number): Promise<void> {}\n\n async delete(key: string): Promise<void> {}\n\n async has(key: string): Promise<boolean> {\n return false;\n }\n}\n"],"mappings":";;AAIO,MAAM,sBAAwD;AAAA,EACnE,YAAoB,SAAS,CAAC,GAAG;AAAb;AAAA,EAAc;AAAA,EALpC,OAIqE;AAAA;AAAA;AAAA,EAGnE,MAAM,IAAI,KAA0D;AAClE,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,IAAI,KAAa,OAA+B,KAA6B;AAAA,EAAC;AAAA,EAEpF,MAAM,OAAO,KAA4B;AAAA,EAAC;AAAA,EAE1C,MAAM,IAAI,KAA+B;AACvC,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/providers/DisabledCacheProvider.mts"],"sourcesContent":["import { createClient, RedisClientOptions, RedisClientType } from 'redis';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\nimport { JSONValue, JSONObject } from '@devbro/neko-helper';\n\n/**\n * A cache provider that disables caching entirely.\n * All operations are no-ops, useful for testing or disabling cache in certain environments.\n */\nexport class DisabledCacheProvider implements CacheProviderInterface {\n /**\n * Creates a new DisabledCacheProvider instance.\n * @param config - Configuration object (currently unused)\n */\n constructor(private config = {}) {}\n\n /**\n * Always returns undefined (no caching).\n * @param key - The cache key\n * @returns Always undefined\n */\n async get(key: string): Promise<JSONValue | JSONObject | undefined> {\n return undefined;\n }\n\n /**\n * Does nothing (no caching).\n * @param key - The cache key\n * @param value - The value to cache\n * @param ttl - Time to live in seconds\n */\n async put(key: string, value: JSONValue | JSONObject, ttl?: number): Promise<void> {}\n\n /**\n * Does nothing (no caching).\n * @param key - The cache key to delete\n */\n async delete(key: string): Promise<void> {}\n\n /**\n * Always returns false (no caching).\n * @param key - The cache key to check\n * @returns Always false\n */\n async has(key: string): Promise<boolean> {\n return false;\n }\n\n /**\n * Returns the increment amount as if starting from 0 (no caching).\n * @param key - The cache key to increment\n * @param amount - The amount to increment by (default: 1)\n * @returns The increment amount\n */\n async increment(key: string, amount: number = 1): Promise<number> {\n // Disabled cache always returns the increment amount as if starting from 0\n return amount;\n }\n}\n"],"mappings":";;AAQO,MAAM,sBAAwD;AAAA;AAAA;AAAA;AAAA;AAAA,EAKnE,YAAoB,SAAS,CAAC,GAAG;AAAb;AAAA,EAAc;AAAA,EAbpC,OAQqE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYnE,MAAM,IAAI,KAA0D;AAClE,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,IAAI,KAAa,OAA+B,KAA6B;AAAA,EAAC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMpF,MAAM,OAAO,KAA4B;AAAA,EAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAO1C,MAAM,IAAI,KAA+B;AACvC,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,UAAU,KAAa,SAAiB,GAAoB;AAEhE,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -1,24 +1,84 @@
1
1
  import { CacheProviderInterface } from '../CacheProviderInterface.mjs';
2
2
  import { JSONObject, JSONValue } from '@devbro/neko-helper';
3
3
 
4
+ /**
5
+ * Configuration options for the file-based cache provider.
6
+ */
4
7
  interface FileCacheConfig {
8
+ /** Directory where cache files are stored (default: './cache') */
5
9
  cacheDirectory?: string;
10
+ /** Default time to live in milliseconds (default: 3600000) */
6
11
  defaultTTL?: number;
12
+ /** Interval in milliseconds for cleanup of expired entries (default: 300000) */
7
13
  cleanupInterval?: number;
8
14
  }
15
+ /**
16
+ * File-based cache provider that stores cache entries as JSON files.
17
+ * Provides persistent caching with automatic cleanup of expired entries.
18
+ */
9
19
  declare class FileCacheProvider implements CacheProviderInterface {
10
20
  private config;
11
21
  private cleanupTimer?;
22
+ /**
23
+ * Creates a new FileCacheProvider instance.
24
+ * @param config - Configuration options for the cache
25
+ */
12
26
  constructor(config?: FileCacheConfig);
27
+ /**
28
+ * Ensures the cache directory exists, creating it if necessary.
29
+ */
13
30
  private ensureCacheDirectory;
31
+ /**
32
+ * Starts the automatic cleanup timer for expired entries.
33
+ */
14
34
  private startCleanupTimer;
35
+ /**
36
+ * Stops the automatic cleanup timer.
37
+ */
15
38
  private stopCleanupTimer;
39
+ /**
40
+ * Generates a safe file path for the given cache key.
41
+ * @param key - The cache key
42
+ * @returns The full file path for the cache entry
43
+ */
16
44
  private getFilePath;
45
+ /**
46
+ * Removes all expired cache entries from the cache directory.
47
+ */
17
48
  private cleanupExpiredEntries;
49
+ /**
50
+ * Retrieves a value from the cache.
51
+ * @param key - The cache key
52
+ * @returns The cached value or undefined if not found or expired
53
+ */
18
54
  get(key: string): Promise<JSONObject | JSONValue | undefined>;
55
+ /**
56
+ * Stores a value in the cache.
57
+ * @param key - The cache key
58
+ * @param value - The value to cache
59
+ * @param ttl - Time to live in seconds (optional)
60
+ */
19
61
  put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void>;
62
+ /**
63
+ * Deletes a value from the cache.
64
+ * @param key - The cache key to delete
65
+ */
20
66
  delete(key: string): Promise<void>;
67
+ /**
68
+ * Checks if a key exists in the cache and has not expired.
69
+ * @param key - The cache key to check
70
+ * @returns True if the key exists and is not expired, false otherwise
71
+ */
21
72
  has(key: string): Promise<boolean>;
73
+ /**
74
+ * Increments a numeric value in the cache atomically using file-based locking.
75
+ * If the key doesn't exist or is expired, it starts from 0.
76
+ * @param key - The cache key to increment
77
+ * @param amount - The amount to increment by (default: 1)
78
+ * @returns The new value after incrementing
79
+ * @throws Error if lock cannot be acquired after maximum retries
80
+ */
81
+ increment(key: string, amount?: number): Promise<number>;
22
82
  }
23
83
 
24
84
  export { type FileCacheConfig, FileCacheProvider };
@@ -12,11 +12,18 @@ class FileCacheProvider {
12
12
  cleanupInterval: 300 * 1e3
13
13
  };
14
14
  cleanupTimer;
15
+ /**
16
+ * Creates a new FileCacheProvider instance.
17
+ * @param config - Configuration options for the cache
18
+ */
15
19
  constructor(config = {}) {
16
20
  this.config = { ...this.config, ...config };
17
21
  this.ensureCacheDirectory();
18
22
  this.startCleanupTimer();
19
23
  }
24
+ /**
25
+ * Ensures the cache directory exists, creating it if necessary.
26
+ */
20
27
  async ensureCacheDirectory() {
21
28
  try {
22
29
  await fs.access(this.config.cacheDirectory);
@@ -24,6 +31,9 @@ class FileCacheProvider {
24
31
  await fs.mkdir(this.config.cacheDirectory, { recursive: true });
25
32
  }
26
33
  }
34
+ /**
35
+ * Starts the automatic cleanup timer for expired entries.
36
+ */
27
37
  startCleanupTimer() {
28
38
  if (this.config.cleanupInterval > 0) {
29
39
  this.cleanupTimer = setInterval(() => {
@@ -31,16 +41,27 @@ class FileCacheProvider {
31
41
  }, this.config.cleanupInterval);
32
42
  }
33
43
  }
44
+ /**
45
+ * Stops the automatic cleanup timer.
46
+ */
34
47
  stopCleanupTimer() {
35
48
  if (this.cleanupTimer) {
36
49
  clearInterval(this.cleanupTimer);
37
50
  this.cleanupTimer = void 0;
38
51
  }
39
52
  }
53
+ /**
54
+ * Generates a safe file path for the given cache key.
55
+ * @param key - The cache key
56
+ * @returns The full file path for the cache entry
57
+ */
40
58
  getFilePath(key) {
41
59
  const safeKey = key.replace(/[^a-z0-9]/gi, "_");
42
60
  return path.join(this.config.cacheDirectory, `${safeKey}.json`);
43
61
  }
62
+ /**
63
+ * Removes all expired cache entries from the cache directory.
64
+ */
44
65
  async cleanupExpiredEntries() {
45
66
  try {
46
67
  const files = await fs.readdir(this.config.cacheDirectory);
@@ -63,6 +84,11 @@ class FileCacheProvider {
63
84
  } catch (error) {
64
85
  }
65
86
  }
87
+ /**
88
+ * Retrieves a value from the cache.
89
+ * @param key - The cache key
90
+ * @returns The cached value or undefined if not found or expired
91
+ */
66
92
  async get(key) {
67
93
  const filePath = this.getFilePath(key);
68
94
  try {
@@ -78,6 +104,12 @@ class FileCacheProvider {
78
104
  return void 0;
79
105
  }
80
106
  }
107
+ /**
108
+ * Stores a value in the cache.
109
+ * @param key - The cache key
110
+ * @param value - The value to cache
111
+ * @param ttl - Time to live in seconds (optional)
112
+ */
81
113
  async put(key, value, ttl) {
82
114
  const filePath = this.getFilePath(key);
83
115
  const now = Date.now();
@@ -90,6 +122,10 @@ class FileCacheProvider {
90
122
  this.ensureCacheDirectory();
91
123
  await fs.writeFile(filePath, JSON.stringify(item), "utf-8");
92
124
  }
125
+ /**
126
+ * Deletes a value from the cache.
127
+ * @param key - The cache key to delete
128
+ */
93
129
  async delete(key) {
94
130
  const filePath = this.getFilePath(key);
95
131
  try {
@@ -97,10 +133,71 @@ class FileCacheProvider {
97
133
  } catch {
98
134
  }
99
135
  }
136
+ /**
137
+ * Checks if a key exists in the cache and has not expired.
138
+ * @param key - The cache key to check
139
+ * @returns True if the key exists and is not expired, false otherwise
140
+ */
100
141
  async has(key) {
101
142
  const value = await this.get(key);
102
143
  return value !== void 0;
103
144
  }
145
+ /**
146
+ * Increments a numeric value in the cache atomically using file-based locking.
147
+ * If the key doesn't exist or is expired, it starts from 0.
148
+ * @param key - The cache key to increment
149
+ * @param amount - The amount to increment by (default: 1)
150
+ * @returns The new value after incrementing
151
+ * @throws Error if lock cannot be acquired after maximum retries
152
+ */
153
+ async increment(key, amount = 1) {
154
+ const filePath = this.getFilePath(key);
155
+ const lockPath = `${filePath}.lock`;
156
+ let lockAcquired = false;
157
+ let retries = 0;
158
+ const maxRetries = 50;
159
+ while (!lockAcquired && retries < maxRetries) {
160
+ try {
161
+ await fs.writeFile(lockPath, "", { flag: "wx" });
162
+ lockAcquired = true;
163
+ } catch {
164
+ await new Promise((resolve) => setTimeout(resolve, 10));
165
+ retries++;
166
+ }
167
+ }
168
+ if (!lockAcquired) {
169
+ throw new Error("Failed to acquire lock for increment operation");
170
+ }
171
+ try {
172
+ let currentValue = 0;
173
+ let item;
174
+ try {
175
+ const content = await fs.readFile(filePath, "utf-8");
176
+ const parsedItem = JSON.parse(content);
177
+ if (parsedItem.expiresAt && parsedItem.expiresAt < Date.now()) {
178
+ item = void 0;
179
+ } else {
180
+ item = parsedItem;
181
+ currentValue = typeof parsedItem.value === "number" ? parsedItem.value : 0;
182
+ }
183
+ } catch {
184
+ }
185
+ const newValue = currentValue + amount;
186
+ const now = Date.now();
187
+ const newItem = {
188
+ value: newValue,
189
+ createdAt: item?.createdAt ?? now,
190
+ expiresAt: item?.expiresAt
191
+ };
192
+ await fs.writeFile(filePath, JSON.stringify(newItem), "utf-8");
193
+ return newValue;
194
+ } finally {
195
+ try {
196
+ await fs.unlink(lockPath);
197
+ } catch {
198
+ }
199
+ }
200
+ }
104
201
  }
105
202
  export {
106
203
  FileCacheProvider
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/providers/FileCacheProvider.mts"],"sourcesContent":["import * as fs from 'fs/promises';\nimport * as path from 'path';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\nimport { JSONObject, JSONValue } from '@devbro/neko-helper';\n\nexport interface FileCacheConfig {\n cacheDirectory?: string;\n defaultTTL?: number;\n cleanupInterval?: number;\n}\n\ninterface CacheItem {\n value: any;\n expiresAt?: number;\n createdAt: number;\n}\n\nexport class FileCacheProvider implements CacheProviderInterface {\n private config: FileCacheConfig = {\n cacheDirectory: path.join(process.cwd(), 'cache'),\n defaultTTL: 3600 * 1000,\n cleanupInterval: 300 * 1000,\n };\n\n private cleanupTimer?: NodeJS.Timeout;\n\n constructor(config: FileCacheConfig = {}) {\n this.config = { ...this.config, ...config };\n\n this.ensureCacheDirectory();\n this.startCleanupTimer();\n }\n\n private async ensureCacheDirectory(): Promise<void> {\n try {\n await fs.access(this.config.cacheDirectory!);\n } catch {\n await fs.mkdir(this.config.cacheDirectory!, { recursive: true });\n }\n }\n\n private startCleanupTimer(): void {\n if (this.config.cleanupInterval! > 0) {\n this.cleanupTimer = setInterval(() => {\n this.cleanupExpiredEntries().catch(console.error);\n }, this.config.cleanupInterval!);\n }\n }\n\n private stopCleanupTimer(): void {\n if (this.cleanupTimer) {\n clearInterval(this.cleanupTimer);\n this.cleanupTimer = undefined;\n }\n }\n\n private getFilePath(key: string): string {\n // Create a safe filename from the key\n const safeKey = key.replace(/[^a-z0-9]/gi, '_');\n return path.join(this.config.cacheDirectory!, `${safeKey}.json`);\n }\n\n private async cleanupExpiredEntries(): Promise<void> {\n try {\n const files = await fs.readdir(this.config.cacheDirectory!);\n const now = Date.now();\n\n for (const file of files) {\n if (file.endsWith('.json')) {\n const filePath = path.join(this.config.cacheDirectory!, file);\n try {\n const content = await fs.readFile(filePath, 'utf-8');\n const item: CacheItem = JSON.parse(content);\n\n if (item.expiresAt && item.expiresAt < now) {\n await fs.unlink(filePath);\n }\n } catch {\n // If file is corrupted, delete it\n await fs.unlink(filePath).catch(() => {});\n }\n }\n }\n } catch (error) {\n // Ignore cleanup errors\n }\n }\n\n async get(key: string): Promise<JSONObject | JSONValue | undefined> {\n const filePath = this.getFilePath(key);\n\n try {\n const content = await fs.readFile(filePath, 'utf-8');\n const item: CacheItem = JSON.parse(content);\n\n // Check if item has expired\n if (item.expiresAt && item.expiresAt < Date.now()) {\n await fs.unlink(filePath).catch(() => {});\n return undefined;\n }\n\n return item.value;\n } catch (error) {\n return undefined;\n }\n }\n\n async put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void> {\n const filePath = this.getFilePath(key);\n const now = Date.now();\n const effectiveTTL = ttl ?? this.config.defaultTTL!;\n\n const item: CacheItem = {\n value,\n createdAt: now,\n expiresAt: effectiveTTL > 0 ? now + effectiveTTL * 1000 : undefined,\n };\n\n this.ensureCacheDirectory();\n await fs.writeFile(filePath, JSON.stringify(item), 'utf-8');\n }\n\n async delete(key: string): Promise<void> {\n const filePath = this.getFilePath(key);\n\n try {\n await fs.unlink(filePath);\n } catch {\n // File doesn't exist, that's fine\n }\n }\n\n async has(key: string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AA4B;AACvC,UAAM,WAAW,KAAK,YAAY,GAAG;AAErC,QAAI;AACF,YAAM,GAAG,OAAO,QAAQ;AAAA,IAC1B,QAAQ;AAAA,IAER;AAAA,EACF;AAAA,EAEA,MAAM,IAAI,KAA+B;AACvC,UAAM,QAAQ,MAAM,KAAK,IAAI,GAAG;AAChC,WAAO,UAAU;AAAA,EACnB;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/providers/FileCacheProvider.mts"],"sourcesContent":["import * as fs from 'fs/promises';\nimport * as path from 'path';\nimport { CacheProviderInterface } from '../CacheProviderInterface.mjs';\nimport { JSONObject, JSONValue } from '@devbro/neko-helper';\n\n/**\n * Configuration options for the file-based cache provider.\n */\nexport interface FileCacheConfig {\n /** Directory where cache files are stored (default: './cache') */\n cacheDirectory?: string;\n /** Default time to live in milliseconds (default: 3600000) */\n defaultTTL?: number;\n /** Interval in milliseconds for cleanup of expired entries (default: 300000) */\n cleanupInterval?: number;\n}\n\n/**\n * Represents a cached item stored in a file.\n */\ninterface CacheItem {\n /** The cached value */\n value: any;\n /** Timestamp when the item expires (milliseconds since epoch) */\n expiresAt?: number;\n /** Timestamp when the item was created (milliseconds since epoch) */\n createdAt: number;\n}\n\n/**\n * File-based cache provider that stores cache entries as JSON files.\n * Provides persistent caching with automatic cleanup of expired entries.\n */\nexport class FileCacheProvider implements CacheProviderInterface {\n private config: FileCacheConfig = {\n cacheDirectory: path.join(process.cwd(), 'cache'),\n defaultTTL: 3600 * 1000,\n cleanupInterval: 300 * 1000,\n };\n\n private cleanupTimer?: NodeJS.Timeout;\n\n /**\n * Creates a new FileCacheProvider instance.\n * @param config - Configuration options for the cache\n */\n constructor(config: FileCacheConfig = {}) {\n this.config = { ...this.config, ...config };\n\n this.ensureCacheDirectory();\n this.startCleanupTimer();\n }\n\n /**\n * Ensures the cache directory exists, creating it if necessary.\n */\n private async ensureCacheDirectory(): Promise<void> {\n try {\n await fs.access(this.config.cacheDirectory!);\n } catch {\n await fs.mkdir(this.config.cacheDirectory!, { recursive: true });\n }\n }\n\n /**\n * Starts the automatic cleanup timer for expired entries.\n */\n private startCleanupTimer(): void {\n if (this.config.cleanupInterval! > 0) {\n this.cleanupTimer = setInterval(() => {\n this.cleanupExpiredEntries().catch(console.error);\n }, this.config.cleanupInterval!);\n }\n }\n\n /**\n * Stops the automatic cleanup timer.\n */\n private stopCleanupTimer(): void {\n if (this.cleanupTimer) {\n clearInterval(this.cleanupTimer);\n this.cleanupTimer = undefined;\n }\n }\n\n /**\n * Generates a safe file path for the given cache key.\n * @param key - The cache key\n * @returns The full file path for the cache entry\n */\n private getFilePath(key: string): string {\n // Create a safe filename from the key\n const safeKey = key.replace(/[^a-z0-9]/gi, '_');\n return path.join(this.config.cacheDirectory!, `${safeKey}.json`);\n }\n\n /**\n * Removes all expired cache entries from the cache directory.\n */\n private async cleanupExpiredEntries(): Promise<void> {\n try {\n const files = await fs.readdir(this.config.cacheDirectory!);\n const now = Date.now();\n\n for (const file of files) {\n if (file.endsWith('.json')) {\n const filePath = path.join(this.config.cacheDirectory!, file);\n try {\n const content = await fs.readFile(filePath, 'utf-8');\n const item: CacheItem = JSON.parse(content);\n\n if (item.expiresAt && item.expiresAt < now) {\n await fs.unlink(filePath);\n }\n } catch {\n // If file is corrupted, delete it\n await fs.unlink(filePath).catch(() => {});\n }\n }\n }\n } catch (error) {\n // Ignore cleanup errors\n }\n }\n\n /**\n * Retrieves a value from the cache.\n * @param key - The cache key\n * @returns The cached value or undefined if not found or expired\n */\n async get(key: string): Promise<JSONObject | JSONValue | undefined> {\n const filePath = this.getFilePath(key);\n\n try {\n const content = await fs.readFile(filePath, 'utf-8');\n const item: CacheItem = JSON.parse(content);\n\n // Check if item has expired\n if (item.expiresAt && item.expiresAt < Date.now()) {\n await fs.unlink(filePath).catch(() => {});\n return undefined;\n }\n\n return item.value;\n } catch (error) {\n return undefined;\n }\n }\n\n /**\n * Stores a value in the cache.\n * @param key - The cache key\n * @param value - The value to cache\n * @param ttl - Time to live in seconds (optional)\n */\n async put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void> {\n const filePath = this.getFilePath(key);\n const now = Date.now();\n const effectiveTTL = ttl ?? this.config.defaultTTL!;\n\n const item: CacheItem = {\n value,\n createdAt: now,\n expiresAt: effectiveTTL > 0 ? now + effectiveTTL * 1000 : undefined,\n };\n\n this.ensureCacheDirectory();\n await fs.writeFile(filePath, JSON.stringify(item), 'utf-8');\n }\n\n /**\n * Deletes a value from the cache.\n * @param key - The cache key to delete\n */\n async delete(key: string): Promise<void> {\n const filePath = this.getFilePath(key);\n\n try {\n await fs.unlink(filePath);\n } catch {\n // File doesn't exist, that's fine\n }\n }\n\n /**\n * Checks if a key exists in the cache and has not expired.\n * @param key - The cache key to check\n * @returns True if the key exists and is not expired, false otherwise\n */\n async has(key: string): Promise<boolean> {\n const value = await this.get(key);\n return value !== undefined;\n }\n\n /**\n * Increments a numeric value in the cache atomically using file-based locking.\n * If the key doesn't exist or is expired, it starts from 0.\n * @param key - The cache key to increment\n * @param amount - The amount to increment by (default: 1)\n * @returns The new value after incrementing\n * @throws Error if lock cannot be acquired after maximum retries\n */\n async increment(key: string, amount: number = 1): Promise<number> {\n const filePath = this.getFilePath(key);\n\n // Use a lock file to ensure atomicity\n const lockPath = `${filePath}.lock`;\n\n // Simple file-based locking mechanism\n let lockAcquired = false;\n let retries = 0;\n const maxRetries = 50;\n\n while (!lockAcquired && retries < maxRetries) {\n try {\n // Try to create lock file exclusively\n await fs.writeFile(lockPath, '', { flag: 'wx' });\n lockAcquired = true;\n } catch {\n // Lock exists, wait a bit and retry\n await new Promise((resolve) => setTimeout(resolve, 10));\n retries++;\n }\n }\n\n if (!lockAcquired) {\n throw new Error('Failed to acquire lock for increment operation');\n }\n\n try {\n let currentValue = 0;\n let item: CacheItem | undefined;\n\n // Read current value\n try {\n const content = await fs.readFile(filePath, 'utf-8');\n const parsedItem = JSON.parse(content);\n\n // Check if item has expired\n if (parsedItem.expiresAt && parsedItem.expiresAt < Date.now()) {\n item = undefined;\n } else {\n item = parsedItem;\n currentValue = typeof parsedItem.value === 'number' ? parsedItem.value : 0;\n }\n } catch {\n // File doesn't exist or is corrupted, start from 0\n }\n\n // Calculate new value\n const newValue = currentValue + amount;\n\n // Write back with same TTL if it existed\n const now = Date.now();\n const newItem: CacheItem = {\n value: newValue,\n createdAt: item?.createdAt ?? now,\n expiresAt: item?.expiresAt,\n };\n\n await fs.writeFile(filePath, JSON.stringify(newItem), 'utf-8');\n\n return newValue;\n } finally {\n // Release lock\n try {\n await fs.unlink(lockPath);\n } catch {\n // Ignore errors when removing lock file\n }\n }\n }\n}\n"],"mappings":";;AAAA,YAAY,QAAQ;AACpB,YAAY,UAAU;AAgCf,MAAM,kBAAoD;AAAA,EAjCjE,OAiCiE;AAAA;AAAA;AAAA,EACvD,SAA0B;AAAA,IAChC,gBAAgB,KAAK,KAAK,QAAQ,IAAI,GAAG,OAAO;AAAA,IAChD,YAAY,OAAO;AAAA,IACnB,iBAAiB,MAAM;AAAA,EACzB;AAAA,EAEQ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMR,YAAY,SAA0B,CAAC,GAAG;AACxC,SAAK,SAAS,EAAE,GAAG,KAAK,QAAQ,GAAG,OAAO;AAE1C,SAAK,qBAAqB;AAC1B,SAAK,kBAAkB;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA,EAKA,MAAc,uBAAsC;AAClD,QAAI;AACF,YAAM,GAAG,OAAO,KAAK,OAAO,cAAe;AAAA,IAC7C,QAAQ;AACN,YAAM,GAAG,MAAM,KAAK,OAAO,gBAAiB,EAAE,WAAW,KAAK,CAAC;AAAA,IACjE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,oBAA0B;AAChC,QAAI,KAAK,OAAO,kBAAmB,GAAG;AACpC,WAAK,eAAe,YAAY,MAAM;AACpC,aAAK,sBAAsB,EAAE,MAAM,QAAQ,KAAK;AAAA,MAClD,GAAG,KAAK,OAAO,eAAgB;AAAA,IACjC;AAA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AE,CAAC;AACtD;AAAA,MACF;AAAA,IACF;AAEA,QAAI,CAAC,cAAc;AACjB,YAAM,IAAI,MAAM,gDAAgD;AAAA,IAClE;AAEA,QAAI;AACF,UAAI,eAAe;AACnB,UAAI;AAGJ,UAAI;AACF,cAAM,UAAU,MAAM,GAAG,SAAS,UAAU,OAAO;AACnD,cAAM,aAAa,KAAK,MAAM,OAAO;AAGrC,YAAI,WAAW,aAAa,WAAW,YAAY,KAAK,IAAI,GAAG;AAC7D,iBAAO;AAAA,QACT,OAAO;AACL,iBAAO;AACP,yBAAe,OAAO,WAAW,UAAU,WAAW,WAAW,QAAQ;AAAA,QAC3E;AAAA,MACF,QAAQ;AAAA,MAER;AAGA,YAAM,WAAW,eAAe;AAGhC,YAAM,MAAM,KAAK,IAAI;AACrB,YAAM,UAAqB;AAAA,QACzB,OAAO;AAAA,QACP,WAAW,MAAM,aAAa;AAAA,QAC9B,WAAW,MAAM;AAAA,MACnB;AAEA,YAAM,GAAG,UAAU,UAAU,KAAK,UAAU,OAAO,GAAG,OAAO;AAE7D,aAAO;AAAA,IACT,UAAE;AAEA,UAAI;AACF,cAAM,GAAG,OAAO,QAAQ;AAAA,MAC1B,QAAQ;AAAA,MAER;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -2,19 +2,60 @@ import { CacheProviderInterface } from '../CacheProviderInterface.mjs';
2
2
  import { JSONValue, JSONObject } from '@devbro/neko-helper';
3
3
  import Memcached from 'memcached';
4
4
 
5
+ /**
6
+ * Configuration options for the Memcached cache provider.
7
+ */
5
8
  interface MemcachedConfig {
9
+ /** Memcached server location(s) */
6
10
  location?: Memcached.Location;
11
+ /** Additional Memcached options */
7
12
  options?: Memcached.options;
8
13
  }
14
+ /**
15
+ * Memcached-based cache provider that stores cache entries in a Memcached server.
16
+ * Provides distributed caching with automatic serialization and expiration.
17
+ */
9
18
  declare class MemcacheCacheProvider implements CacheProviderInterface {
10
19
  private config;
11
20
  private client;
12
21
  private defaultTTL;
22
+ /**
23
+ * Creates a new MemcacheCacheProvider instance.
24
+ * @param config - Memcached configuration options
25
+ */
13
26
  constructor(config?: MemcachedConfig);
27
+ /**
28
+ * Retrieves a value from the cache.
29
+ * @param key - The cache key
30
+ * @returns The cached value or undefined if not found
31
+ */
14
32
  get(key: string): Promise<JSONValue | JSONObject | undefined>;
33
+ /**
34
+ * Stores a value in the cache.
35
+ * @param key - The cache key
36
+ * @param value - The value to cache
37
+ * @param ttl - Time to live in seconds (optional)
38
+ */
15
39
  put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void>;
40
+ /**
41
+ * Deletes a value from the cache.
42
+ * @param key - The cache key to delete
43
+ */
16
44
  delete(key: string): Promise<void>;
45
+ /**
46
+ * Checks if a key exists in the cache.
47
+ * @param key - The cache key to check
48
+ * @returns True if the key exists, false otherwise
49
+ */
17
50
  has(key: string): Promise<boolean>;
51
+ /**
52
+ * Increments a numeric value in the cache atomically using Memcached's native increment.
53
+ * If the key doesn't exist, it is initialized with the increment amount.
54
+ * @param key - The cache key to increment
55
+ * @param amount - The amount to increment by (default: 1)
56
+ * @returns The new value after incrementing
57
+ */
58
+ increment(key: string, amount?: number): Promise<number>;
18
59
  }
19
60
 
20
61
  export { MemcacheCacheProvider, type MemcachedConfig };