@objectstack/service-cache 4.0.3 → 4.0.4
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/.turbo/turbo-build.log +4 -4
- package/CHANGELOG.md +8 -0
- package/README.md +294 -0
- package/package.json +3 -3
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @objectstack/service-cache@4.0.
|
|
2
|
+
> @objectstack/service-cache@4.0.4 build /home/runner/work/framework/framework/packages/services/service-cache
|
|
3
3
|
> tsup --config ../../../tsup.config.ts
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -12,11 +12,11 @@
|
|
|
12
12
|
[34mCJS[39m Build start
|
|
13
13
|
[32mCJS[39m [1mdist/index.cjs [22m[32m4.09 KB[39m
|
|
14
14
|
[32mCJS[39m [1mdist/index.cjs.map [22m[32m9.50 KB[39m
|
|
15
|
-
[32mCJS[39m ⚡️ Build success in
|
|
15
|
+
[32mCJS[39m ⚡️ Build success in 92ms
|
|
16
16
|
[32mESM[39m [1mdist/index.js [22m[32m2.98 KB[39m
|
|
17
17
|
[32mESM[39m [1mdist/index.js.map [22m[32m8.94 KB[39m
|
|
18
|
-
[32mESM[39m ⚡️ Build success in
|
|
18
|
+
[32mESM[39m ⚡️ Build success in 94ms
|
|
19
19
|
[34mDTS[39m Build start
|
|
20
|
-
[32mDTS[39m ⚡️ Build success in
|
|
20
|
+
[32mDTS[39m ⚡️ Build success in 14556ms
|
|
21
21
|
[32mDTS[39m [1mdist/index.d.ts [22m[32m3.56 KB[39m
|
|
22
22
|
[32mDTS[39m [1mdist/index.d.cts [22m[32m3.56 KB[39m
|
package/CHANGELOG.md
CHANGED
package/README.md
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# @objectstack/service-cache
|
|
2
|
+
|
|
3
|
+
Cache Service for ObjectStack — implements `ICacheService` with in-memory and Redis adapters.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Multiple Adapters**: In-memory (development) and Redis (production) support
|
|
8
|
+
- **Type-Safe**: Full TypeScript support with generic value types
|
|
9
|
+
- **TTL Support**: Automatic expiration with time-to-live
|
|
10
|
+
- **Namespace Support**: Organize cache keys by namespace
|
|
11
|
+
- **Pattern Matching**: Delete keys by pattern (e.g., `user:*`)
|
|
12
|
+
- **Statistics**: Track hit/miss rates and memory usage
|
|
13
|
+
- **JSON Serialization**: Automatic serialization of complex objects
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add @objectstack/service-cache
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
For Redis adapter:
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add ioredis
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Basic Usage
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { defineStack } from '@objectstack/spec';
|
|
30
|
+
import { ServiceCache } from '@objectstack/service-cache';
|
|
31
|
+
|
|
32
|
+
const stack = defineStack({
|
|
33
|
+
services: [
|
|
34
|
+
ServiceCache.configure({
|
|
35
|
+
adapter: 'memory', // or 'redis'
|
|
36
|
+
defaultTTL: 300, // 5 minutes
|
|
37
|
+
}),
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Configuration
|
|
43
|
+
|
|
44
|
+
### In-Memory Adapter (Development)
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
ServiceCache.configure({
|
|
48
|
+
adapter: 'memory',
|
|
49
|
+
defaultTTL: 300,
|
|
50
|
+
maxSize: 1000, // Maximum number of entries
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Redis Adapter (Production)
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
ServiceCache.configure({
|
|
58
|
+
adapter: 'redis',
|
|
59
|
+
redis: {
|
|
60
|
+
host: 'localhost',
|
|
61
|
+
port: 6379,
|
|
62
|
+
password: process.env.REDIS_PASSWORD,
|
|
63
|
+
db: 0,
|
|
64
|
+
},
|
|
65
|
+
defaultTTL: 600,
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Service API
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
// Get cache service
|
|
73
|
+
const cache = kernel.getService<ICacheService>('cache');
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Set/Get Operations
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// Set a value
|
|
80
|
+
await cache.set('user:123', { name: 'John', email: 'john@example.com' });
|
|
81
|
+
|
|
82
|
+
// Set with custom TTL (in seconds)
|
|
83
|
+
await cache.set('session:abc', sessionData, { ttl: 3600 }); // 1 hour
|
|
84
|
+
|
|
85
|
+
// Get a value
|
|
86
|
+
const user = await cache.get('user:123');
|
|
87
|
+
|
|
88
|
+
// Get with type safety
|
|
89
|
+
const user = await cache.get<User>('user:123');
|
|
90
|
+
|
|
91
|
+
// Get multiple keys
|
|
92
|
+
const users = await cache.mget(['user:123', 'user:456']);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Existence & Deletion
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// Check if key exists
|
|
99
|
+
const exists = await cache.has('user:123');
|
|
100
|
+
|
|
101
|
+
// Delete a key
|
|
102
|
+
await cache.del('user:123');
|
|
103
|
+
|
|
104
|
+
// Delete multiple keys
|
|
105
|
+
await cache.del(['session:abc', 'session:def']);
|
|
106
|
+
|
|
107
|
+
// Delete by pattern
|
|
108
|
+
await cache.delPattern('user:*');
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Namespaced Operations
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
// Create a namespaced cache instance
|
|
115
|
+
const userCache = cache.namespace('user');
|
|
116
|
+
|
|
117
|
+
// Set in namespace (key becomes 'user:123')
|
|
118
|
+
await userCache.set('123', userData);
|
|
119
|
+
|
|
120
|
+
// Get from namespace
|
|
121
|
+
const user = await userCache.get('123');
|
|
122
|
+
|
|
123
|
+
// Clear entire namespace
|
|
124
|
+
await userCache.clear();
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### TTL Management
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
// Get remaining TTL (in seconds)
|
|
131
|
+
const ttl = await cache.ttl('session:abc');
|
|
132
|
+
|
|
133
|
+
// Update TTL
|
|
134
|
+
await cache.expire('session:abc', 7200); // 2 hours
|
|
135
|
+
|
|
136
|
+
// Make key permanent (remove expiration)
|
|
137
|
+
await cache.persist('user:123');
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Atomic Operations
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
// Increment (useful for counters)
|
|
144
|
+
await cache.incr('page:views:123'); // Returns new value
|
|
145
|
+
|
|
146
|
+
// Increment by amount
|
|
147
|
+
await cache.incrby('score:user:123', 10);
|
|
148
|
+
|
|
149
|
+
// Decrement
|
|
150
|
+
await cache.decr('inventory:product:456');
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Batch Operations
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
// Set multiple keys at once
|
|
157
|
+
await cache.mset({
|
|
158
|
+
'user:123': user1Data,
|
|
159
|
+
'user:456': user2Data,
|
|
160
|
+
'user:789': user3Data,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Get multiple keys
|
|
164
|
+
const users = await cache.mget(['user:123', 'user:456', 'user:789']);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Advanced Features
|
|
168
|
+
|
|
169
|
+
### Cache Aside Pattern
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
async function getUser(id: string): Promise<User> {
|
|
173
|
+
// Try cache first
|
|
174
|
+
const cached = await cache.get<User>(`user:${id}`);
|
|
175
|
+
if (cached) return cached;
|
|
176
|
+
|
|
177
|
+
// Load from database
|
|
178
|
+
const user = await db.findUser(id);
|
|
179
|
+
|
|
180
|
+
// Store in cache
|
|
181
|
+
await cache.set(`user:${id}`, user, { ttl: 600 });
|
|
182
|
+
|
|
183
|
+
return user;
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Cache-Through Pattern
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
async function getUserCacheThrough(id: string): Promise<User> {
|
|
191
|
+
return cache.getOrSet(`user:${id}`, async () => {
|
|
192
|
+
return await db.findUser(id);
|
|
193
|
+
}, { ttl: 600 });
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Invalidation on Write
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
async function updateUser(id: string, data: Partial<User>) {
|
|
201
|
+
// Update database
|
|
202
|
+
await db.updateUser(id, data);
|
|
203
|
+
|
|
204
|
+
// Invalidate cache
|
|
205
|
+
await cache.del(`user:${id}`);
|
|
206
|
+
|
|
207
|
+
// Or update cache immediately
|
|
208
|
+
const updated = await db.findUser(id);
|
|
209
|
+
await cache.set(`user:${id}`, updated);
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Tagging & Invalidation
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
// Tag cache entries
|
|
217
|
+
await cache.set('product:123', productData, {
|
|
218
|
+
ttl: 600,
|
|
219
|
+
tags: ['products', 'category:electronics'],
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// Invalidate by tag
|
|
223
|
+
await cache.invalidateTag('category:electronics');
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Statistics & Monitoring
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
// Get cache statistics
|
|
230
|
+
const stats = await cache.stats();
|
|
231
|
+
// {
|
|
232
|
+
// hits: 1250,
|
|
233
|
+
// misses: 325,
|
|
234
|
+
// hitRate: 0.794,
|
|
235
|
+
// keys: 450,
|
|
236
|
+
// memoryUsage: 1024000 // bytes
|
|
237
|
+
// }
|
|
238
|
+
|
|
239
|
+
// Reset statistics
|
|
240
|
+
await cache.resetStats();
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## REST API Endpoints
|
|
244
|
+
|
|
245
|
+
```
|
|
246
|
+
GET /api/v1/cache/stats # Get cache statistics
|
|
247
|
+
POST /api/v1/cache/clear # Clear cache
|
|
248
|
+
DELETE /api/v1/cache/:key # Delete specific key
|
|
249
|
+
DELETE /api/v1/cache/pattern/:pattern # Delete by pattern
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Best Practices
|
|
253
|
+
|
|
254
|
+
1. **Use Namespaces**: Organize cache keys with namespaces
|
|
255
|
+
2. **Set Appropriate TTLs**: Don't cache data longer than necessary
|
|
256
|
+
3. **Handle Misses**: Always have fallback logic when cache misses
|
|
257
|
+
4. **Invalidate on Write**: Clear stale cache after updates
|
|
258
|
+
5. **Monitor Hit Rates**: Track cache effectiveness with statistics
|
|
259
|
+
6. **Serialize Carefully**: Be mindful of what you serialize (avoid circular references)
|
|
260
|
+
7. **Use Redis in Production**: In-memory adapter is for development only
|
|
261
|
+
|
|
262
|
+
## Performance Considerations
|
|
263
|
+
|
|
264
|
+
- **In-Memory Adapter**: Fast but limited by server memory, not shared across instances
|
|
265
|
+
- **Redis Adapter**: Shared across instances, persistent, but network latency
|
|
266
|
+
- **TTL Strategy**: Balance between freshness and cache hit rate
|
|
267
|
+
- **Key Patterns**: Use consistent naming conventions for easier invalidation
|
|
268
|
+
|
|
269
|
+
## Contract Implementation
|
|
270
|
+
|
|
271
|
+
Implements `ICacheService` from `@objectstack/spec/contracts`:
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
interface ICacheService {
|
|
275
|
+
get<T>(key: string): Promise<T | null>;
|
|
276
|
+
set<T>(key: string, value: T, options?: CacheOptions): Promise<void>;
|
|
277
|
+
del(key: string | string[]): Promise<void>;
|
|
278
|
+
has(key: string): Promise<boolean>;
|
|
279
|
+
ttl(key: string): Promise<number>;
|
|
280
|
+
expire(key: string, ttl: number): Promise<void>;
|
|
281
|
+
clear(): Promise<void>;
|
|
282
|
+
namespace(name: string): ICacheService;
|
|
283
|
+
}
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
## License
|
|
287
|
+
|
|
288
|
+
Apache-2.0
|
|
289
|
+
|
|
290
|
+
## See Also
|
|
291
|
+
|
|
292
|
+
- [Redis Documentation](https://redis.io/documentation)
|
|
293
|
+
- [@objectstack/spec/contracts](../../spec/src/contracts/)
|
|
294
|
+
- [Caching Best Practices](/content/docs/guides/caching/)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@objectstack/service-cache",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.4",
|
|
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,8 +14,8 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@objectstack/core": "4.0.
|
|
18
|
-
"@objectstack/spec": "4.0.
|
|
17
|
+
"@objectstack/core": "4.0.4",
|
|
18
|
+
"@objectstack/spec": "4.0.4"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@types/node": "^25.6.0",
|