@geekmidas/cache 0.0.5 → 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 (43) hide show
  1. package/dist/__tests__/cache.spec.cjs +4 -3
  2. package/dist/__tests__/cache.spec.mjs +4 -3
  3. package/dist/__tests__/integration.spec.cjs +3 -2
  4. package/dist/__tests__/integration.spec.mjs +3 -2
  5. package/dist/__tests__/memory.spec.cjs +3 -2
  6. package/dist/__tests__/memory.spec.mjs +3 -2
  7. package/dist/__tests__/upstash.spec.cjs +2 -2
  8. package/dist/__tests__/upstash.spec.mjs +2 -2
  9. package/dist/expo.cjs +7 -7
  10. package/dist/expo.d.cts +2 -2
  11. package/dist/expo.d.mts +2 -2
  12. package/dist/expo.mjs +8 -8
  13. package/dist/{index-CVen_7Cc.d.mts → index-B6eF5Yxi.d.cts} +9 -1
  14. package/dist/{index-q3xYn8_0.d.cts → index-Bv61mxVT.d.mts} +9 -1
  15. package/dist/index.cjs +3 -0
  16. package/dist/index.d.cts +2 -2
  17. package/dist/index.d.mts +2 -2
  18. package/dist/index.mjs +3 -0
  19. package/dist/memory-Csy6J-Ak.cjs +39 -0
  20. package/dist/memory-QDTnSEX5.mjs +33 -0
  21. package/dist/memory.cjs +2 -1
  22. package/dist/memory.d.cts +4 -2
  23. package/dist/memory.d.mts +4 -2
  24. package/dist/memory.mjs +2 -1
  25. package/dist/src-C7d6HaTp.cjs +19 -0
  26. package/dist/src-DVpoP03o.mjs +13 -0
  27. package/dist/{upstash-_05vyc5o.mjs → upstash-CXy_duOa.mjs} +5 -0
  28. package/dist/{upstash-DiFDpOHy.cjs → upstash-u8Hs4sZG.cjs} +5 -0
  29. package/dist/upstash.cjs +1 -1
  30. package/dist/upstash.d.cts +2 -1
  31. package/dist/upstash.d.mts +2 -1
  32. package/dist/upstash.mjs +1 -1
  33. package/dist/{vi.bdSIJ99Y-CKSnd2bq.cjs → vi.bdSIJ99Y-B-qkABPq.cjs} +1 -1
  34. package/dist/{vi.bdSIJ99Y-DffpE2Zw.mjs → vi.bdSIJ99Y-CB5havZF.mjs} +1 -1
  35. package/package.json +1 -1
  36. package/src/expo.ts +8 -10
  37. package/src/index.ts +25 -0
  38. package/src/memory.ts +22 -2
  39. package/src/upstash.ts +10 -0
  40. package/dist/memory-YteQXj7m.mjs +0 -16
  41. package/dist/memory-n6Iosq6J.cjs +0 -22
  42. /package/dist/{magic-string.es-BvJNBYzY.mjs → magic-string.es-CBih0MJw.mjs} +0 -0
  43. /package/dist/{magic-string.es-CKiDiYoS.cjs → magic-string.es-KiPEzMtt.cjs} +0 -0
@@ -1,6 +1,7 @@
1
- const require_memory = require('../memory-n6Iosq6J.cjs');
2
- const require_upstash = require('../upstash-DiFDpOHy.cjs');
3
- const require_vi_bdSIJ99Y = require('../vi.bdSIJ99Y-CKSnd2bq.cjs');
1
+ require('../src-C7d6HaTp.cjs');
2
+ const require_memory = require('../memory-Csy6J-Ak.cjs');
3
+ const require_upstash = require('../upstash-u8Hs4sZG.cjs');
4
+ const require_vi_bdSIJ99Y = require('../vi.bdSIJ99Y-B-qkABPq.cjs');
4
5
 
5
6
  //#region src/__tests__/cache.spec.ts
