@devbro/neko-cache 0.1.4 → 0.1.6

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.
@@ -1,13 +1,47 @@
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
+ */
11
45
  increment(key: string, amount?: number): Promise<number>;
12
46
  }
13
47
 
@@ -1,22 +1,52 @@
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
+ */
20
50
  async increment(key, amount = 1) {
21
51
  return amount;
22
52
  }
@@ -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 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":";;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;AAAA,EAEA,MAAM,UAAU,KAAa,SAAiB,GAAoB;AAEhE,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/providers/DisabledCacheProvider.mts"],"sourcesContent":["import { 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":";;AAOO,MAAM,sBAAwD;AAAA;AAAA;AAAA;AAAA;AAAA,EAKnE,YAAoB,SAAS,CAAC,GAAG;AAAb;AAAA,EAAc;AAAA,EAZpC,OAOqE;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,83 @@
1
1
  import { CacheProviderInterface } from '../CacheProviderInterface.mjs';
2
2
  import { JSONObject, JSONValue } from '@devbro/neko-helper';
3
3
 
4
- interface FileCacheConfig {
4
+ /**
5
+ * Configuration options for the file-based cache provider.
6
+ */
7
+ type 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
+ */
22
81
  increment(key: string, amount?: number): Promise<number>;
23
82
  }
24
83
 
@@ -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,23 @@ 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
+ */
104
153
  async increment(key, amount = 1) {
105
154
  const filePath = this.getFilePath(key);
106
155
  const lockPath = `${filePath}.lock`;
@@ -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): Promise<boolean> {\n const value = await this.get(key);\n return value !== undefined;\n }\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;AAgBf,MAAM,kBAAoD;AAAA,EAjBjE,OAiBiE;AAAA;AAAA;AAAA,EACvD,SAA0B;AAAA,IAChC,gBAAgB,KAAK,KAAK,QAA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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 type 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;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,mBAAyB;AAC/B,QAAI,KAAK,cAAc;AACrB,oBAAc,KAAK,YAAY;AAC/B,WAAK,eAAe;AAAA,IACtB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,YAAY,KAAqB;AAEvC,UAAM,UAAU,IAAI,QAAQ,eAAe,GAAG;AAC9C,WAAO,KAAK,KAAK,KAAK,OAAO,gBAAiB,GAAG,OAAO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;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,59 @@ import { CacheProviderInterface } from '../CacheProviderInterface.mjs';
2
2
  import { JSONValue, JSONObject } from '@devbro/neko-helper';
3
3
  import Memcached from 'memcached';
4
4
 
5
- interface MemcachedConfig {
5
+ /**
6
+ * Configuration options for the Memcached cache provider.
7
+ */
8
+ type 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
+ */
18
58
  increment(key: string, amount?: number): Promise<number>;
19
59
  }
20
60
 
@@ -3,6 +3,10 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
3
3
  import Memcached from "memcached";
4
4
  class MemcacheCacheProvider {
5
5
  // default TTL in seconds
6
+ /**
7
+ * Creates a new MemcacheCacheProvider instance.
8
+ * @param config - Memcached configuration options
9
+ */
6
10
  constructor(config = {}) {
7
11
  this.config = config;
8
12
  this.client = new Memcached(config.location || "localhost:11211", config.options || {});
@@ -12,6 +16,11 @@ class MemcacheCacheProvider {
12
16
  }
13
17
  client;
14
18
  defaultTTL = 3600;
19
+ /**
20
+ * Retrieves a value from the cache.
21
+ * @param key - The cache key
22
+ * @returns The cached value or undefined if not found
23
+ */
15
24
  async get(key) {
16
25
  return new Promise((resolve, reject) => {
17
26
  this.client.get(key, (err, data) => {
@@ -31,6 +40,12 @@ class MemcacheCacheProvider {
31
40
  });
32
41
  });
33
42
  }
43
+ /**
44
+ * Stores a value in the cache.
45
+ * @param key - The cache key
46
+ * @param value - The value to cache
47
+ * @param ttl - Time to live in seconds (optional)
48
+ */
34
49
  async put(key, value, ttl) {
35
50
  return new Promise((resolve, reject) => {
36
51
  const serializedValue = JSON.stringify(value);
@@ -44,6 +59,10 @@ class MemcacheCacheProvider {
44
59
  });
45
60
  });
46
61
  }
62
+ /**
63
+ * Deletes a value from the cache.
64
+ * @param key - The cache key to delete
65
+ */
47
66
  async delete(key) {
48
67
  return new Promise((resolve, reject) => {
49
68
  this.client.del(key, (err) => {
@@ -55,6 +74,11 @@ class MemcacheCacheProvider {
55
74
  });
56
75
  });
57
76
  }
77
+ /**
78
+ * Checks if a key exists in the cache.
79
+ * @param key - The cache key to check
80
+ * @returns True if the key exists, false otherwise
81
+ */
58
82
  async has(key) {
59
83
  return new Promise((resolve, reject) => {
60
84
  this.client.get(key, (err, data) => {
@@ -66,6 +90,13 @@ class MemcacheCacheProvider {
66
90
  });
67
91
  });
68
92
  }
93
+ /**
94
+ * Increments a numeric value in the cache atomically using Memcached's native increment.
95
+ * If the key doesn't exist, it is initialized with the increment amount.
96
+ * @param key - The cache key to increment
97
+ * @param amount - The amount to increment by (default: 1)
98
+ * @returns The new value after incrementing
99
+ */
69
100
  async increment(key, amount = 1) {
70
101
  return new Promise((resolve, reject) => {
71
102
  this.client.incr(key, amount, (err, result) => {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/providers/MemcacheCacheProvider.mts"],"sourcesContent":["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 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"],"mappings":";;AAEA,OAAO,eAAe;AAOf,MAAM,sBAAwD;AAAA;AAAA,EAInE,YAAoB,SAA0B,CAAC,GAAG;AAA9B;AAClB,SAAK,SAAS,IAAI,UAAU,OAAO,YAAY,mBAAmB,OAAO,WAAW,CAAC,CAAC;AAAA,EACxF;AAAA,EAfF,OASqE;AAAA;AAAA;AAAA,EAC3D;AAAA,EACA,aAAqB;AAAA,EAM7B,MAAM,IAAI,KAA0D;AAClE,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,WAAK,OAAO,IAAI,KAAK,CAAC,KAAwB,SAAc;AAC1D,YAAI,KAAK;AACP,iBAA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A6B;AACpE,YAAI,KAAK;AACP,iBAAO,GAAG;AACV;AAAA,QACF;AAGA,YAAI,WAAW,OAAO;AAEpB,eAAK,OAAO,IAAI,KAAK,OAAO,SAAS,GAAG,KAAK,YAAY,CAAC,WAA8B;AACtF,gBAAI,QAAQ;AACV,qBAAO,MAAM;AACb;AAAA,YACF;AACA,oBAAQ,MAAM;AAAA,UAChB,CAAC;AAAA,QACH,OAAO;AACL,kBAAQ,MAAgB;AAAA,QAC1B;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/providers/MemcacheCacheProvider.mts"],"sourcesContent":["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 type 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"],"mappings":";;AAEA,OAAO,eAAe;AAgBf,MAAM,sBAAwD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQnE,YAAoB,SAA0B,CAAC,GAAG;AAA9B;AAClB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;AAAA;AAAA;AAAA;AAAA,EAOA,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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,UAAU,KAAa,SAAiB,GAAoB;AAChE,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AAEtC,WAAK,OAAO,KAAK,KAAK,QAAQ,CAAC,KAAU,WAA6B;AACpE,YAAI,KAAK;AACP,iBAAO,GAAG;AACV;AAAA,QACF;AAGA,YAAI,WAAW,OAAO;AAEpB,eAAK,OAAO,IAAI,KAAK,OAAO,SAAS,GAAG,KAAK,YAAY,CAAC,WAA8B;AACtF,gBAAI,QAAQ;AACV,qBAAO,MAAM;AACb;AAAA,YACF;AACA,oBAAQ,MAAM;AAAA,UAChB,CAAC;AAAA,QACH,OAAO;AACL,kBAAQ,MAAgB;AAAA,QAC1B;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":[]}
@@ -1,24 +1,77 @@
1
1
  import { JSONObject, JSONValue } from '@devbro/neko-helper';
2
2
  import { CacheProviderInterface } from '../CacheProviderInterface.mjs';
3
3
 
4
- interface MemoryCacheConfig {
4
+ /**
5
+ * Configuration options for the in-memory cache provider.
6
+ */
7
+ type MemoryCacheConfig = {
8
+ /** Maximum number of items to store in cache (default: 1000) */
5
9
  maxSize?: number;
10
+ /** Default time to live in seconds (default: 3600) */
6
11
  defaultTTL?: number;
12
+ /** Interval in seconds to run cleanup of expired entries (default: 600) */
7
13
  cleanupInterval?: number;
8
- }
14
+ };
15
+ /**
16
+ * In-memory cache provider with LRU eviction and automatic cleanup.
17
+ * Stores cache entries in memory with support for TTL and size limits.
18
+ */
9
19
  declare class MemoryCacheProvider implements CacheProviderInterface {
10
20
  private cache;
11
21
  private config;
12
22
  private cleanupTimer?;
23
+ /**
24
+ * Creates a new MemoryCacheProvider instance.
25
+ * @param config - Configuration options for the cache
26
+ */
13
27
  constructor(config?: MemoryCacheConfig);
28
+ /**
29
+ * Starts the automatic cleanup timer for expired entries.
30
+ */
14
31
  private startCleanupTimer;
32
+ /**
33
+ * Stops the automatic cleanup timer.
34
+ */
15
35
  private stopCleanupTimer;
36
+ /**
37
+ * Removes all expired entries from the cache.
38
+ */
16
39
  private cleanupExpiredEntries;
40
+ /**
41
+ * Evicts the least recently used item if cache size exceeds maximum.
42
+ */
17
43
  private evictLRU;
44
+ /**
45
+ * Retrieves a value from the cache.
46
+ * @param key - The cache key
47
+ * @returns The cached value or undefined if not found or expired
48
+ */
18
49
  get(key: string): Promise<JSONObject | JSONValue | undefined>;
50
+ /**
51
+ * Stores a value in the cache.
52
+ * @param key - The cache key
53
+ * @param value - The value to cache
54
+ * @param ttl - Time to live in seconds (optional)
55
+ */
19
56
  put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void>;
57
+ /**
58
+ * Deletes a value from the cache.
59
+ * @param key - The cache key to delete
60
+ */
20
61
  delete(key: string): Promise<void>;
62
+ /**
63
+ * Checks if a key exists in the cache and has not expired.
64
+ * @param key - The cache key to check
65
+ * @returns True if the key exists and is not expired, false otherwise
66
+ */
21
67
  has(key: string): Promise<boolean>;
68
+ /**
69
+ * Increments a numeric value in the cache atomically.
70
+ * If the key doesn't exist or is expired, it starts from 0.
71
+ * @param key - The cache key to increment
72
+ * @param amount - The amount to increment by (default: 1)
73
+ * @returns The new value after incrementing
74
+ */
22
75
  increment(key: string, amount?: number): Promise<number>;
23
76
  }
24
77