@tekir/cache 0.1.5 → 0.1.7
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/cache.d.ts +1 -1
- package/dist/stores/database.d.ts +1 -1
- package/dist/stores/memory.d.ts +1 -1
- package/dist/stores/redis.d.ts +1 -3
- package/dist/stores/redis.js +2 -0
- package/package.json +2 -2
- package/src/cache.ts +1 -1
- package/src/stores/database.ts +1 -1
- package/src/stores/memory.ts +1 -1
- package/src/stores/redis.ts +12 -4
package/dist/cache.d.ts
CHANGED
|
@@ -41,7 +41,7 @@ export declare class Cache {
|
|
|
41
41
|
* @param key - The cache key.
|
|
42
42
|
* @returns The cached value, or `null` if not found or expired.
|
|
43
43
|
*/
|
|
44
|
-
get<T =
|
|
44
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
45
45
|
/**
|
|
46
46
|
* Set a value in the default store.
|
|
47
47
|
*
|
|
@@ -28,7 +28,7 @@ export declare class DatabaseCacheStore implements CacheStore {
|
|
|
28
28
|
* @param key - The cache key.
|
|
29
29
|
* @returns The stored value parsed from JSON, or `null`.
|
|
30
30
|
*/
|
|
31
|
-
get<T =
|
|
31
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
32
32
|
/**
|
|
33
33
|
* Store a value under the given key, reptekirg any existing entry.
|
|
34
34
|
*
|
package/dist/stores/memory.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export declare class MemoryCacheStore implements CacheStore {
|
|
|
19
19
|
* @param key - The cache key.
|
|
20
20
|
* @returns The stored value cast to `T`, or `null`.
|
|
21
21
|
*/
|
|
22
|
-
get<T =
|
|
22
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
23
23
|
/**
|
|
24
24
|
* Store a value under the given key with an optional TTL.
|
|
25
25
|
*
|
package/dist/stores/redis.d.ts
CHANGED
|
@@ -5,8 +5,6 @@ interface RedisClient {
|
|
|
5
5
|
expire(key: string, seconds: number): Promise<unknown>;
|
|
6
6
|
exists(key: string): Promise<boolean | number>;
|
|
7
7
|
del(key: string): Promise<unknown>;
|
|
8
|
-
send(command: string, args: unknown[]): Promise<unknown>;
|
|
9
|
-
connected?: boolean;
|
|
10
8
|
}
|
|
11
9
|
/**
|
|
12
10
|
* Redis-backed cache store with key prefixing and optional TTL.
|
|
@@ -41,7 +39,7 @@ export declare class RedisCacheStore implements CacheStore {
|
|
|
41
39
|
* const value = await store.get<string>('greeting') // 'hello' or null
|
|
42
40
|
* ```
|
|
43
41
|
*/
|
|
44
|
-
get<T =
|
|
42
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
45
43
|
/**
|
|
46
44
|
* Store a value under the given key with an optional TTL.
|
|
47
45
|
* The value is serialized to JSON before being sent to Redis.
|
package/dist/stores/redis.js
CHANGED
|
@@ -109,6 +109,8 @@ export class RedisCacheStore {
|
|
|
109
109
|
* ```
|
|
110
110
|
*/
|
|
111
111
|
async flush() {
|
|
112
|
+
// Structural cast: callers may pass clients with varying `send` arg
|
|
113
|
+
// types. We only need it to accept a string command and an array.
|
|
112
114
|
await this.redis.send('FLUSHDB', []);
|
|
113
115
|
}
|
|
114
116
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tekir/cache",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "In-memory, Redis, and database caching abstraction",
|
|
5
5
|
"author": "dev@tekir.io",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@tekir/core": "^0.1.
|
|
42
|
+
"@tekir/core": "^0.1.27"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"@tekir/redis": "^0.1.0"
|
package/src/cache.ts
CHANGED
|
@@ -56,7 +56,7 @@ export class Cache {
|
|
|
56
56
|
* @param key - The cache key.
|
|
57
57
|
* @returns The cached value, or `null` if not found or expired.
|
|
58
58
|
*/
|
|
59
|
-
async get<T =
|
|
59
|
+
async get<T = any>(key: string): Promise<T | null> { return this.store().get<T>(key) }
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
62
|
* Set a value in the default store.
|
package/src/stores/database.ts
CHANGED
|
@@ -44,7 +44,7 @@ export class DatabaseCacheStore implements CacheStore {
|
|
|
44
44
|
* @param key - The cache key.
|
|
45
45
|
* @returns The stored value parsed from JSON, or `null`.
|
|
46
46
|
*/
|
|
47
|
-
async get<T =
|
|
47
|
+
async get<T = any>(key: string): Promise<T | null> {
|
|
48
48
|
await this._ensureTable()
|
|
49
49
|
const row = await this.db.queryOne(`SELECT value, expires_at FROM "${this.table}" WHERE key = ?`, [key]) as CacheDbRow | null
|
|
50
50
|
if (!row) return null
|
package/src/stores/memory.ts
CHANGED
|
@@ -21,7 +21,7 @@ export class MemoryCacheStore implements CacheStore {
|
|
|
21
21
|
* @param key - The cache key.
|
|
22
22
|
* @returns The stored value cast to `T`, or `null`.
|
|
23
23
|
*/
|
|
24
|
-
async get<T =
|
|
24
|
+
async get<T = any>(key: string): Promise<T | null> {
|
|
25
25
|
const entry = this.data.get(key)
|
|
26
26
|
if (!entry) return null
|
|
27
27
|
if (entry.expiresAt && Date.now() > entry.expiresAt) {
|
package/src/stores/redis.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { CacheStore } from '../types'
|
|
2
2
|
|
|
3
|
+
// Lean shape: only the calls the cache store actually makes. `send` is
|
|
4
|
+
// intentionally absent because raw command shapes vary across clients
|
|
5
|
+
// (`@tekir/redis` typed string-only, ioredis any[], node-redis unknown[]).
|
|
6
|
+
// `flush()` invokes it through a structural cast so any client works.
|
|
3
7
|
interface RedisClient {
|
|
4
8
|
get(key: string): Promise<string | null>
|
|
5
9
|
set(key: string, value: string): Promise<unknown>
|
|
@@ -8,8 +12,10 @@ interface RedisClient {
|
|
|
8
12
|
// (node-redis, ioredis). Normalised at the call site.
|
|
9
13
|
exists(key: string): Promise<boolean | number>
|
|
10
14
|
del(key: string): Promise<unknown>
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface RawSendable {
|
|
18
|
+
send(command: string, args?: unknown[]): Promise<unknown>
|
|
13
19
|
}
|
|
14
20
|
|
|
15
21
|
/**
|
|
@@ -50,7 +56,7 @@ export class RedisCacheStore implements CacheStore {
|
|
|
50
56
|
* const value = await store.get<string>('greeting') // 'hello' or null
|
|
51
57
|
* ```
|
|
52
58
|
*/
|
|
53
|
-
async get<T =
|
|
59
|
+
async get<T = any>(key: string): Promise<T | null> {
|
|
54
60
|
const val = await this.redis.get(this.prefix + key)
|
|
55
61
|
if (val === null) return null
|
|
56
62
|
try { return JSON.parse(val) as T } catch { return val as unknown as T }
|
|
@@ -122,6 +128,8 @@ export class RedisCacheStore implements CacheStore {
|
|
|
122
128
|
* ```
|
|
123
129
|
*/
|
|
124
130
|
async flush(): Promise<void> {
|
|
125
|
-
|
|
131
|
+
// Structural cast: callers may pass clients with varying `send` arg
|
|
132
|
+
// types. We only need it to accept a string command and an array.
|
|
133
|
+
await (this.redis as unknown as RawSendable).send('FLUSHDB', [])
|
|
126
134
|
}
|
|
127
135
|
}
|