6
7
  require_vi_bdSIJ99Y.describe("Cache Interface", () => {
@@ -1,6 +1,7 @@
1
- import { describe, globalExpect, it } from "../vi.bdSIJ99Y-DffpE2Zw.mjs";
2
- import { InMemoryCache } from "../memory-YteQXj7m.mjs";
3
- import { UpstashCache } from "../upstash-_05vyc5o.mjs";
1
+ import { describe, globalExpect, it } from "../vi.bdSIJ99Y-CB5havZF.mjs";
2
+ import "../src-DVpoP03o.mjs";
3
+ import { InMemoryCache } from "../memory-QDTnSEX5.mjs";
4
+ import { UpstashCache } from "../upstash-CXy_duOa.mjs";
4
5
 
5
6
  //#region src/__tests__/cache.spec.ts
6
7
  describe("Cache Interface", () => {
@@ -1,5 +1,6 @@
1
- const require_memory = require('../memory-n6Iosq6J.cjs');
2
- const require_vi_bdSIJ99Y = require('../vi.bdSIJ99Y-CKSnd2bq.cjs');
1
+ require('../src-C7d6HaTp.cjs');
2
+ const require_memory = require('../memory-Csy6J-Ak.cjs');
3
+ const require_vi_bdSIJ99Y = require('../vi.bdSIJ99Y-B-qkABPq.cjs');
3
4
 
4
5
  //#region src/__tests__/integration.spec.ts
5
6
  require_vi_bdSIJ99Y.describe("Cache Integration Tests", () => {
@@ -1,5 +1,6 @@
1
- import { beforeEach, describe, globalExpect, it } from "../vi.bdSIJ99Y-DffpE2Zw.mjs";
2
- import { InMemoryCache } from "../memory-YteQXj7m.mjs";
1
+ import { beforeEach, describe, globalExpect, it } from "../vi.bdSIJ99Y-CB5havZF.mjs";
2
+ import "../src-DVpoP03o.mjs";
3
+ import { InMemoryCache } from "../memory-QDTnSEX5.mjs";
3
4
 
4
5
  //#region src/__tests__/integration.spec.ts
5
6
  describe("Cache Integration Tests", () => {
@@ -1,5 +1,6 @@
1
- const require_memory = require('../memory-n6Iosq6J.cjs');
2
- const require_vi_bdSIJ99Y = require('../vi.bdSIJ99Y-CKSnd2bq.cjs');
1
+ require('../src-C7d6HaTp.cjs');
2
+ const require_memory = require('../memory-Csy6J-Ak.cjs');
3
+ const require_vi_bdSIJ99Y = require('../vi.bdSIJ99Y-B-qkABPq.cjs');
3
4
 
4
5
  //#region src/__tests__/memory.spec.ts
5
6
  require_vi_bdSIJ99Y.describe("InMemoryCache", () => {
@@ -1,5 +1,6 @@
1
- import { beforeEach, describe, globalExpect, it } from "../vi.bdSIJ99Y-DffpE2Zw.mjs";
2
- import { InMemoryCache } from "../memory-YteQXj7m.mjs";
1
+ import { beforeEach, describe, globalExpect, it } from "../vi.bdSIJ99Y-CB5havZF.mjs";
2
+ import "../src-DVpoP03o.mjs";
3
+ import { InMemoryCache } from "../memory-QDTnSEX5.mjs";
3
4
 
4
5
  //#region src/__tests__/memory.spec.ts
5
6
  describe("InMemoryCache", () => {
@@ -1,5 +1,5 @@
1
- const require_upstash = require('../upstash-DiFDpOHy.cjs');
2
- const require_vi_bdSIJ99Y = require('../vi.bdSIJ99Y-CKSnd2bq.cjs');
1
+ const require_upstash = require('../upstash-u8Hs4sZG.cjs');
2
+ const require_vi_bdSIJ99Y = require('../vi.bdSIJ99Y-B-qkABPq.cjs');
3
3
 
4
4
  //#region src/__tests__/upstash.spec.ts
5
5
  require_vi_bdSIJ99Y.describe("UpstashCache", () => {
@@ -1,5 +1,5 @@
1
- import { afterEach, beforeEach, describe, globalExpect, it } from "../vi.bdSIJ99Y-DffpE2Zw.mjs";
2
- import { UpstashCache } from "../upstash-_05vyc5o.mjs";
1
+ import { afterEach, beforeEach, describe, globalExpect, it } from "../vi.bdSIJ99Y-CB5havZF.mjs";
2
+ import { UpstashCache } from "../upstash-CXy_duOa.mjs";
3
3
 
4
4
  //#region src/__tests__/upstash.spec.ts
5
5
  describe("UpstashCache", () => {
package/dist/expo.cjs CHANGED
@@ -1,4 +1,5 @@
1
1
  const require_chunk = require('./chunk-DWy1uDak.cjs');
2
+ const require_src = require('./src-C7d6HaTp.cjs');
2
3
  const date_fns = require_chunk.__toESM(require("date-fns"));
3
4
  const expo_secure_store = require_chunk.__toESM(require("expo-secure-store"));
4
5
 
@@ -7,24 +8,23 @@ var ExpoSecureCache = class ExpoSecureCache {
7
8
  static getExpiryKey(key) {
8
9
  return `${key}:expiresAt`;
9
10
  }
10
- async getExpiration(key) {
11
+ async ttl(key) {
11
12
  const result = await expo_secure_store.getItemAsync(ExpoSecureCache.getExpiryKey(key));
12
13
  if (!result) return 0;
13
- const date = new Date(JSON.parse(result));
14
- const secondsLeft = (0, date_fns.differenceInSeconds)(date, /* @__PURE__ */ new Date());
14
+ const secondsLeft = require_src.getExpirationInSeconds(result);
15
15
  return Math.max(secondsLeft, 0);
16
16
  }
17
17
  async get(key) {
18
18
  const result = await expo_secure_store.getItemAsync(key);
19
19
  if (!result) return void 0;
20
- const expiresAt = await this.getExpiration(key);
20
+ const expiresAt = await this.ttl(key);
21
21
  if (expiresAt === 0) return void 0;
22
22
  return JSON.parse(result);
23
23
  }
24
- async set(key, value, ttl) {
25
- const expiresAt = ttl ? Date.now() + ttl * 1e3 : 0;
24
+ async set(key, value, ttl = 600) {
25
+ const expiresAt = (0, date_fns.addSeconds)(/* @__PURE__ */ new Date(), ttl).toISOString();
26
26
  await expo_secure_store.setItemAsync(key, JSON.stringify(value));
27
- await expo_secure_store.setItemAsync(ExpoSecureCache.getExpiryKey(key), JSON.stringify(expiresAt));
27
+ await expo_secure_store.setItemAsync(ExpoSecureCache.getExpiryKey(key), expiresAt);
28
28
  }
29
29
  async delete(key) {
30
30
  await expo_secure_store.deleteItemAsync(key);
package/dist/expo.d.cts CHANGED
@@ -1,9 +1,9 @@
1
- import { Cache } from "./index-q3xYn8_0.cjs";
1
+ import { Cache } from "./index-B6eF5Yxi.cjs";
2
2
 
3
3
  //#region src/expo.d.ts
4
4
  declare class ExpoSecureCache<T> implements Cache<T> {
5
5
  static getExpiryKey(key: string): string;
6
- private getExpiration;
6
+ ttl(key: string): Promise<number>;
7
7
  get(key: string): Promise<T | undefined>;
8
8
  set(key: string, value: T, ttl?: number): Promise<void>;
9
9
  delete(key: string): Promise<void>;
package/dist/expo.d.mts CHANGED
@@ -1,9 +1,9 @@
1
- import { Cache } from "./index-CVen_7Cc.mjs";
1
+ import { Cache } from "./index-Bv61mxVT.mjs";
2
2
 
3
3
  //#region src/expo.d.ts
4
4
  declare class ExpoSecureCache<T> implements Cache<T> {
5
5
  static getExpiryKey(key: string): string;
6
- private getExpiration;
6
+ ttl(key: string): Promise<number>;
7
7
  get(key: string): Promise<T | undefined>;
8
8
  set(key: string, value: T, ttl?: number): Promise<void>;
9
9
  delete(key: string): Promise<void>;
package/dist/expo.mjs CHANGED
@@ -1,4 +1,5 @@
1
- import { differenceInSeconds } from "date-fns";
1
+ import { getExpirationInSeconds } from "./src-DVpoP03o.mjs";
2
+ import { addSeconds } from "date-fns";
2
3
  import * as SecureStore from "expo-secure-store";
3
4
 
4
5
  //#region src/expo.ts
@@ -6,24 +7,23 @@ var ExpoSecureCache = class ExpoSecureCache {
6
7
  static getExpiryKey(key) {
7
8
  return `${key}:expiresAt`;
8
9
  }
9
- async getExpiration(key) {
10
+ async ttl(key) {
10
11
  const result = await SecureStore.getItemAsync(ExpoSecureCache.getExpiryKey(key));
11
12
  if (!result) return 0;
12
- const date = new Date(JSON.parse(result));
13
- const secondsLeft = differenceInSeconds(date, /* @__PURE__ */ new Date());
13
+ const secondsLeft = getExpirationInSeconds(result);
14
14
  return Math.max(secondsLeft, 0);
15
15
  }
16
16
  async get(key) {
17
17
  const result = await SecureStore.getItemAsync(key);
18
18
  if (!result) return void 0;
19
- const expiresAt = await this.getExpiration(key);
19
+ const expiresAt = await this.ttl(key);
20
20
  if (expiresAt === 0) return void 0;
21
21
  return JSON.parse(result);
22
22
  }
23
- async set(key, value, ttl) {
24
- const expiresAt = ttl ? Date.now() + ttl * 1e3 : 0;
23
+ async set(key, value, ttl = 600) {
24
+ const expiresAt = addSeconds(/* @__PURE__ */ new Date(), ttl).toISOString();
25
25
  await SecureStore.setItemAsync(key, JSON.stringify(value));
26
- await SecureStore.setItemAsync(ExpoSecureCache.getExpiryKey(key), JSON.stringify(expiresAt));
26
+ await SecureStore.setItemAsync(ExpoSecureCache.getExpiryKey(key), expiresAt);
27
27
  }
28
28
  async delete(key) {
29
29
  await SecureStore.deleteItemAsync(key);
@@ -1,4 +1,5 @@
1
1
  //#region src/index.d.ts
2
+ declare const getExpirationInSeconds: (expiresAt: string | number | Date | undefined | null) => number;
2
3
  interface Cache<T> {
3
4
  /**
4
5
  * Retrieves a value from the cache.
@@ -20,6 +21,13 @@ interface Cache<T> {
20
21
  * @param key The key to delete from the cache.
21
22
  */
22
23
  delete(key: string): Promise<void>;
24
+ /**
25
+ * Retrieves the time-to-live (TTL) for a cached value.
26
+ *
27
+ * @param key The key to retrieve the TTL for.
28
+ * @returns The TTL in seconds, or undefined if the key does not exist.
29
+ */
30
+ ttl(key: string): Promise<number>;
23
31
  }
24
32
  //#endregion
25
- export { Cache };
33
+ export { Cache, getExpirationInSeconds };
@@ -1,4 +1,5 @@
1
1
  //#region src/index.d.ts
2
+ declare const getExpirationInSeconds: (expiresAt: string | number | Date | undefined | null) => number;
2
3
  interface Cache<T> {
3
4
  /**
4
5
  * Retrieves a value from the cache.
@@ -20,6 +21,13 @@ interface Cache<T> {
20
21
  * @param key The key to delete from the cache.
21
22
  */
22
23
  delete(key: string): Promise<void>;
24
+ /**
25
+ * Retrieves the time-to-live (TTL) for a cached value.
26
+ *
27
+ * @param key The key to retrieve the TTL for.
28
+ * @returns The TTL in seconds, or undefined if the key does not exist.
29
+ */
30
+ ttl(key: string): Promise<number>;
23
31
  }
24
32
  //#endregion
25
- export { Cache };
33
+ export { Cache, getExpirationInSeconds };
package/dist/index.cjs CHANGED
@@ -0,0 +1,3 @@
1
+ const require_src = require('./src-C7d6HaTp.cjs');
2
+
3
+ exports.getExpirationInSeconds = require_src.getExpirationInSeconds;
package/dist/index.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- import { Cache } from "./index-q3xYn8_0.cjs";
2
- export { Cache };
1
+ import { Cache, getExpirationInSeconds } from "./index-B6eF5Yxi.cjs";
2
+ export { Cache, getExpirationInSeconds };
package/dist/index.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { Cache } from "./index-CVen_7Cc.mjs";
2
- export { Cache };
1
+ import { Cache, getExpirationInSeconds } from "./index-Bv61mxVT.mjs";
2
+ export { Cache, getExpirationInSeconds };
package/dist/index.mjs CHANGED
@@ -0,0 +1,3 @@
1
+ import { getExpirationInSeconds } from "./src-DVpoP03o.mjs";
2
+
3
+ export { getExpirationInSeconds };
@@ -0,0 +1,39 @@
1
+ const require_chunk = require('./chunk-DWy1uDak.cjs');
2
+ const require_src = require('./src-C7d6HaTp.cjs');
3
+ const date_fns = require_chunk.__toESM(require("date-fns"));
4
+
5
+ //#region src/memory.ts
6
+ var InMemoryCache = class {
7
+ store = /* @__PURE__ */ new Map();
8
+ expirations = /* @__PURE__ */ new Map();
9
+ async get(key) {
10
+ const expiration = await this.ttl(key);
11
+ if (!expiration) {
12
+ await this.delete(key);
13
+ return void 0;
14
+ }
15
+ return this.store.get(key);
16
+ }
17
+ async ttl(key) {
18
+ const expiration = this.expirations.get(key);
19
+ if (!expiration) return 0;
20
+ return require_src.getExpirationInSeconds(expiration);
21
+ }
22
+ async set(key, value, ttl = 600) {
23
+ this.store.set(key, value);
24
+ const now = /* @__PURE__ */ new Date();
25
+ const expiresAt = (0, date_fns.addSeconds)(now, ttl);
26
+ this.expirations.set(key, expiresAt);
27
+ }
28
+ async delete(key) {
29
+ this.store.delete(key);
30
+ }
31
+ };
32
+
33
+ //#endregion
34
+ Object.defineProperty(exports, 'InMemoryCache', {
35
+ enumerable: true,
36
+ get: function () {
37
+ return InMemoryCache;
38
+ }
39
+ });
@@ -0,0 +1,33 @@
1
+ import { getExpirationInSeconds } from "./src-DVpoP03o.mjs";
2
+ import { addSeconds } from "date-fns";
3
+
4
+ //#region src/memory.ts
5
+ var InMemoryCache = class {
6
+ store = /* @__PURE__ */ new Map();
7
+ expirations = /* @__PURE__ */ new Map();
8
+ async get(key) {
9
+ const expiration = await this.ttl(key);
10
+ if (!expiration) {
11
+ await this.delete(key);
12
+ return void 0;
13
+ }
14
+ return this.store.get(key);
15
+ }
16
+ async ttl(key) {
17
+ const expiration = this.expirations.get(key);
18
+ if (!expiration) return 0;
19
+ return getExpirationInSeconds(expiration);
20
+ }
21
+ async set(key, value, ttl = 600) {
22
+ this.store.set(key, value);
23
+ const now = /* @__PURE__ */ new Date();
24
+ const expiresAt = addSeconds(now, ttl);
25
+ this.expirations.set(key, expiresAt);
26
+ }
27
+ async delete(key) {
28
+ this.store.delete(key);
29
+ }
30
+ };
31
+
32
+ //#endregion
33
+ export { InMemoryCache };
package/dist/memory.cjs CHANGED
@@ -1,3 +1,4 @@
1
- const require_memory = require('./memory-n6Iosq6J.cjs');
1
+ require('./src-C7d6HaTp.cjs');
2
+ const require_memory = require('./memory-Csy6J-Ak.cjs');
2
3
 
3
4
  exports.InMemoryCache = require_memory.InMemoryCache;
package/dist/memory.d.cts CHANGED
@@ -1,10 +1,12 @@
1
- import { Cache } from "./index-q3xYn8_0.cjs";
1
+ import { Cache } from "./index-B6eF5Yxi.cjs";
2
2
 
3
3
  //#region src/memory.d.ts
4
4
  declare class InMemoryCache<T> implements Cache<T> {
5
5
  private store;
6
+ private expirations;
6
7
  get(key: string): Promise<T | undefined>;
7
- set(key: string, value: T): Promise<void>;
8
+ ttl(key: string): Promise<number>;
9
+ set(key: string, value: T, ttl?: number): Promise<void>;
8
10
  delete(key: string): Promise<void>;
9
11
  }
10
12
  //#endregion
package/dist/memory.d.mts CHANGED
@@ -1,10 +1,12 @@
1
- import { Cache } from "./index-CVen_7Cc.mjs";
1
+ import { Cache } from "./index-Bv61mxVT.mjs";
2
2
 
3
3
  //#region src/memory.d.ts
4
4
  declare class InMemoryCache<T> implements Cache<T> {
5
5
  private store;
6
+ private expirations;
6
7
  get(key: string): Promise<T | undefined>;
7
- set(key: string, value: T): Promise<void>;
8
+ ttl(key: string): Promise<number>;
9
+ set(key: string, value: T, ttl?: number): Promise<void>;
8
10
  delete(key: string): Promise<void>;
9
11
  }
10
12
  //#endregion
package/dist/memory.mjs CHANGED
@@ -1,3 +1,4 @@
1
- import { InMemoryCache } from "./memory-YteQXj7m.mjs";
1
+ import "./src-DVpoP03o.mjs";
2
+ import { InMemoryCache } from "./memory-QDTnSEX5.mjs";
2
3
 
3
4
  export { InMemoryCache };
@@ -0,0 +1,19 @@
1
+ const require_chunk = require('./chunk-DWy1uDak.cjs');
2
+ const date_fns = require_chunk.__toESM(require("date-fns"));
3
+
4
+ //#region src/index.ts
5
+ const getExpirationInSeconds = (expiresAt) => {
6
+ if (!expiresAt) return 0;
7
+ const expirationDate = new Date(expiresAt);
8
+ const now = /* @__PURE__ */ new Date();
9
+ const secondsLeft = (0, date_fns.differenceInSeconds)(expirationDate, now);
10
+ return Math.max(secondsLeft, 0);
11
+ };
12
+
13
+ //#endregion
14
+ Object.defineProperty(exports, 'getExpirationInSeconds', {
15
+ enumerable: true,
16
+ get: function () {
17
+ return getExpirationInSeconds;
18
+ }
19
+ });
@@ -0,0 +1,13 @@
1
+ import { differenceInSeconds } from "date-fns";
2
+
3
+ //#region src/index.ts
4
+ const getExpirationInSeconds = (expiresAt) => {
5
+ if (!expiresAt) return 0;
6
+ const expirationDate = new Date(expiresAt);
7
+ const now = /* @__PURE__ */ new Date();
8
+ const secondsLeft = differenceInSeconds(expirationDate, now);
9
+ return Math.max(secondsLeft, 0);
10
+ };
11
+
12
+ //#endregion
13
+ export { getExpirationInSeconds };
@@ -9,6 +9,11 @@ var UpstashCache = class {
9
9
  token
10
10
  });
11
11
  }
12
+ async ttl(key) {
13
+ const ttl = await this.client.ttl(key);
14
+ if (ttl === -2) return 0;
15
+ return ttl;
16
+ }
12
17
  async get(key) {
13
18
  const v = await this.client.get(key);
14
19
  if (v === null) return void 0;
@@ -10,6 +10,11 @@ var UpstashCache = class {
10
10
  token
11
11
  });
12
12
  }
13
+ async ttl(key) {
14
+ const ttl = await this.client.ttl(key);
15
+ if (ttl === -2) return 0;
16
+ return ttl;
17
+ }
13
18
  async get(key) {
14
19
  const v = await this.client.get(key);
15
20
  if (v === null) return void 0;
package/dist/upstash.cjs CHANGED
@@ -1,3 +1,3 @@
1
- const require_upstash = require('./upstash-DiFDpOHy.cjs');
1
+ const require_upstash = require('./upstash-u8Hs4sZG.cjs');
2
2
 
3
3
  exports.UpstashCache = require_upstash.UpstashCache;
@@ -1,9 +1,10 @@
1
- import { Cache } from "./index-q3xYn8_0.cjs";
1
+ import { Cache } from "./index-B6eF5Yxi.cjs";
2
2
 
3
3
  //#region src/upstash.d.ts
4
4
  declare class UpstashCache<T> implements Cache<T> {
5
5
  private client;
6
6
  constructor(url: string, token: string);
7
+ ttl(key: string): Promise<number>;
7
8
  get(key: string): Promise<T | undefined>;
8
9
  set(key: string, value: T, ttl?: number): Promise<void>;
9
10
  delete(key: string): Promise<void>;
@@ -1,9 +1,10 @@
1
- import { Cache } from "./index-CVen_7Cc.mjs";
1
+ import { Cache } from "./index-Bv61mxVT.mjs";
2
2
 
3
3
  //#region src/upstash.d.ts
4
4
  declare class UpstashCache<T> implements Cache<T> {
5
5
  private client;
6
6
  constructor(url: string, token: string);
7
+ ttl(key: string): Promise<number>;
7
8
  get(key: string): Promise<T | undefined>;
8
9
  set(key: string, value: T, ttl?: number): Promise<void>;
9
10
  delete(key: string): Promise<void>;
package/dist/upstash.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { UpstashCache } from "./upstash-_05vyc5o.mjs";
1
+ import { UpstashCache } from "./upstash-CXy_duOa.mjs";
2
2
 
3
3
  export { UpstashCache };
@@ -11570,7 +11570,7 @@ function offsetToLineNumber(source, offset) {
11570
11570
  return line + 1;
11571
11571
  }
11572
11572
  async function saveInlineSnapshots(environment, snapshots) {
11573
- const MagicString = (await Promise.resolve().then(() => require("./magic-string.es-CKiDiYoS.cjs"))).default;
11573
+ const MagicString = (await Promise.resolve().then(() => require("./magic-string.es-KiPEzMtt.cjs"))).default;
11574
11574
  const files = new Set(snapshots.map((i) => i.file));
11575
11575
  await Promise.all(Array.from(files).map(async (file) => {
11576
11576
  const snaps = snapshots.filter((i) => i.file === file);
@@ -11595,7 +11595,7 @@ function offsetToLineNumber(source, offset) {
11595
11595
  return line + 1;
11596
11596
  }
11597
11597
  async function saveInlineSnapshots(environment, snapshots) {
11598
- const MagicString = (await import("./magic-string.es-BvJNBYzY.mjs")).default;
11598
+ const MagicString = (await import("./magic-string.es-CBih0MJw.mjs")).default;
11599
11599
  const files = new Set(snapshots.map((i) => i.file));
11600
11600
  await Promise.all(Array.from(files).map(async (file) => {
11601
11601
  const snaps = snapshots.filter((i) => i.file === file);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geekmidas/cache",
3
- "version": "0.0.5",
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",
package/src/expo.ts CHANGED
@@ -1,13 +1,13 @@
1
- import { differenceInSeconds } from 'date-fns';
1
+ import { addSeconds } from 'date-fns';
2
2
  import * as SecureStore from 'expo-secure-store';
3
- import type { Cache } from './index';
3
+ import { type Cache, getExpirationInSeconds } from './index';
4
4
 
5
5
  export class ExpoSecureCache<T> implements Cache<T> {
6
6
  static getExpiryKey(key: string): string {
7
7
  return `${key}:expiresAt`;
8
8
  }
9
9
 
10
- private async getExpiration(key: string): Promise<number> {
10
+ async ttl(key: string): Promise<number> {
11
11
  const result = await SecureStore.getItemAsync(
12
12
  ExpoSecureCache.getExpiryKey(key),
13
13
  );
@@ -15,9 +15,7 @@ export class ExpoSecureCache<T> implements Cache<T> {
15
15
  return 0;
16
16
  }
17
17
 
18
- const date = new Date(JSON.parse(result));
19
-
20
- const secondsLeft = differenceInSeconds(date, new Date());
18
+ const secondsLeft = getExpirationInSeconds(result);
21
19
 
22
20
  return Math.max(secondsLeft, 0);
23
21
  }
@@ -29,7 +27,7 @@ export class ExpoSecureCache<T> implements Cache<T> {
29
27
  return undefined;
30
28
  }
31
29
 
32
- const expiresAt = await this.getExpiration(key);
30
+ const expiresAt = await this.ttl(key);
33
31
  if (expiresAt === 0) {
34
32
  return undefined;
35
33
  }
@@ -37,12 +35,12 @@ export class ExpoSecureCache<T> implements Cache<T> {
37
35
  return JSON.parse(result) as T;
38
36
  }
39
37
 
40
- async set(key: string, value: T, ttl?: number): Promise<void> {
41
- const expiresAt = ttl ? Date.now() + ttl * 1000 : 0;
38
+ async set(key: string, value: T, ttl: number = 600): Promise<void> {
39
+ const expiresAt = addSeconds(new Date(), ttl).toISOString();
42
40
  await SecureStore.setItemAsync(key, JSON.stringify(value));
43
41
  await SecureStore.setItemAsync(
44
42
  ExpoSecureCache.getExpiryKey(key),
45
- JSON.stringify(expiresAt),
43
+ expiresAt,
46
44
  );
47
45
  }
48
46
 
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
 
@@ -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
- });