@geekmidas/cache 0.1.0 → 1.0.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/CHANGELOG.md +7 -0
- package/dist/expo.cjs.map +1 -1
- package/dist/expo.d.cts +3 -1
- package/dist/expo.d.cts.map +1 -0
- package/dist/expo.d.mts +3 -1
- package/dist/expo.d.mts.map +1 -0
- package/dist/expo.mjs.map +1 -1
- package/dist/{index-CExVL1mV.d.cts → index-BSYYdwRm.d.cts} +2 -1
- package/dist/index-BSYYdwRm.d.cts.map +1 -0
- package/dist/{index-M02PehQA.d.mts → index-Dx96gr-9.d.mts} +2 -1
- package/dist/index-Dx96gr-9.d.mts.map +1 -0
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/memory.cjs.map +1 -1
- package/dist/memory.d.cts +3 -1
- package/dist/memory.d.cts.map +1 -0
- package/dist/memory.d.mts +3 -1
- package/dist/memory.d.mts.map +1 -0
- package/dist/memory.mjs.map +1 -1
- package/dist/src-CrE4Nl5T.mjs.map +1 -1
- package/dist/src-zfYb7mdS.cjs.map +1 -1
- package/dist/upstash.cjs.map +1 -1
- package/dist/upstash.d.cts +3 -1
- package/dist/upstash.d.cts.map +1 -0
- package/dist/upstash.d.mts +3 -1
- package/dist/upstash.d.mts.map +1 -0
- package/dist/upstash.mjs.map +1 -1
- package/package.json +1 -1
- package/src/__benchmarks__/memory.bench.ts +43 -43
- package/src/__tests__/cache.spec.ts +137 -100
- package/src/__tests__/integration.spec.ts +276 -276
- package/src/__tests__/memory.spec.ts +129 -129
- package/src/__tests__/upstash.spec.ts +209 -180
- package/src/expo.ts +44 -44
- package/src/index.ts +36 -36
- package/src/memory.ts +27 -27
- package/src/upstash.ts +27 -27
- package/tsconfig.json +9 -0
- package/tsdown.config.ts +1 -1
package/src/memory.ts
CHANGED
|
@@ -2,37 +2,37 @@ import { addSeconds } from 'date-fns';
|
|
|
2
2
|
import { type Cache, getExpirationInSeconds } from './';
|
|
3
3
|
|
|
4
4
|
export class InMemoryCache implements Cache {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
private store: Map<string, unknown> = new Map();
|
|
6
|
+
private expirations: Map<string, Date> = new Map();
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
async get<T>(key: string): Promise<T | undefined> {
|
|
9
|
+
const expiration = await this.ttl(key);
|
|
10
|
+
if (!expiration) {
|
|
11
|
+
await this.delete(key);
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
return this.store.get(key) as T | undefined;
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
async ttl(key: string): Promise<number> {
|
|
19
|
+
const expiration = this.expirations.get(key);
|
|
20
|
+
if (!expiration) {
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
return getExpirationInSeconds(expiration);
|
|
25
|
+
}
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
async set<T>(key: string, value: T, ttl: number = 600): Promise<void> {
|
|
28
|
+
this.store.set(key, value);
|
|
29
|
+
const now = new Date();
|
|
30
|
+
const expiresAt = addSeconds(now, ttl);
|
|
31
|
+
this.expirations.set(key, expiresAt);
|
|
32
|
+
}
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
async delete(key: string): Promise<void> {
|
|
35
|
+
this.store.delete(key);
|
|
36
|
+
this.expirations.delete(key);
|
|
37
|
+
}
|
|
38
38
|
}
|
package/src/upstash.ts
CHANGED
|
@@ -2,40 +2,40 @@ import { Redis } from '@upstash/redis';
|
|
|
2
2
|
import type { Cache } from './';
|
|
3
3
|
|
|
4
4
|
export class UpstashCache implements Cache {
|
|
5
|
-
|
|
5
|
+
private client: Redis;
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
constructor(url: string, token: string) {
|
|
8
|
+
this.client = new Redis({
|
|
9
|
+
url,
|
|
10
|
+
token,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
async ttl(key: string): Promise<number> {
|
|
15
|
+
const ttl = await this.client.ttl(key);
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
if (ttl === -2) {
|
|
18
|
+
return 0; // Key does not exist
|
|
19
|
+
}
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
return ttl; // Returns TTL in seconds
|
|
22
|
+
}
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
async get<T>(key: string): Promise<T | undefined> {
|
|
25
|
+
const v = await this.client.get(key);
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
if (v === null) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
return v as T;
|
|
32
|
+
}
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
async set<T>(key: string, value: T, ttl = 3600): Promise<void> {
|
|
35
|
+
await this.client.set(key, value, { ex: ttl });
|
|
36
|
+
}
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
async delete(key: string): Promise<void> {
|
|
39
|
+
await this.client.del(key);
|
|
40
|
+
}
|
|
41
41
|
}
|
package/tsconfig.json
ADDED
package/tsdown.config.ts
CHANGED