@objectstack/service-cache 4.0.4 → 4.0.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/package.json +30 -6
- package/.turbo/turbo-build.log +0 -22
- package/CHANGELOG.md +0 -177
- package/src/cache-service-plugin.ts +0 -63
- package/src/index.ts +0 -8
- package/src/memory-cache-adapter.test.ts +0 -107
- package/src/memory-cache-adapter.ts +0 -90
- package/src/redis-cache-adapter.ts +0 -63
- package/tsconfig.json +0 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@objectstack/service-cache",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.5",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "Cache Service for ObjectStack — implements ICacheService with in-memory and Redis adapters",
|
|
6
6
|
"type": "module",
|
|
@@ -14,13 +14,37 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@objectstack/core": "4.0.
|
|
18
|
-
"@objectstack/spec": "4.0.
|
|
17
|
+
"@objectstack/core": "4.0.5",
|
|
18
|
+
"@objectstack/spec": "4.0.5"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@types/node": "^25.6.
|
|
22
|
-
"typescript": "^6.0.
|
|
23
|
-
"vitest": "^4.1.
|
|
21
|
+
"@types/node": "^25.6.2",
|
|
22
|
+
"typescript": "^6.0.3",
|
|
23
|
+
"vitest": "^4.1.5"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"objectstack",
|
|
27
|
+
"service",
|
|
28
|
+
"cache",
|
|
29
|
+
"redis"
|
|
30
|
+
],
|
|
31
|
+
"author": "ObjectStack",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/objectstack-ai/framework.git",
|
|
35
|
+
"directory": "packages/services/service-cache"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://objectstack.ai/docs",
|
|
38
|
+
"bugs": "https://github.com/objectstack-ai/framework/issues",
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"README.md"
|
|
45
|
+
],
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18.0.0"
|
|
24
48
|
},
|
|
25
49
|
"scripts": {
|
|
26
50
|
"build": "tsup --config ../../../tsup.config.ts",
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @objectstack/service-cache@4.0.4 build /home/runner/work/framework/framework/packages/services/service-cache
|
|
3
|
-
> tsup --config ../../../tsup.config.ts
|
|
4
|
-
|
|
5
|
-
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
-
[34mCLI[39m tsup v8.5.1
|
|
8
|
-
[34mCLI[39m Using tsup config: /home/runner/work/framework/framework/tsup.config.ts
|
|
9
|
-
[34mCLI[39m Target: es2020
|
|
10
|
-
[34mCLI[39m Cleaning output folder
|
|
11
|
-
[34mESM[39m Build start
|
|
12
|
-
[34mCJS[39m Build start
|
|
13
|
-
[32mCJS[39m [1mdist/index.cjs [22m[32m4.09 KB[39m
|
|
14
|
-
[32mCJS[39m [1mdist/index.cjs.map [22m[32m9.50 KB[39m
|
|
15
|
-
[32mCJS[39m ⚡️ Build success in 92ms
|
|
16
|
-
[32mESM[39m [1mdist/index.js [22m[32m2.98 KB[39m
|
|
17
|
-
[32mESM[39m [1mdist/index.js.map [22m[32m8.94 KB[39m
|
|
18
|
-
[32mESM[39m ⚡️ Build success in 94ms
|
|
19
|
-
[34mDTS[39m Build start
|
|
20
|
-
[32mDTS[39m ⚡️ Build success in 14556ms
|
|
21
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m3.56 KB[39m
|
|
22
|
-
[32mDTS[39m [1mdist/index.d.cts [22m[32m3.56 KB[39m
|
package/CHANGELOG.md
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
# @objectstack/service-cache
|
|
2
|
-
|
|
3
|
-
## 4.0.4
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- Updated dependencies [326b66b]
|
|
8
|
-
- @objectstack/spec@4.0.4
|
|
9
|
-
- @objectstack/core@4.0.4
|
|
10
|
-
|
|
11
|
-
## 4.0.3
|
|
12
|
-
|
|
13
|
-
### Patch Changes
|
|
14
|
-
|
|
15
|
-
- @objectstack/spec@4.0.3
|
|
16
|
-
- @objectstack/core@4.0.3
|
|
17
|
-
|
|
18
|
-
## 4.0.2
|
|
19
|
-
|
|
20
|
-
### Patch Changes
|
|
21
|
-
|
|
22
|
-
- Updated dependencies [5f659e9]
|
|
23
|
-
- @objectstack/spec@4.0.2
|
|
24
|
-
- @objectstack/core@4.0.2
|
|
25
|
-
|
|
26
|
-
## 4.0.0
|
|
27
|
-
|
|
28
|
-
### Patch Changes
|
|
29
|
-
|
|
30
|
-
- Updated dependencies [f08ffc3]
|
|
31
|
-
- Updated dependencies [e0b0a78]
|
|
32
|
-
- @objectstack/spec@4.0.0
|
|
33
|
-
- @objectstack/core@4.0.0
|
|
34
|
-
|
|
35
|
-
## 3.3.1
|
|
36
|
-
|
|
37
|
-
### Patch Changes
|
|
38
|
-
|
|
39
|
-
- @objectstack/spec@3.3.1
|
|
40
|
-
- @objectstack/core@3.3.1
|
|
41
|
-
|
|
42
|
-
## 3.3.0
|
|
43
|
-
|
|
44
|
-
### Patch Changes
|
|
45
|
-
|
|
46
|
-
- @objectstack/spec@3.3.0
|
|
47
|
-
- @objectstack/core@3.3.0
|
|
48
|
-
|
|
49
|
-
## 3.2.9
|
|
50
|
-
|
|
51
|
-
### Patch Changes
|
|
52
|
-
|
|
53
|
-
- @objectstack/spec@3.2.9
|
|
54
|
-
- @objectstack/core@3.2.9
|
|
55
|
-
|
|
56
|
-
## 3.2.8
|
|
57
|
-
|
|
58
|
-
### Patch Changes
|
|
59
|
-
|
|
60
|
-
- @objectstack/spec@3.2.8
|
|
61
|
-
- @objectstack/core@3.2.8
|
|
62
|
-
|
|
63
|
-
## 3.2.7
|
|
64
|
-
|
|
65
|
-
### Patch Changes
|
|
66
|
-
|
|
67
|
-
- @objectstack/spec@3.2.7
|
|
68
|
-
- @objectstack/core@3.2.7
|
|
69
|
-
|
|
70
|
-
## 3.2.6
|
|
71
|
-
|
|
72
|
-
### Patch Changes
|
|
73
|
-
|
|
74
|
-
- @objectstack/spec@3.2.6
|
|
75
|
-
- @objectstack/core@3.2.6
|
|
76
|
-
|
|
77
|
-
## 3.2.5
|
|
78
|
-
|
|
79
|
-
### Patch Changes
|
|
80
|
-
|
|
81
|
-
- @objectstack/spec@3.2.5
|
|
82
|
-
- @objectstack/core@3.2.5
|
|
83
|
-
|
|
84
|
-
## 3.2.4
|
|
85
|
-
|
|
86
|
-
### Patch Changes
|
|
87
|
-
|
|
88
|
-
- @objectstack/spec@3.2.4
|
|
89
|
-
- @objectstack/core@3.2.4
|
|
90
|
-
|
|
91
|
-
## 3.2.3
|
|
92
|
-
|
|
93
|
-
### Patch Changes
|
|
94
|
-
|
|
95
|
-
- @objectstack/spec@3.2.3
|
|
96
|
-
- @objectstack/core@3.2.3
|
|
97
|
-
|
|
98
|
-
## 3.2.2
|
|
99
|
-
|
|
100
|
-
### Patch Changes
|
|
101
|
-
|
|
102
|
-
- Updated dependencies [46defbb]
|
|
103
|
-
- @objectstack/spec@3.2.2
|
|
104
|
-
- @objectstack/core@3.2.2
|
|
105
|
-
|
|
106
|
-
## 3.2.1
|
|
107
|
-
|
|
108
|
-
### Patch Changes
|
|
109
|
-
|
|
110
|
-
- Updated dependencies [850b546]
|
|
111
|
-
- @objectstack/spec@3.2.1
|
|
112
|
-
- @objectstack/core@3.2.1
|
|
113
|
-
|
|
114
|
-
## 3.2.0
|
|
115
|
-
|
|
116
|
-
### Patch Changes
|
|
117
|
-
|
|
118
|
-
- Updated dependencies [5901c29]
|
|
119
|
-
- @objectstack/spec@3.2.0
|
|
120
|
-
- @objectstack/core@3.2.0
|
|
121
|
-
|
|
122
|
-
## 3.1.1
|
|
123
|
-
|
|
124
|
-
### Patch Changes
|
|
125
|
-
|
|
126
|
-
- Updated dependencies [953d667]
|
|
127
|
-
- @objectstack/spec@3.1.1
|
|
128
|
-
- @objectstack/core@3.1.1
|
|
129
|
-
|
|
130
|
-
## 3.1.0
|
|
131
|
-
|
|
132
|
-
### Patch Changes
|
|
133
|
-
|
|
134
|
-
- Updated dependencies [0088830]
|
|
135
|
-
- @objectstack/spec@3.1.0
|
|
136
|
-
- @objectstack/core@3.1.0
|
|
137
|
-
|
|
138
|
-
## 3.0.11
|
|
139
|
-
|
|
140
|
-
### Patch Changes
|
|
141
|
-
|
|
142
|
-
- Updated dependencies [92d9d99]
|
|
143
|
-
- @objectstack/spec@3.0.11
|
|
144
|
-
- @objectstack/core@3.0.11
|
|
145
|
-
|
|
146
|
-
## 3.0.10
|
|
147
|
-
|
|
148
|
-
### Patch Changes
|
|
149
|
-
|
|
150
|
-
- Updated dependencies [d1e5d31]
|
|
151
|
-
- @objectstack/spec@3.0.10
|
|
152
|
-
- @objectstack/core@3.0.10
|
|
153
|
-
|
|
154
|
-
## 3.0.9
|
|
155
|
-
|
|
156
|
-
### Patch Changes
|
|
157
|
-
|
|
158
|
-
- Updated dependencies [15e0df6]
|
|
159
|
-
- @objectstack/spec@3.0.9
|
|
160
|
-
- @objectstack/core@3.0.9
|
|
161
|
-
|
|
162
|
-
## 3.0.8
|
|
163
|
-
|
|
164
|
-
### Patch Changes
|
|
165
|
-
|
|
166
|
-
- Updated dependencies [5a968a2]
|
|
167
|
-
- @objectstack/spec@3.0.8
|
|
168
|
-
- @objectstack/core@3.0.8
|
|
169
|
-
|
|
170
|
-
## 3.0.7
|
|
171
|
-
|
|
172
|
-
### Patch Changes
|
|
173
|
-
|
|
174
|
-
- Updated dependencies [0119bd7]
|
|
175
|
-
- Updated dependencies [5426bdf]
|
|
176
|
-
- @objectstack/spec@3.0.7
|
|
177
|
-
- @objectstack/core@3.0.7
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import type { Plugin, PluginContext } from '@objectstack/core';
|
|
4
|
-
import { MemoryCacheAdapter } from './memory-cache-adapter.js';
|
|
5
|
-
import type { MemoryCacheAdapterOptions } from './memory-cache-adapter.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Configuration options for the CacheServicePlugin.
|
|
9
|
-
*/
|
|
10
|
-
export interface CacheServicePluginOptions {
|
|
11
|
-
/** Cache adapter type (default: 'memory') */
|
|
12
|
-
adapter?: 'memory' | 'redis';
|
|
13
|
-
/** Options for the memory cache adapter */
|
|
14
|
-
memory?: MemoryCacheAdapterOptions;
|
|
15
|
-
/** Redis connection URL (used when adapter is 'redis') */
|
|
16
|
-
redisUrl?: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* CacheServicePlugin — Production ICacheService implementation.
|
|
21
|
-
*
|
|
22
|
-
* Registers a cache service with the kernel during the init phase.
|
|
23
|
-
* Supports in-memory and Redis adapters.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* import { ObjectKernel } from '@objectstack/core';
|
|
28
|
-
* import { CacheServicePlugin } from '@objectstack/service-cache';
|
|
29
|
-
*
|
|
30
|
-
* const kernel = new ObjectKernel();
|
|
31
|
-
* kernel.use(new CacheServicePlugin({ adapter: 'memory', memory: { maxSize: 1000 } }));
|
|
32
|
-
* await kernel.bootstrap();
|
|
33
|
-
*
|
|
34
|
-
* const cache = kernel.getService('cache');
|
|
35
|
-
* await cache.set('key', 'value', 60);
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export class CacheServicePlugin implements Plugin {
|
|
39
|
-
name = 'com.objectstack.service.cache';
|
|
40
|
-
version = '1.0.0';
|
|
41
|
-
type = 'standard';
|
|
42
|
-
|
|
43
|
-
private readonly options: CacheServicePluginOptions;
|
|
44
|
-
|
|
45
|
-
constructor(options: CacheServicePluginOptions = {}) {
|
|
46
|
-
this.options = { adapter: 'memory', ...options };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async init(ctx: PluginContext): Promise<void> {
|
|
50
|
-
const adapter = this.options.adapter;
|
|
51
|
-
if (adapter === 'redis') {
|
|
52
|
-
// Redis adapter is a skeleton — throw an informative error for now
|
|
53
|
-
throw new Error(
|
|
54
|
-
'Redis cache adapter is not yet implemented. ' +
|
|
55
|
-
'Use adapter: "memory" or provide a custom ICacheService via ctx.registerService("cache", impl).'
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const cache = new MemoryCacheAdapter(this.options.memory);
|
|
60
|
-
ctx.registerService('cache', cache);
|
|
61
|
-
ctx.logger.info('CacheServicePlugin: registered memory cache adapter');
|
|
62
|
-
}
|
|
63
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
export { CacheServicePlugin } from './cache-service-plugin.js';
|
|
4
|
-
export type { CacheServicePluginOptions } from './cache-service-plugin.js';
|
|
5
|
-
export { MemoryCacheAdapter } from './memory-cache-adapter.js';
|
|
6
|
-
export type { MemoryCacheAdapterOptions } from './memory-cache-adapter.js';
|
|
7
|
-
export { RedisCacheAdapter } from './redis-cache-adapter.js';
|
|
8
|
-
export type { RedisCacheAdapterOptions } from './redis-cache-adapter.js';
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import { describe, it, expect } from 'vitest';
|
|
4
|
-
import { MemoryCacheAdapter } from './memory-cache-adapter';
|
|
5
|
-
import type { ICacheService } from '@objectstack/spec/contracts';
|
|
6
|
-
|
|
7
|
-
describe('MemoryCacheAdapter', () => {
|
|
8
|
-
it('should implement ICacheService contract', () => {
|
|
9
|
-
const cache: ICacheService = new MemoryCacheAdapter();
|
|
10
|
-
expect(typeof cache.get).toBe('function');
|
|
11
|
-
expect(typeof cache.set).toBe('function');
|
|
12
|
-
expect(typeof cache.delete).toBe('function');
|
|
13
|
-
expect(typeof cache.has).toBe('function');
|
|
14
|
-
expect(typeof cache.clear).toBe('function');
|
|
15
|
-
expect(typeof cache.stats).toBe('function');
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('should set and get a value', async () => {
|
|
19
|
-
const cache = new MemoryCacheAdapter();
|
|
20
|
-
await cache.set('key1', 'value1');
|
|
21
|
-
expect(await cache.get('key1')).toBe('value1');
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('should return undefined for missing key', async () => {
|
|
25
|
-
const cache = new MemoryCacheAdapter();
|
|
26
|
-
expect(await cache.get('nonexistent')).toBeUndefined();
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should delete a key', async () => {
|
|
30
|
-
const cache = new MemoryCacheAdapter();
|
|
31
|
-
await cache.set('key1', 'value1');
|
|
32
|
-
expect(await cache.delete('key1')).toBe(true);
|
|
33
|
-
expect(await cache.get('key1')).toBeUndefined();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should return false when deleting missing key', async () => {
|
|
37
|
-
const cache = new MemoryCacheAdapter();
|
|
38
|
-
expect(await cache.delete('missing')).toBe(false);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should check if a key exists with has()', async () => {
|
|
42
|
-
const cache = new MemoryCacheAdapter();
|
|
43
|
-
expect(await cache.has('key1')).toBe(false);
|
|
44
|
-
await cache.set('key1', 'value1');
|
|
45
|
-
expect(await cache.has('key1')).toBe(true);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('should clear all entries', async () => {
|
|
49
|
-
const cache = new MemoryCacheAdapter();
|
|
50
|
-
await cache.set('a', 1);
|
|
51
|
-
await cache.set('b', 2);
|
|
52
|
-
await cache.clear();
|
|
53
|
-
expect(await cache.has('a')).toBe(false);
|
|
54
|
-
expect(await cache.has('b')).toBe(false);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should expire entries based on TTL', async () => {
|
|
58
|
-
const cache = new MemoryCacheAdapter();
|
|
59
|
-
await cache.set('temp', 'data', 0.001); // 1ms TTL
|
|
60
|
-
await new Promise(r => setTimeout(r, 20));
|
|
61
|
-
expect(await cache.get('temp')).toBeUndefined();
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should track hit/miss stats', async () => {
|
|
65
|
-
const cache = new MemoryCacheAdapter();
|
|
66
|
-
await cache.set('key1', 'value1');
|
|
67
|
-
await cache.get('key1'); // hit
|
|
68
|
-
await cache.get('missing'); // miss
|
|
69
|
-
const stats = await cache.stats();
|
|
70
|
-
expect(stats.hits).toBe(1);
|
|
71
|
-
expect(stats.misses).toBe(1);
|
|
72
|
-
expect(stats.keyCount).toBe(1);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('should apply defaultTtl when no TTL is provided', async () => {
|
|
76
|
-
const cache = new MemoryCacheAdapter({ defaultTtl: 0.001 });
|
|
77
|
-
await cache.set('key', 'value');
|
|
78
|
-
await new Promise(r => setTimeout(r, 20));
|
|
79
|
-
expect(await cache.get('key')).toBeUndefined();
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('should evict oldest entry when maxSize is reached', async () => {
|
|
83
|
-
const cache = new MemoryCacheAdapter({ maxSize: 2 });
|
|
84
|
-
await cache.set('a', 1);
|
|
85
|
-
await cache.set('b', 2);
|
|
86
|
-
await cache.set('c', 3); // should evict 'a'
|
|
87
|
-
expect(await cache.has('a')).toBe(false);
|
|
88
|
-
expect(await cache.get('b')).toBe(2);
|
|
89
|
-
expect(await cache.get('c')).toBe(3);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should not evict when updating existing key at maxSize', async () => {
|
|
93
|
-
const cache = new MemoryCacheAdapter({ maxSize: 2 });
|
|
94
|
-
await cache.set('a', 1);
|
|
95
|
-
await cache.set('b', 2);
|
|
96
|
-
await cache.set('a', 10); // update, not new entry
|
|
97
|
-
expect(await cache.get('a')).toBe(10);
|
|
98
|
-
expect(await cache.get('b')).toBe(2);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('should handle has() with expired TTL', async () => {
|
|
102
|
-
const cache = new MemoryCacheAdapter();
|
|
103
|
-
await cache.set('expiring', 'val', 0.001);
|
|
104
|
-
await new Promise(r => setTimeout(r, 20));
|
|
105
|
-
expect(await cache.has('expiring')).toBe(false);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import type { ICacheService, CacheStats } from '@objectstack/spec/contracts';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* In-memory cache entry with optional TTL expiry.
|
|
7
|
-
*/
|
|
8
|
-
interface CacheEntry<T = unknown> {
|
|
9
|
-
value: T;
|
|
10
|
-
expires?: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Configuration options for MemoryCacheAdapter.
|
|
15
|
-
*/
|
|
16
|
-
export interface MemoryCacheAdapterOptions {
|
|
17
|
-
/** Maximum number of entries before eviction (0 = unlimited) */
|
|
18
|
-
maxSize?: number;
|
|
19
|
-
/** Default TTL in seconds (0 = no expiry) */
|
|
20
|
-
defaultTtl?: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* In-memory cache adapter implementing ICacheService.
|
|
25
|
-
*
|
|
26
|
-
* Uses a Map-backed store with TTL-based expiry and LRU-style eviction.
|
|
27
|
-
* Suitable for single-process environments, development, and testing.
|
|
28
|
-
*/
|
|
29
|
-
export class MemoryCacheAdapter implements ICacheService {
|
|
30
|
-
private readonly store = new Map<string, CacheEntry>();
|
|
31
|
-
private hits = 0;
|
|
32
|
-
private misses = 0;
|
|
33
|
-
private readonly maxSize: number;
|
|
34
|
-
private readonly defaultTtl: number;
|
|
35
|
-
|
|
36
|
-
constructor(options: MemoryCacheAdapterOptions = {}) {
|
|
37
|
-
this.maxSize = options.maxSize ?? 0;
|
|
38
|
-
this.defaultTtl = options.defaultTtl ?? 0;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async get<T = unknown>(key: string): Promise<T | undefined> {
|
|
42
|
-
const entry = this.store.get(key);
|
|
43
|
-
if (!entry || (entry.expires && Date.now() > entry.expires)) {
|
|
44
|
-
if (entry) this.store.delete(key);
|
|
45
|
-
this.misses++;
|
|
46
|
-
return undefined;
|
|
47
|
-
}
|
|
48
|
-
this.hits++;
|
|
49
|
-
return entry.value as T;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async set<T = unknown>(key: string, value: T, ttl?: number): Promise<void> {
|
|
53
|
-
const effectiveTtl = ttl ?? this.defaultTtl;
|
|
54
|
-
if (this.maxSize > 0 && !this.store.has(key) && this.store.size >= this.maxSize) {
|
|
55
|
-
// Evict oldest entry (first key in Map insertion order)
|
|
56
|
-
const firstKey = this.store.keys().next().value;
|
|
57
|
-
if (firstKey !== undefined) this.store.delete(firstKey);
|
|
58
|
-
}
|
|
59
|
-
this.store.set(key, {
|
|
60
|
-
value,
|
|
61
|
-
expires: effectiveTtl > 0 ? Date.now() + effectiveTtl * 1000 : undefined,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async delete(key: string): Promise<boolean> {
|
|
66
|
-
return this.store.delete(key);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async has(key: string): Promise<boolean> {
|
|
70
|
-
const entry = this.store.get(key);
|
|
71
|
-
if (!entry) return false;
|
|
72
|
-
if (entry.expires && Date.now() > entry.expires) {
|
|
73
|
-
this.store.delete(key);
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async clear(): Promise<void> {
|
|
80
|
-
this.store.clear();
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async stats(): Promise<CacheStats> {
|
|
84
|
-
return {
|
|
85
|
-
hits: this.hits,
|
|
86
|
-
misses: this.misses,
|
|
87
|
-
keyCount: this.store.size,
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import type { ICacheService, CacheStats } from '@objectstack/spec/contracts';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Configuration for the Redis cache adapter.
|
|
7
|
-
*/
|
|
8
|
-
export interface RedisCacheAdapterOptions {
|
|
9
|
-
/** Redis connection URL (e.g. 'redis://localhost:6379') */
|
|
10
|
-
url: string;
|
|
11
|
-
/** Key prefix for namespacing (default: 'os:') */
|
|
12
|
-
keyPrefix?: string;
|
|
13
|
-
/** Default TTL in seconds (0 = no expiry) */
|
|
14
|
-
defaultTtl?: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Redis cache adapter skeleton implementing ICacheService.
|
|
19
|
-
*
|
|
20
|
-
* This is a placeholder for future Redis integration.
|
|
21
|
-
* Concrete implementation will use `ioredis` or `redis` client.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* const cache = new RedisCacheAdapter({ url: 'redis://localhost:6379' });
|
|
26
|
-
* await cache.set('user:1', { name: 'Alice' }, 3600);
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
export class RedisCacheAdapter implements ICacheService {
|
|
30
|
-
private readonly url: string;
|
|
31
|
-
private readonly keyPrefix: string;
|
|
32
|
-
private readonly defaultTtl: number;
|
|
33
|
-
|
|
34
|
-
constructor(options: RedisCacheAdapterOptions) {
|
|
35
|
-
this.url = options.url;
|
|
36
|
-
this.keyPrefix = options.keyPrefix ?? 'os:';
|
|
37
|
-
this.defaultTtl = options.defaultTtl ?? 0;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async get<T = unknown>(_key: string): Promise<T | undefined> {
|
|
41
|
-
throw new Error(`RedisCacheAdapter not yet implemented (url: ${this.url}, prefix: ${this.keyPrefix}, ttl: ${this.defaultTtl})`);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async set<T = unknown>(_key: string, _value: T, _ttl?: number): Promise<void> {
|
|
45
|
-
throw new Error('RedisCacheAdapter not yet implemented');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async delete(_key: string): Promise<boolean> {
|
|
49
|
-
throw new Error('RedisCacheAdapter not yet implemented');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async has(_key: string): Promise<boolean> {
|
|
53
|
-
throw new Error('RedisCacheAdapter not yet implemented');
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async clear(): Promise<void> {
|
|
57
|
-
throw new Error('RedisCacheAdapter not yet implemented');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async stats(): Promise<CacheStats> {
|
|
61
|
-
throw new Error('RedisCacheAdapter not yet implemented');
|
|
62
|
-
}
|
|
63
|
-
}
|
package/tsconfig.json
DELETED