@veloxts/cache 0.6.56 → 0.6.57
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/CHANGELOG.md +8 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +24 -0
- package/dist/providers.d.ts +52 -0
- package/dist/providers.js +69 -0
- package/dist/tokens.d.ts +60 -0
- package/dist/tokens.js +65 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @veloxts/cache
|
|
3
|
+
*
|
|
4
|
+
* Multi-driver caching layer for VeloxTS framework.
|
|
5
|
+
*
|
|
6
|
+
* Features:
|
|
7
|
+
* - Multiple drivers: memory (lru-cache), Redis (ioredis)
|
|
8
|
+
* - Type-safe cache operations with automatic serialization
|
|
9
|
+
* - Cache tags for grouped invalidation
|
|
10
|
+
* - Distributed locks for exclusive access
|
|
11
|
+
* - `remember()` pattern for cache-aside
|
|
12
|
+
* - Human-readable TTL strings ('1h', '30m', '1d')
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { cachePlugin } from '@veloxts/cache';
|
|
17
|
+
*
|
|
18
|
+
* // Register plugin
|
|
19
|
+
* app.use(cachePlugin({
|
|
20
|
+
* driver: 'redis',
|
|
21
|
+
* config: { url: process.env.REDIS_URL },
|
|
22
|
+
* }));
|
|
23
|
+
*
|
|
24
|
+
* // In procedures:
|
|
25
|
+
* const user = await ctx.cache.remember('user:123', '1h', async () => {
|
|
26
|
+
* return ctx.db.user.findUnique({ where: { id: '123' } });
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Tags for grouped invalidation
|
|
30
|
+
* await ctx.cache.tags(['users']).put('user:123', user);
|
|
31
|
+
* await ctx.cache.tags(['users']).flush();
|
|
32
|
+
*
|
|
33
|
+
* // Distributed locks
|
|
34
|
+
* await ctx.cache.lockAndRun('payment:process', '30s', async () => {
|
|
35
|
+
* // Only one process can run this at a time
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @packageDocumentation
|
|
40
|
+
*/
|
|
41
|
+
export { createMemoryCache, DRIVER_NAME as MEMORY_DRIVER } from './drivers/memory.js';
|
|
42
|
+
export { createRedisCache, DRIVER_NAME as REDIS_DRIVER } from './drivers/redis.js';
|
|
43
|
+
export { type CacheManager, cache, createCacheManager, type TaggedCache, } from './manager.js';
|
|
44
|
+
export { cachePlugin, closeCache, getCache, getCacheFromInstance, initCache, } from './plugin.js';
|
|
45
|
+
export type { CacheConfig, CacheDriver, CacheEntry, CacheOptions, CachePluginOptions, CacheStore, DurationString, LockOptions, LockResult, MemoryCacheConfig, RedisCacheConfig, TaggableCacheStore, TTL, } from './types.js';
|
|
46
|
+
export { formatTtl, isDurationString, parseTtl, parseTtlMs, } from './utils.js';
|
|
47
|
+
/**
|
|
48
|
+
* DI tokens and providers for @veloxts/cache
|
|
49
|
+
*
|
|
50
|
+
* Use these to integrate cache services with the @veloxts/core DI container.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* import { Container } from '@veloxts/core';
|
|
55
|
+
* import { registerCacheProviders, CACHE_MANAGER } from '@veloxts/cache';
|
|
56
|
+
*
|
|
57
|
+
* const container = new Container();
|
|
58
|
+
* await registerCacheProviders(container, { driver: 'memory' });
|
|
59
|
+
*
|
|
60
|
+
* const cache = container.resolve(CACHE_MANAGER);
|
|
61
|
+
* await cache.put('key', 'value', '1h');
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export { registerCacheProviders } from './providers.js';
|
|
65
|
+
export { CACHE_CONFIG, CACHE_MANAGER, CACHE_STORE } from './tokens.js';
|
package/dist/index.js
CHANGED
|
@@ -47,3 +47,27 @@ export { cache, createCacheManager, } from './manager.js';
|
|
|
47
47
|
export { cachePlugin, closeCache, getCache, getCacheFromInstance, initCache, } from './plugin.js';
|
|
48
48
|
// Utilities
|
|
49
49
|
export { formatTtl, isDurationString, parseTtl, parseTtlMs, } from './utils.js';
|
|
50
|
+
// ============================================================================
|
|
51
|
+
// Dependency Injection
|
|
52
|
+
// ============================================================================
|
|
53
|
+
/**
|
|
54
|
+
* DI tokens and providers for @veloxts/cache
|
|
55
|
+
*
|
|
56
|
+
* Use these to integrate cache services with the @veloxts/core DI container.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* import { Container } from '@veloxts/core';
|
|
61
|
+
* import { registerCacheProviders, CACHE_MANAGER } from '@veloxts/cache';
|
|
62
|
+
*
|
|
63
|
+
* const container = new Container();
|
|
64
|
+
* await registerCacheProviders(container, { driver: 'memory' });
|
|
65
|
+
*
|
|
66
|
+
* const cache = container.resolve(CACHE_MANAGER);
|
|
67
|
+
* await cache.put('key', 'value', '1h');
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
// Provider exports - factory functions for registering services
|
|
71
|
+
export { registerCacheProviders } from './providers.js';
|
|
72
|
+
// Token exports - unique identifiers for DI resolution
|
|
73
|
+
export { CACHE_CONFIG, CACHE_MANAGER, CACHE_STORE } from './tokens.js';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DI Providers for @veloxts/cache
|
|
3
|
+
*
|
|
4
|
+
* Factory provider functions for registering cache services with the DI container.
|
|
5
|
+
* These providers allow services to be managed by the container for testability and flexibility.
|
|
6
|
+
*
|
|
7
|
+
* @module cache/providers
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Container } from '@veloxts/core';
|
|
12
|
+
* import { registerCacheProviders, CACHE_MANAGER } from '@veloxts/cache';
|
|
13
|
+
*
|
|
14
|
+
* const container = new Container();
|
|
15
|
+
* await registerCacheProviders(container, { driver: 'memory' });
|
|
16
|
+
*
|
|
17
|
+
* const cache = container.resolve(CACHE_MANAGER);
|
|
18
|
+
* await cache.put('key', 'value', '1h');
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import type { Container } from '@veloxts/core';
|
|
22
|
+
import type { CachePluginOptions } from './types.js';
|
|
23
|
+
/**
|
|
24
|
+
* Registers cache providers with a container
|
|
25
|
+
*
|
|
26
|
+
* This handles async initialization of the cache manager and registers
|
|
27
|
+
* the resolved instance directly for synchronous resolution.
|
|
28
|
+
*
|
|
29
|
+
* @param container - The DI container to register providers with
|
|
30
|
+
* @param config - Cache plugin options (driver, prefix, etc.)
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { Container } from '@veloxts/core';
|
|
35
|
+
* import { registerCacheProviders, CACHE_MANAGER } from '@veloxts/cache';
|
|
36
|
+
*
|
|
37
|
+
* const container = new Container();
|
|
38
|
+
*
|
|
39
|
+
* // Memory cache (development)
|
|
40
|
+
* await registerCacheProviders(container, { driver: 'memory' });
|
|
41
|
+
*
|
|
42
|
+
* // Redis cache (production)
|
|
43
|
+
* await registerCacheProviders(container, {
|
|
44
|
+
* driver: 'redis',
|
|
45
|
+
* config: { url: process.env.REDIS_URL },
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* const cache = container.resolve(CACHE_MANAGER);
|
|
49
|
+
* await cache.put('key', 'value', '30m');
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function registerCacheProviders(container: Container, config?: CachePluginOptions): Promise<void>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DI Providers for @veloxts/cache
|
|
3
|
+
*
|
|
4
|
+
* Factory provider functions for registering cache services with the DI container.
|
|
5
|
+
* These providers allow services to be managed by the container for testability and flexibility.
|
|
6
|
+
*
|
|
7
|
+
* @module cache/providers
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Container } from '@veloxts/core';
|
|
12
|
+
* import { registerCacheProviders, CACHE_MANAGER } from '@veloxts/cache';
|
|
13
|
+
*
|
|
14
|
+
* const container = new Container();
|
|
15
|
+
* await registerCacheProviders(container, { driver: 'memory' });
|
|
16
|
+
*
|
|
17
|
+
* const cache = container.resolve(CACHE_MANAGER);
|
|
18
|
+
* await cache.put('key', 'value', '1h');
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { createCacheManager } from './manager.js';
|
|
22
|
+
import { CACHE_CONFIG, CACHE_MANAGER } from './tokens.js';
|
|
23
|
+
// ============================================================================
|
|
24
|
+
// Bulk Registration Helpers
|
|
25
|
+
// ============================================================================
|
|
26
|
+
/**
|
|
27
|
+
* Registers cache providers with a container
|
|
28
|
+
*
|
|
29
|
+
* This handles async initialization of the cache manager and registers
|
|
30
|
+
* the resolved instance directly for synchronous resolution.
|
|
31
|
+
*
|
|
32
|
+
* @param container - The DI container to register providers with
|
|
33
|
+
* @param config - Cache plugin options (driver, prefix, etc.)
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* import { Container } from '@veloxts/core';
|
|
38
|
+
* import { registerCacheProviders, CACHE_MANAGER } from '@veloxts/cache';
|
|
39
|
+
*
|
|
40
|
+
* const container = new Container();
|
|
41
|
+
*
|
|
42
|
+
* // Memory cache (development)
|
|
43
|
+
* await registerCacheProviders(container, { driver: 'memory' });
|
|
44
|
+
*
|
|
45
|
+
* // Redis cache (production)
|
|
46
|
+
* await registerCacheProviders(container, {
|
|
47
|
+
* driver: 'redis',
|
|
48
|
+
* config: { url: process.env.REDIS_URL },
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* const cache = container.resolve(CACHE_MANAGER);
|
|
52
|
+
* await cache.put('key', 'value', '30m');
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export async function registerCacheProviders(container, config = {}) {
|
|
56
|
+
// Register config
|
|
57
|
+
container.register({
|
|
58
|
+
provide: CACHE_CONFIG,
|
|
59
|
+
useValue: config,
|
|
60
|
+
});
|
|
61
|
+
// Create cache manager (async operation)
|
|
62
|
+
const cacheManager = await createCacheManager(config);
|
|
63
|
+
// Register the resolved cache manager instance directly
|
|
64
|
+
// This allows synchronous resolution from the container
|
|
65
|
+
container.register({
|
|
66
|
+
provide: CACHE_MANAGER,
|
|
67
|
+
useValue: cacheManager,
|
|
68
|
+
});
|
|
69
|
+
}
|
package/dist/tokens.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DI Tokens for @veloxts/cache
|
|
3
|
+
*
|
|
4
|
+
* Symbol-based tokens for type-safe dependency injection.
|
|
5
|
+
* These tokens allow cache services to be registered, resolved, and mocked via the DI container.
|
|
6
|
+
*
|
|
7
|
+
* @module cache/tokens
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Container } from '@veloxts/core';
|
|
12
|
+
* import { CACHE_MANAGER, registerCacheProviders } from '@veloxts/cache';
|
|
13
|
+
*
|
|
14
|
+
* const container = new Container();
|
|
15
|
+
* await registerCacheProviders(container, { driver: 'memory' });
|
|
16
|
+
*
|
|
17
|
+
* const cache = container.resolve(CACHE_MANAGER);
|
|
18
|
+
* await cache.put('key', 'value', '1h');
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import type { CacheManager } from './manager.js';
|
|
22
|
+
import type { CachePluginOptions, TaggableCacheStore } from './types.js';
|
|
23
|
+
/**
|
|
24
|
+
* Cache manager token
|
|
25
|
+
*
|
|
26
|
+
* The main cache manager instance with get/put/remember/tags/lock support.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const cache = container.resolve(CACHE_MANAGER);
|
|
31
|
+
* await cache.put('user:123', user, '30m');
|
|
32
|
+
* const cached = await cache.get('user:123');
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare const CACHE_MANAGER: import("@veloxts/core").SymbolToken<CacheManager>;
|
|
36
|
+
/**
|
|
37
|
+
* Cache store token
|
|
38
|
+
*
|
|
39
|
+
* The underlying cache store (memory or Redis).
|
|
40
|
+
* Use CACHE_MANAGER for high-level operations; use this for direct store access.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const store = container.resolve(CACHE_STORE);
|
|
45
|
+
* await store.putWithTags('key', value, ['users'], '1h');
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare const CACHE_STORE: import("@veloxts/core").SymbolToken<TaggableCacheStore>;
|
|
49
|
+
/**
|
|
50
|
+
* Cache configuration token
|
|
51
|
+
*
|
|
52
|
+
* Contains cache plugin options including driver and driver-specific config.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const config = container.resolve(CACHE_CONFIG);
|
|
57
|
+
* console.log(config.driver); // 'memory' or 'redis'
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare const CACHE_CONFIG: import("@veloxts/core").SymbolToken<CachePluginOptions>;
|
package/dist/tokens.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DI Tokens for @veloxts/cache
|
|
3
|
+
*
|
|
4
|
+
* Symbol-based tokens for type-safe dependency injection.
|
|
5
|
+
* These tokens allow cache services to be registered, resolved, and mocked via the DI container.
|
|
6
|
+
*
|
|
7
|
+
* @module cache/tokens
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { Container } from '@veloxts/core';
|
|
12
|
+
* import { CACHE_MANAGER, registerCacheProviders } from '@veloxts/cache';
|
|
13
|
+
*
|
|
14
|
+
* const container = new Container();
|
|
15
|
+
* await registerCacheProviders(container, { driver: 'memory' });
|
|
16
|
+
*
|
|
17
|
+
* const cache = container.resolve(CACHE_MANAGER);
|
|
18
|
+
* await cache.put('key', 'value', '1h');
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { token } from '@veloxts/core';
|
|
22
|
+
// ============================================================================
|
|
23
|
+
// Core Cache Tokens
|
|
24
|
+
// ============================================================================
|
|
25
|
+
/**
|
|
26
|
+
* Cache manager token
|
|
27
|
+
*
|
|
28
|
+
* The main cache manager instance with get/put/remember/tags/lock support.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const cache = container.resolve(CACHE_MANAGER);
|
|
33
|
+
* await cache.put('user:123', user, '30m');
|
|
34
|
+
* const cached = await cache.get('user:123');
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export const CACHE_MANAGER = token.symbol('CACHE_MANAGER');
|
|
38
|
+
/**
|
|
39
|
+
* Cache store token
|
|
40
|
+
*
|
|
41
|
+
* The underlying cache store (memory or Redis).
|
|
42
|
+
* Use CACHE_MANAGER for high-level operations; use this for direct store access.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const store = container.resolve(CACHE_STORE);
|
|
47
|
+
* await store.putWithTags('key', value, ['users'], '1h');
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export const CACHE_STORE = token.symbol('CACHE_STORE');
|
|
51
|
+
// ============================================================================
|
|
52
|
+
// Configuration Tokens
|
|
53
|
+
// ============================================================================
|
|
54
|
+
/**
|
|
55
|
+
* Cache configuration token
|
|
56
|
+
*
|
|
57
|
+
* Contains cache plugin options including driver and driver-specific config.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const config = container.resolve(CACHE_CONFIG);
|
|
62
|
+
* console.log(config.driver); // 'memory' or 'redis'
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export const CACHE_CONFIG = token.symbol('CACHE_CONFIG');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veloxts/cache",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.57",
|
|
4
4
|
"description": "Multi-driver caching layer for VeloxTS framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"fastify-plugin": "5.1.0",
|
|
32
32
|
"lru-cache": "11.1.0",
|
|
33
33
|
"superjson": "2.2.2",
|
|
34
|
-
"@veloxts/core": "0.6.
|
|
34
|
+
"@veloxts/core": "0.6.57"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"ioredis": ">=5.0.0"
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"ioredis": "5.6.1",
|
|
48
48
|
"typescript": "5.9.3",
|
|
49
49
|
"vitest": "4.0.16",
|
|
50
|
-
"@veloxts/testing": "0.6.
|
|
50
|
+
"@veloxts/testing": "0.6.57"
|
|
51
51
|
},
|
|
52
52
|
"keywords": [
|
|
53
53
|
"velox",
|