@parsrun/cache 0.1.0
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/README.md +135 -0
- package/dist/adapters/cloudflare-kv.d.ts +51 -0
- package/dist/adapters/cloudflare-kv.js +197 -0
- package/dist/adapters/cloudflare-kv.js.map +1 -0
- package/dist/adapters/index.d.ts +6 -0
- package/dist/adapters/index.js +783 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/memory.d.ts +59 -0
- package/dist/adapters/memory.js +179 -0
- package/dist/adapters/memory.js.map +1 -0
- package/dist/adapters/redis.d.ts +72 -0
- package/dist/adapters/redis.js +242 -0
- package/dist/adapters/redis.js.map +1 -0
- package/dist/adapters/upstash.d.ts +50 -0
- package/dist/adapters/upstash.js +229 -0
- package/dist/adapters/upstash.js.map +1 -0
- package/dist/index.d.ts +140 -0
- package/dist/index.js +978 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +234 -0
- package/dist/types.js +43 -0
- package/dist/types.js.map +1 -0
- package/package.json +80 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { CacheServiceConfig, CacheGetOptions, CacheSetOptions } from './types.js';
|
|
2
|
+
export { CacheAdapter, CacheAdapterType, CacheDeleteOptions, CacheEntry, CacheError, CacheErrorCodes, CloudflareKVCacheConfig, KVNamespace, MemoryCacheConfig, RedisCacheConfig, UpstashCacheConfig } from './types.js';
|
|
3
|
+
export { MemoryCacheAdapter, createMemoryCacheAdapter } from './adapters/memory.js';
|
|
4
|
+
export { RedisCacheAdapter, createRedisCacheAdapter } from './adapters/redis.js';
|
|
5
|
+
export { UpstashCacheAdapter, createUpstashCacheAdapter } from './adapters/upstash.js';
|
|
6
|
+
export { CloudflareKVCacheAdapter, createCloudflareKVCacheAdapter } from './adapters/cloudflare-kv.js';
|
|
7
|
+
export { CacheConfig, CacheGetResult, CacheStats, MultiTierCacheConfig, CacheSetOptions as ParsCacheSetOptions, CloudflareKvConfig as ParsCloudflareKvConfig, MemoryCacheConfig as ParsMemoryCacheConfig, RedisCacheConfig as ParsRedisCacheConfig, UpstashCacheConfig as ParsUpstashCacheConfig, cacheConfig, cacheGetResult, cacheStats, cloudflareKvConfig, memoryCacheConfig, multiTierCacheConfig, cacheSetOptions as parsCacheSetOptions, redisCacheConfig, type, upstashCacheConfig } from '@parsrun/types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @parsrun/cache
|
|
11
|
+
* Edge-compatible caching for Pars
|
|
12
|
+
*
|
|
13
|
+
* Supports multiple adapters:
|
|
14
|
+
* - Memory (development)
|
|
15
|
+
* - Redis/ioredis (Node.js)
|
|
16
|
+
* - Upstash (Edge)
|
|
17
|
+
* - Cloudflare KV (Workers)
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { createCacheService, createMemoryCacheAdapter } from '@parsrun/cache';
|
|
22
|
+
*
|
|
23
|
+
* const cache = createCacheService({
|
|
24
|
+
* adapter: createMemoryCacheAdapter(),
|
|
25
|
+
* defaultTtl: 3600,
|
|
26
|
+
* keyPrefix: 'myapp',
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* await cache.set('user:123', { name: 'John' });
|
|
30
|
+
* const user = await cache.get('user:123');
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Cache Service
|
|
36
|
+
* High-level cache service with prefix and default TTL support
|
|
37
|
+
*/
|
|
38
|
+
declare class CacheService {
|
|
39
|
+
private adapter;
|
|
40
|
+
private defaultTtl;
|
|
41
|
+
private keyPrefix;
|
|
42
|
+
private debug;
|
|
43
|
+
constructor(config: CacheServiceConfig);
|
|
44
|
+
private prefixKey;
|
|
45
|
+
/**
|
|
46
|
+
* Get adapter type
|
|
47
|
+
*/
|
|
48
|
+
get adapterType(): string;
|
|
49
|
+
/**
|
|
50
|
+
* Get a value from cache
|
|
51
|
+
*/
|
|
52
|
+
get<T = unknown>(key: string, options?: CacheGetOptions): Promise<T | null>;
|
|
53
|
+
/**
|
|
54
|
+
* Set a value in cache
|
|
55
|
+
*/
|
|
56
|
+
set<T = unknown>(key: string, value: T, options?: CacheSetOptions): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Delete a value from cache
|
|
59
|
+
*/
|
|
60
|
+
delete(key: string): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Check if a key exists in cache
|
|
63
|
+
*/
|
|
64
|
+
has(key: string): Promise<boolean>;
|
|
65
|
+
/**
|
|
66
|
+
* Clear all entries from cache
|
|
67
|
+
*/
|
|
68
|
+
clear(): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Get multiple values at once
|
|
71
|
+
*/
|
|
72
|
+
getMany<T = unknown>(keys: string[]): Promise<Map<string, T | null>>;
|
|
73
|
+
/**
|
|
74
|
+
* Set multiple values at once
|
|
75
|
+
*/
|
|
76
|
+
setMany<T = unknown>(entries: Map<string, T>, options?: CacheSetOptions): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Delete multiple keys
|
|
79
|
+
*/
|
|
80
|
+
deleteMany(keys: string[]): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Invalidate entries by tags
|
|
83
|
+
*/
|
|
84
|
+
invalidateByTags(tags: string[]): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Get TTL for a key (in seconds)
|
|
87
|
+
* @returns TTL in seconds, -1 if no expiry, -2 if key doesn't exist
|
|
88
|
+
*/
|
|
89
|
+
ttl(key: string): Promise<number>;
|
|
90
|
+
/**
|
|
91
|
+
* Get or set a value with callback
|
|
92
|
+
* @param key Cache key
|
|
93
|
+
* @param fn Function to compute value if not cached
|
|
94
|
+
* @param options Set options (applied if value is computed)
|
|
95
|
+
*/
|
|
96
|
+
getOrSet<T>(key: string, fn: () => T | Promise<T>, options?: CacheSetOptions): Promise<T>;
|
|
97
|
+
/**
|
|
98
|
+
* Wrap a function with caching
|
|
99
|
+
*/
|
|
100
|
+
wrap<TArgs extends unknown[], TReturn>(fn: (...args: TArgs) => Promise<TReturn>, keyFn: (...args: TArgs) => string, options?: CacheSetOptions): (...args: TArgs) => Promise<TReturn>;
|
|
101
|
+
/**
|
|
102
|
+
* Close/cleanup cache resources
|
|
103
|
+
*/
|
|
104
|
+
close(): Promise<void>;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Create a cache service
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* // With Memory (development)
|
|
112
|
+
* const cache = createCacheService({
|
|
113
|
+
* adapter: createMemoryCacheAdapter(),
|
|
114
|
+
* defaultTtl: 3600,
|
|
115
|
+
* });
|
|
116
|
+
*
|
|
117
|
+
* // With Upstash (Edge)
|
|
118
|
+
* const cache = createCacheService({
|
|
119
|
+
* adapter: createUpstashCacheAdapter({
|
|
120
|
+
* url: process.env.UPSTASH_REDIS_REST_URL,
|
|
121
|
+
* token: process.env.UPSTASH_REDIS_REST_TOKEN,
|
|
122
|
+
* }),
|
|
123
|
+
* defaultTtl: 3600,
|
|
124
|
+
* });
|
|
125
|
+
*
|
|
126
|
+
* // With Cloudflare KV (Workers)
|
|
127
|
+
* const cache = createCacheService({
|
|
128
|
+
* adapter: createCloudflareKVCacheAdapter({
|
|
129
|
+
* namespace: env.CACHE_KV,
|
|
130
|
+
* }),
|
|
131
|
+
* });
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
declare function createCacheService(config: CacheServiceConfig): CacheService;
|
|
135
|
+
declare const _default: {
|
|
136
|
+
CacheService: typeof CacheService;
|
|
137
|
+
createCacheService: typeof createCacheService;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export { CacheGetOptions, CacheService, CacheServiceConfig, CacheSetOptions, createCacheService, _default as default };
|