@veloxts/cache 0.7.1 → 0.7.3

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 CHANGED
@@ -1,5 +1,20 @@
1
1
  # @veloxts/cache
2
2
 
3
+ ## 0.7.3
4
+
5
+ ### Patch Changes
6
+
7
+ - feat(cli): auto-populate Zod schemas from Prisma model fields
8
+ - Updated dependencies
9
+ - @veloxts/core@0.7.3
10
+
11
+ ## 0.7.2
12
+
13
+ ### Patch Changes
14
+
15
+ - Updated dependencies
16
+ - @veloxts/core@0.7.2
17
+
3
18
  ## 0.7.1
4
19
 
5
20
  ### Patch Changes
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Cache Drivers
3
+ *
4
+ * Re-exports all cache driver implementations.
5
+ */
6
+ export { createMemoryCache, DRIVER_NAME as MEMORY_DRIVER } from './memory.js';
7
+ export { createRedisCache, DRIVER_NAME as REDIS_DRIVER } from './redis.js';
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Memory Cache Driver
3
+ *
4
+ * In-memory cache implementation using LRU-cache.
5
+ * Best for development or single-instance deployments.
6
+ */
7
+ import type { MemoryCacheConfig, TaggableCacheStore } from '../types.js';
8
+ /**
9
+ * Create a memory cache store.
10
+ *
11
+ * @param config - Memory cache configuration
12
+ * @returns Cache store implementation
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const cache = createMemoryCache({ maxSize: 500 });
17
+ *
18
+ * await cache.put('user:123', { name: 'John' }, '30m');
19
+ * const user = await cache.get('user:123');
20
+ * ```
21
+ */
22
+ export declare function createMemoryCache(config?: Omit<MemoryCacheConfig, 'driver'>): TaggableCacheStore;
23
+ /**
24
+ * Memory cache driver name.
25
+ */
26
+ export declare const DRIVER_NAME: "memory";
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Redis Cache Driver
3
+ *
4
+ * Redis cache implementation using ioredis.
5
+ * Best for production and multi-instance deployments.
6
+ */
7
+ import type { RedisCacheConfig, TaggableCacheStore } from '../types.js';
8
+ /**
9
+ * Create a Redis cache store.
10
+ *
11
+ * @param config - Redis cache configuration
12
+ * @returns Cache store implementation
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const cache = await createRedisCache({
17
+ * url: process.env.REDIS_URL,
18
+ * prefix: 'myapp:',
19
+ * });
20
+ *
21
+ * await cache.put('user:123', { name: 'John' }, '30m');
22
+ * const user = await cache.get('user:123');
23
+ * ```
24
+ */
25
+ export declare function createRedisCache(config: Omit<RedisCacheConfig, 'driver'>): Promise<TaggableCacheStore>;
26
+ /**
27
+ * Redis cache driver name.
28
+ */
29
+ export declare const DRIVER_NAME: "redis";
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Cache Manager
3
+ *
4
+ * High-level cache API with remember, tags, and lock support.
5
+ */
6
+ import type { CachePluginOptions, LockOptions, LockResult, TTL } from './types.js';
7
+ /**
8
+ * Cache manager interface.
9
+ */
10
+ export interface CacheManager {
11
+ /**
12
+ * Get a value from the cache.
13
+ */
14
+ get<T>(key: string): Promise<T | null>;
15
+ /**
16
+ * Store a value in the cache.
17
+ */
18
+ put<T>(key: string, value: T, ttl?: TTL): Promise<void>;
19
+ /**
20
+ * Check if a key exists in the cache.
21
+ */
22
+ has(key: string): Promise<boolean>;
23
+ /**
24
+ * Remove a key from the cache.
25
+ */
26
+ forget(key: string): Promise<boolean>;
27
+ /**
28
+ * Increment a numeric value.
29
+ */
30
+ increment(key: string, value?: number): Promise<number>;
31
+ /**
32
+ * Decrement a numeric value.
33
+ */
34
+ decrement(key: string, value?: number): Promise<number>;
35
+ /**
36
+ * Clear all entries from the cache.
37
+ */
38
+ flush(): Promise<void>;
39
+ /**
40
+ * Get multiple values from the cache.
41
+ */
42
+ many<T>(keys: string[]): Promise<Map<string, T | null>>;
43
+ /**
44
+ * Store multiple values in the cache.
45
+ */
46
+ putMany<T>(entries: Map<string, T>, ttl?: TTL): Promise<void>;
47
+ /**
48
+ * Get a value or compute and cache it if not present.
49
+ */
50
+ remember<T>(key: string, ttl: TTL, callback: () => Promise<T>): Promise<T>;
51
+ /**
52
+ * Get a value or compute and cache it forever if not present.
53
+ */
54
+ rememberForever<T>(key: string, callback: () => Promise<T>): Promise<T>;
55
+ /**
56
+ * Delete a value and return it.
57
+ */
58
+ pull<T>(key: string): Promise<T | null>;
59
+ /**
60
+ * Store a value only if the key doesn't exist.
61
+ */
62
+ add<T>(key: string, value: T, ttl?: TTL): Promise<boolean>;
63
+ /**
64
+ * Create a tagged cache instance.
65
+ */
66
+ tags(tags: string[]): TaggedCache;
67
+ /**
68
+ * Acquire a distributed lock.
69
+ */
70
+ lock(key: string, options: LockOptions): Promise<LockResult>;
71
+ /**
72
+ * Execute a callback with a lock.
73
+ */
74
+ lockAndRun<T>(key: string, ttl: TTL, callback: () => Promise<T>): Promise<T>;
75
+ /**
76
+ * Close the cache connection.
77
+ */
78
+ close(): Promise<void>;
79
+ }
80
+ /**
81
+ * Tagged cache interface for grouped invalidation.
82
+ */
83
+ export interface TaggedCache {
84
+ /**
85
+ * Get a value from the tagged cache.
86
+ */
87
+ get<T>(key: string): Promise<T | null>;
88
+ /**
89
+ * Store a value with the configured tags.
90
+ */
91
+ put<T>(key: string, value: T, ttl?: TTL): Promise<void>;
92
+ /**
93
+ * Get a value or compute and cache it with tags.
94
+ */
95
+ remember<T>(key: string, ttl: TTL, callback: () => Promise<T>): Promise<T>;
96
+ /**
97
+ * Flush all entries with the configured tags.
98
+ */
99
+ flush(): Promise<void>;
100
+ }
101
+ /**
102
+ * Create a cache manager.
103
+ *
104
+ * @param options - Cache plugin options
105
+ * @returns Cache manager instance
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * // Memory cache (development)
110
+ * const cache = await createCacheManager({ driver: 'memory' });
111
+ *
112
+ * // Redis cache (production)
113
+ * const cache = await createCacheManager({
114
+ * driver: 'redis',
115
+ * config: { url: process.env.REDIS_URL },
116
+ * });
117
+ *
118
+ * // Basic usage
119
+ * await cache.put('user:123', { name: 'John' }, '30m');
120
+ * const user = await cache.get('user:123');
121
+ *
122
+ * // Remember pattern
123
+ * const user = await cache.remember('user:123', '1h', async () => {
124
+ * return await db.user.findUnique({ where: { id: '123' } });
125
+ * });
126
+ *
127
+ * // Tags for grouped invalidation
128
+ * await cache.tags(['users']).put('user:123', user);
129
+ * await cache.tags(['users']).flush(); // Invalidates all user entries
130
+ *
131
+ * // Distributed lock
132
+ * await cache.lockAndRun('process:payment', '30s', async () => {
133
+ * // Only one process can run this at a time
134
+ * });
135
+ * ```
136
+ */
137
+ export declare function createCacheManager(options?: CachePluginOptions): Promise<CacheManager>;
138
+ /**
139
+ * Alias for createCacheManager.
140
+ */
141
+ export declare const cache: typeof createCacheManager;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Cache Utilities
3
+ *
4
+ * Helper functions for cache operations.
5
+ */
6
+ import type { DurationString, TTL } from './types.js';
7
+ /**
8
+ * Check if a value is a duration string.
9
+ */
10
+ export declare function isDurationString(value: unknown): value is DurationString;
11
+ /**
12
+ * Parse a TTL value to seconds.
13
+ *
14
+ * @param ttl - TTL as seconds (number) or duration string (e.g., '1h', '30m')
15
+ * @returns TTL in seconds
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * parseTtl(3600) // 3600
20
+ * parseTtl('1h') // 3600
21
+ * parseTtl('30m') // 1800
22
+ * parseTtl('1d') // 86400
23
+ * ```
24
+ */
25
+ export declare function parseTtl(ttl: TTL): number;
26
+ /**
27
+ * Parse a TTL value to milliseconds.
28
+ */
29
+ export declare function parseTtlMs(ttl: TTL): number;
30
+ /**
31
+ * Format a TTL in seconds to a human-readable string.
32
+ */
33
+ export declare function formatTtl(seconds: number): string;
34
+ /**
35
+ * Calculate expiration timestamp from TTL.
36
+ */
37
+ export declare function calculateExpiration(ttl: TTL): number;
38
+ /**
39
+ * Check if a timestamp has expired.
40
+ */
41
+ export declare function isExpired(expiresAt: number | null): boolean;
42
+ /**
43
+ * Generate a cache key with prefix.
44
+ */
45
+ export declare function prefixKey(key: string, prefix: string): string;
46
+ /**
47
+ * Generate a tag key for tag-based invalidation.
48
+ */
49
+ export declare function tagKey(tag: string, prefix: string): string;
50
+ /**
51
+ * Generate a unique lock token using cryptographically secure random values.
52
+ */
53
+ export declare function generateLockToken(): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veloxts/cache",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "description": "Multi-driver caching layer for VeloxTS framework",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -35,7 +35,7 @@
35
35
  "fastify-plugin": "5.1.0",
36
36
  "lru-cache": "11.2.6",
37
37
  "superjson": "2.2.6",
38
- "@veloxts/core": "0.7.1"
38
+ "@veloxts/core": "0.7.3"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "ioredis": ">=5.0.0"
@@ -51,7 +51,7 @@
51
51
  "ioredis": "5.9.3",
52
52
  "typescript": "5.9.3",
53
53
  "vitest": "4.0.18",
54
- "@veloxts/testing": "0.7.1"
54
+ "@veloxts/testing": "0.7.3"
55
55
  },
56
56
  "keywords": [
57
57
  "velox",