@elysiumjs/core 0.4.2 → 0.5.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/src/cache.ts DELETED
@@ -1,218 +0,0 @@
1
- // Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import type { KeyvStoreAdapter } from 'keyv';
16
-
17
- import { createCache } from 'cache-manager';
18
- import { KeyvCacheableMemory } from 'cacheable';
19
- import { Keyv } from 'keyv';
20
- import { alphabetical } from 'radash';
21
-
22
- import { KeyvRedis } from './redis';
23
-
24
- /**
25
- * Interface for a cache storage.
26
- * @author Axel Nana <axel.nana@workbud.com>
27
- */
28
- export interface CacheInterface {
29
- /**
30
- * Retrieves the cached data for a given key.
31
- * @typeParam T The type of the cached data.
32
- *
33
- * @param key The key of the cached data.
34
- */
35
- get<T>(key: string): Promise<T | null>;
36
-
37
- /**
38
- * Retrieves the cached data for a given list of keys.
39
- * @typeParam T The type of the cached data.
40
- *
41
- * @param keys The list of cached data keys.
42
- */
43
- mget<T>(keys: string[]): Promise<Array<T | null>>;
44
-
45
- /**
46
- * Gets the TTL of the cached data with the given key.
47
- * @param key The key of the cached data.
48
- * @returns The TTL of the cached data, or `null` if the data does not exist.
49
- */
50
- ttl(key: string): Promise<number | null>;
51
-
52
- /**
53
- * Caches data under the given key and an optional TTL.
54
- * @typeParam T The type of the data to cache.
55
- *
56
- * @param key The key of the cached data.
57
- * @param data The data to cache.
58
- * @param ttl The optional TTL of the cached data. If not defined, the data is cached forever.
59
- * @returns The cached data.
60
- */
61
- set<T>(key: string, data: T, ttl?: number): Promise<T>;
62
-
63
- /**
64
- * Caches data under the given list of keys and an optional TTL.
65
- * @typeParam T The type of the data to cache.
66
- *
67
- * @param data The list of data to cache.
68
- * @returns The list of cached data.
69
- */
70
- mset<T>(
71
- data: Array<{
72
- key: string;
73
- value: T;
74
- ttl?: number;
75
- }>
76
- ): Promise<
77
- Array<{
78
- key: string;
79
- value: T;
80
- ttl?: number;
81
- }>
82
- >;
83
-
84
- /**
85
- * Deletes the cached data with the given key.
86
- * @typeParam T The type of the data to cache.
87
- *
88
- * @param key The key of the cached data.
89
- * @returns `true` if the data was deleted, `false` otherwise.
90
- */
91
- del(key: string): Promise<boolean>;
92
-
93
- /**
94
- * Deletes the cached data with the given list of keys.
95
- * @typeParam T The type of the data to cache.
96
- *
97
- * @param keys The list of cached data keys.
98
- * @returns `true` if the data was deleted, `false` otherwise.
99
- */
100
- mdel(keys: string[]): Promise<boolean>;
101
-
102
- /**
103
- * Deletes all cached data.
104
- * @returns `true` if the data was deleted, `false` otherwise.
105
- */
106
- clear(): Promise<boolean>;
107
-
108
- /**
109
- * Creates a sub-cache with the given tags.
110
- * @param tags The list of tags for the sub-cache.
111
- */
112
- tags(...tags: string[]): Omit<CacheInterface, 'tags'>;
113
- }
114
-
115
- /**
116
- * Makes a cache interface wrapping the given store.
117
- * @author Axel Nana <axel.nana@workburd.com>
118
- * @param store The cache storage to use in the interface.
119
- * @returns A new `CacheInterface`.
120
- */
121
- const makeCacheInterface = (store: KeyvStoreAdapter): CacheInterface => {
122
- const base = createCache({
123
- stores: [
124
- new Keyv({
125
- store,
126
- namespace: 'cache'
127
- })
128
- ]
129
- });
130
-
131
- const tagsCache = new Map<string, Omit<CacheInterface, 'tags'>>();
132
-
133
- return {
134
- ...base,
135
- tags(...tags) {
136
- const namespace = `cache__${alphabetical(tags, (item) => item).join('__')}`;
137
- let cache = tagsCache.get(namespace);
138
-
139
- if (!cache) {
140
- cache = createCache({
141
- stores: [
142
- new Keyv({
143
- store,
144
- namespace
145
- })
146
- ]
147
- });
148
-
149
- tagsCache.set(namespace, cache);
150
- }
151
-
152
- return cache;
153
- }
154
- };
155
- };
156
-
157
- export abstract class Cache {
158
- static #redis: CacheInterface | undefined;
159
- static #memory: CacheInterface | undefined;
160
-
161
- /**
162
- * A weak cache storage for simple non-intrusive caching of data.
163
- * @author Axel Nana <axel.nana@workbud.com>
164
- */
165
- public static readonly weak = Object.seal({
166
- /**
167
- * INTERNAL USAGE ONLY.
168
- * @private
169
- */
170
- map: new WeakMap<object, unknown>(),
171
-
172
- /**
173
- * Gets the cached data for a given context.
174
- * @param ctx The cache context.
175
- * @returns The cached data.
176
- */
177
- get<T extends object>(ctx: object): T {
178
- if (!this.map.has(ctx)) this.map.set(ctx, {});
179
- return this.map.get(ctx) as T;
180
- },
181
-
182
- /**
183
- * Deletes the cached data for a given context.
184
- * @param ctx The cache context.
185
- */
186
- del(ctx: object): void {
187
- this.map.delete(ctx);
188
- }
189
- });
190
-
191
- /**
192
- * Redis-based cache storage.
193
- * @author Axel Nana <axel.nana@workbud.com>
194
- */
195
- public static get redis(): CacheInterface {
196
- if (!Cache.#redis) {
197
- Cache.#redis = makeCacheInterface(
198
- new KeyvRedis({
199
- connection: 'cache'
200
- })
201
- );
202
- }
203
-
204
- return Cache.#redis;
205
- }
206
-
207
- /**
208
- * Memory-based cache storage.
209
- * @author Axel Nana <axel.nana@workbud.com>
210
- */
211
- public static get memory(): CacheInterface {
212
- if (!Cache.#memory) {
213
- Cache.#memory = makeCacheInterface(new KeyvCacheableMemory({ ttl: 60000, lruSize: 5000 }));
214
- }
215
-
216
- return Cache.#memory;
217
- }
218
- }