@bgord/bun 1.7.1 → 1.7.2
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/dist/cache-repository-lru-cache.adapter.d.ts +5 -1
- package/dist/cache-repository-lru-cache.adapter.d.ts.map +1 -1
- package/dist/cache-repository-lru-cache.adapter.js +19 -3
- package/dist/cache-repository-lru-cache.adapter.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/cache-repository-lru-cache.adapter.ts +29 -7
package/package.json
CHANGED
|
@@ -1,32 +1,54 @@
|
|
|
1
|
-
import { LRUCache } from "lru-cache";
|
|
1
|
+
import type { LRUCache } from "lru-cache";
|
|
2
2
|
import type { CacheRepositoryPort, CacheRepositoryTtlType } from "./cache-repository.port";
|
|
3
3
|
import type { Hash } from "./hash.vo";
|
|
4
4
|
import type { HashValueType } from "./hash-value.vo";
|
|
5
5
|
|
|
6
|
+
export const CacheRepositoryLruCacheAdapterError = {
|
|
7
|
+
MissingDependency: "cache.repository.lru.cache.adapter.error.missing.dependency",
|
|
8
|
+
};
|
|
9
|
+
|
|
6
10
|
export class CacheRepositoryLruCacheAdapter implements CacheRepositoryPort {
|
|
7
|
-
private readonly store
|
|
11
|
+
private readonly store: LRUCache<HashValueType, any>;
|
|
12
|
+
|
|
13
|
+
private constructor(store: LRUCache<HashValueType, any>) {
|
|
14
|
+
this.store = store;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static async build(config: CacheRepositoryTtlType): Promise<CacheRepositoryLruCacheAdapter> {
|
|
18
|
+
// biome-ignore lint: lint/suspicious/noAssignInExpressions
|
|
19
|
+
let LRUCacheConstructor;
|
|
8
20
|
|
|
9
|
-
|
|
10
|
-
|
|
21
|
+
try {
|
|
22
|
+
const module = await import("lru-cache");
|
|
23
|
+
|
|
24
|
+
LRUCacheConstructor = module.LRUCache;
|
|
25
|
+
} catch {
|
|
26
|
+
throw new Error(CacheRepositoryLruCacheAdapterError.MissingDependency);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const store = new LRUCacheConstructor<HashValueType, any>({
|
|
11
30
|
max: 100_000,
|
|
12
31
|
ttl: config.type === "finite" ? config.ttl.ms : undefined,
|
|
13
32
|
ttlAutopurge: true,
|
|
14
33
|
});
|
|
34
|
+
|
|
35
|
+
return new CacheRepositoryLruCacheAdapter(store);
|
|
15
36
|
}
|
|
16
37
|
|
|
38
|
+
// 5. Clean implementation without null checks
|
|
17
39
|
async get<T>(subject: Hash): Promise<T | null> {
|
|
18
40
|
return (this.store.get(subject.get()) as T) ?? null;
|
|
19
41
|
}
|
|
20
42
|
|
|
21
|
-
async set<T>(subject: Hash, value: T) {
|
|
43
|
+
async set<T>(subject: Hash, value: T): Promise<void> {
|
|
22
44
|
this.store.set(subject.get(), value);
|
|
23
45
|
}
|
|
24
46
|
|
|
25
|
-
async delete(subject: Hash) {
|
|
47
|
+
async delete(subject: Hash): Promise<void> {
|
|
26
48
|
this.store.delete(subject.get());
|
|
27
49
|
}
|
|
28
50
|
|
|
29
|
-
async flush() {
|
|
51
|
+
async flush(): Promise<void> {
|
|
30
52
|
this.store.clear();
|
|
31
53
|
}
|
|
32
54
|
}
|