@geekmidas/cache 0.0.4 → 0.0.6

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.
Files changed (45) hide show
  1. package/dist/__tests__/cache.spec.cjs +32 -32
  2. package/dist/__tests__/cache.spec.mjs +19 -18
  3. package/dist/__tests__/integration.spec.cjs +46 -46
  4. package/dist/__tests__/integration.spec.mjs +27 -26
  5. package/dist/__tests__/memory.spec.cjs +46 -46
  6. package/dist/__tests__/memory.spec.mjs +24 -23
  7. package/dist/__tests__/upstash.spec.cjs +50 -51
  8. package/dist/__tests__/upstash.spec.mjs +23 -23
  9. package/dist/{chunk-CUT6urMc.cjs → chunk-DWy1uDak.cjs} +9 -0
  10. package/dist/expo.cjs +36 -0
  11. package/dist/expo.d.cts +12 -0
  12. package/dist/expo.d.mts +12 -0
  13. package/dist/expo.mjs +35 -0
  14. package/dist/{index-BPnJ3lrS.d.mts → index-B6eF5Yxi.d.cts} +9 -1
  15. package/dist/{index-q3xYn8_0.d.cts → index-Bv61mxVT.d.mts} +9 -1
  16. package/dist/index.cjs +3 -0
  17. package/dist/index.d.cts +2 -2
  18. package/dist/index.d.mts +2 -2
  19. package/dist/index.mjs +3 -0
  20. package/dist/magic-string.es-CBih0MJw.mjs +1014 -0
  21. package/dist/magic-string.es-KiPEzMtt.cjs +1015 -0
  22. package/dist/memory-Csy6J-Ak.cjs +39 -0
  23. package/dist/memory-QDTnSEX5.mjs +33 -0
  24. package/dist/memory.cjs +2 -1
  25. package/dist/memory.d.cts +4 -2
  26. package/dist/memory.d.mts +4 -2
  27. package/dist/memory.mjs +2 -1
  28. package/dist/src-C7d6HaTp.cjs +19 -0
  29. package/dist/src-DVpoP03o.mjs +13 -0
  30. package/dist/{upstash-CTY-M8vR.mjs → upstash-CXy_duOa.mjs} +5 -0
  31. package/dist/{upstash-0RolGx7m.cjs → upstash-u8Hs4sZG.cjs} +6 -1
  32. package/dist/upstash.cjs +1 -1
  33. package/dist/upstash.d.cts +2 -1
  34. package/dist/upstash.d.mts +2 -1
  35. package/dist/upstash.mjs +1 -1
  36. package/dist/vi.bdSIJ99Y-B-qkABPq.cjs +14955 -0
  37. package/dist/vi.bdSIJ99Y-CB5havZF.mjs +14951 -0
  38. package/package.json +11 -3
  39. package/src/expo.ts +51 -0
  40. package/src/index.ts +25 -0
  41. package/src/memory.ts +22 -2
  42. package/src/upstash.ts +10 -0
  43. package/tsdown.config.ts +5 -0
  44. package/dist/memory-NQi0XmZ2.mjs +0 -16
  45. package/dist/memory-n6Iosq6J.cjs +0 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geekmidas/cache",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Type-safe email client with SMTP support and React templates",
5
5
  "private": false,
6
6
  "type": "module",
@@ -19,15 +19,23 @@
19
19
  "types": "./dist/upstash.d.ts",
20
20
  "import": "./dist/upstash.mjs",
21
21
  "require": "./dist/upstash.cjs"
22
+ },
23
+ "./expo": {
24
+ "types": "./dist/expo.d.ts",
25
+ "import": "./dist/expo.mjs",
26
+ "require": "./dist/expo.cjs"
22
27
  }
23
28
  },
24
29
  "publishConfig": {
25
30
  "registry": "https://registry.npmjs.org/",
26
31
  "access": "public"
27
32
  },
28
- "dependencies": {},
33
+ "dependencies": {
34
+ "date-fns": "~4.1.0"
35
+ },
29
36
  "devDependencies": {},
30
37
  "peerDependencies": {
31
- "@upstash/redis": "~1.35.1"
38
+ "@upstash/redis": "~1.35.1",
39
+ "expo-secure-store": "^14.2.3"
32
40
  }
33
41
  }
package/src/expo.ts ADDED
@@ -0,0 +1,51 @@
1
+ import { addSeconds } from 'date-fns';
2
+ import * as SecureStore from 'expo-secure-store';
3
+ import { type Cache, getExpirationInSeconds } from './index';
4
+
5
+ export class ExpoSecureCache<T> implements Cache<T> {
6
+ static getExpiryKey(key: string): string {
7
+ return `${key}:expiresAt`;
8
+ }
9
+
10
+ async ttl(key: string): Promise<number> {
11
+ const result = await SecureStore.getItemAsync(
12
+ ExpoSecureCache.getExpiryKey(key),
13
+ );
14
+ if (!result) {
15
+ return 0;
16
+ }
17
+
18
+ const secondsLeft = getExpirationInSeconds(result);
19
+
20
+ return Math.max(secondsLeft, 0);
21
+ }
22
+
23
+ // Implementation details
24
+ async get(key: string): Promise<T | undefined> {
25
+ const result = await SecureStore.getItemAsync(key);
26
+ if (!result) {
27
+ return undefined;
28
+ }
29
+
30
+ const expiresAt = await this.ttl(key);
31
+ if (expiresAt === 0) {
32
+ return undefined;
33
+ }
34
+
35
+ return JSON.parse(result) as T;
36
+ }
37
+
38
+ async set(key: string, value: T, ttl: number = 600): Promise<void> {
39
+ const expiresAt = addSeconds(new Date(), ttl).toISOString();
40
+ await SecureStore.setItemAsync(key, JSON.stringify(value));
41
+ await SecureStore.setItemAsync(
42
+ ExpoSecureCache.getExpiryKey(key),
43
+ expiresAt,
44
+ );
45
+ }
46
+
47
+ async delete(key: string): Promise<void> {
48
+ await SecureStore.deleteItemAsync(key);
49
+ await SecureStore.deleteItemAsync(ExpoSecureCache.getExpiryKey(key));
50
+ }
51
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,20 @@
1
+ import { differenceInSeconds } from 'date-fns';
2
+
3
+ export const getExpirationInSeconds = (
4
+ expiresAt: string | number | Date | undefined | null,
5
+ ): number => {
6
+ if (!expiresAt) {
7
+ return 0;
8
+ }
9
+
10
+ const expirationDate = new Date(expiresAt);
11
+ const now = new Date();
12
+
13
+ const secondsLeft = differenceInSeconds(expirationDate, now);
14
+
15
+ return Math.max(secondsLeft, 0);
16
+ };
17
+
1
18
  export interface Cache<T> {
2
19
  /**
3
20
  * Retrieves a value from the cache.
@@ -19,4 +36,12 @@ export interface Cache<T> {
19
36
  * @param key The key to delete from the cache.
20
37
  */
21
38
  delete(key: string): Promise<void>;
39
+
40
+ /**
41
+ * Retrieves the time-to-live (TTL) for a cached value.
42
+ *
43
+ * @param key The key to retrieve the TTL for.
44
+ * @returns The TTL in seconds, or undefined if the key does not exist.
45
+ */
46
+ ttl(key: string): Promise<number>;
22
47
  }
package/src/memory.ts CHANGED
@@ -1,14 +1,34 @@
1
- import type { Cache } from './';
1
+ import { addSeconds } from 'date-fns';
2
+ import { type Cache, getExpirationInSeconds } from './';
2
3
 
3
4
  export class InMemoryCache<T> implements Cache<T> {
4
5
  private store: Map<string, T> = new Map();
6
+ private expirations: Map<string, Date> = new Map();
5
7
 
6
8
  async get(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
+
7
15
  return this.store.get(key);
8
16
  }
9
17
 
10
- async set(key: string, value: T): Promise<void> {
18
+ async ttl(key: string): Promise<number> {
19
+ const expiration = this.expirations.get(key);
20
+ if (!expiration) {
21
+ return 0;
22
+ }
23
+
24
+ return getExpirationInSeconds(expiration);
25
+ }
26
+
27
+ async set(key: string, value: T, ttl: number = 600): Promise<void> {
11
28
  this.store.set(key, value);
29
+ const now = new Date();
30
+ const expiresAt = addSeconds(now, ttl);
31
+ this.expirations.set(key, expiresAt);
12
32
  }
13
33
 
14
34
  async delete(key: string): Promise<void> {
package/src/upstash.ts CHANGED
@@ -11,6 +11,16 @@ export class UpstashCache<T> implements Cache<T> {
11
11
  });
12
12
  }
13
13
 
14
+ async ttl(key: string): Promise<number> {
15
+ const ttl = await this.client.ttl(key);
16
+
17
+ if (ttl === -2) {
18
+ return 0; // Key does not exist
19
+ }
20
+
21
+ return ttl; // Returns TTL in seconds
22
+ }
23
+
14
24
  async get(key: string): Promise<T | undefined> {
15
25
  const v = await this.client.get(key);
16
26
 
@@ -0,0 +1,5 @@
1
+ import { defineConfig } from 'tsdown';
2
+
3
+ export default defineConfig({
4
+ external: ['expo-secure-store', '@upstash/redis'],
5
+ });
@@ -1,16 +0,0 @@
1
- //#region src/memory.ts
2
- var InMemoryCache = class {
3
- store = /* @__PURE__ */ new Map();
4
- async get(key) {
5
- return this.store.get(key);
6
- }
7
- async set(key, value) {
8
- this.store.set(key, value);
9
- }
10
- async delete(key) {
11
- this.store.delete(key);
12
- }
13
- };
14
-
15
- //#endregion
16
- export { InMemoryCache };
@@ -1,22 +0,0 @@
1
-
2
- //#region src/memory.ts
3
- var InMemoryCache = class {
4
- store = /* @__PURE__ */ new Map();
5
- async get(key) {
6
- return this.store.get(key);
7
- }
8
- async set(key, value) {
9
- this.store.set(key, value);
10
- }
11
- async delete(key) {
12
- this.store.delete(key);
13
- }
14
- };
15
-
16
- //#endregion
17
- Object.defineProperty(exports, 'InMemoryCache', {
18
- enumerable: true,
19
- get: function () {
20
- return InMemoryCache;
21
- }
22
- });