@geekmidas/cache 0.0.7 → 0.1.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.
Files changed (63) hide show
  1. package/dist/{chunk-DWy1uDak.cjs → chunk-CUT6urMc.cjs} +0 -9
  2. package/dist/expo.cjs +4 -3
  3. package/dist/expo.cjs.map +1 -0
  4. package/dist/expo.d.cts +6 -5
  5. package/dist/expo.d.mts +6 -5
  6. package/dist/expo.mjs +3 -2
  7. package/dist/expo.mjs.map +1 -0
  8. package/dist/{index-B6eF5Yxi.d.cts → index-CExVL1mV.d.cts} +7 -5
  9. package/dist/{index-Bv61mxVT.d.mts → index-M02PehQA.d.mts} +7 -5
  10. package/dist/index.cjs +1 -1
  11. package/dist/index.d.cts +1 -1
  12. package/dist/index.d.mts +1 -1
  13. package/dist/index.mjs +1 -1
  14. package/dist/memory.cjs +35 -3
  15. package/dist/memory.cjs.map +1 -0
  16. package/dist/memory.d.cts +6 -5
  17. package/dist/memory.d.mts +6 -5
  18. package/dist/memory.mjs +34 -3
  19. package/dist/memory.mjs.map +1 -0
  20. package/dist/{src-DVpoP03o.mjs → src-CrE4Nl5T.mjs} +2 -1
  21. package/dist/src-CrE4Nl5T.mjs.map +1 -0
  22. package/dist/{src-C7d6HaTp.cjs → src-zfYb7mdS.cjs} +3 -2
  23. package/dist/src-zfYb7mdS.cjs.map +1 -0
  24. package/dist/upstash.cjs +32 -2
  25. package/dist/upstash.cjs.map +1 -0
  26. package/dist/upstash.d.cts +6 -5
  27. package/dist/upstash.d.mts +6 -5
  28. package/dist/upstash.mjs +31 -2
  29. package/dist/upstash.mjs.map +1 -0
  30. package/package.json +6 -2
  31. package/src/__benchmarks__/memory.bench.ts +51 -0
  32. package/src/__tests__/cache.spec.ts +31 -35
  33. package/src/__tests__/integration.spec.ts +32 -23
  34. package/src/__tests__/memory.spec.ts +1 -1
  35. package/src/__tests__/upstash.spec.ts +1 -1
  36. package/src/expo.ts +3 -4
  37. package/src/index.ts +5 -4
  38. package/src/memory.ts +6 -5
  39. package/src/upstash.ts +4 -4
  40. package/dist/__tests__/cache.spec.cjs +0 -77
  41. package/dist/__tests__/cache.spec.d.cts +0 -1
  42. package/dist/__tests__/cache.spec.d.mts +0 -1
  43. package/dist/__tests__/cache.spec.mjs +0 -77
  44. package/dist/__tests__/integration.spec.cjs +0 -235
  45. package/dist/__tests__/integration.spec.d.cts +0 -1
  46. package/dist/__tests__/integration.spec.d.mts +0 -1
  47. package/dist/__tests__/integration.spec.mjs +0 -235
  48. package/dist/__tests__/memory.spec.cjs +0 -126
  49. package/dist/__tests__/memory.spec.d.cts +0 -1
  50. package/dist/__tests__/memory.spec.d.mts +0 -1
  51. package/dist/__tests__/memory.spec.mjs +0 -126
  52. package/dist/__tests__/upstash.spec.cjs +0 -201
  53. package/dist/__tests__/upstash.spec.d.cts +0 -1
  54. package/dist/__tests__/upstash.spec.d.mts +0 -1
  55. package/dist/__tests__/upstash.spec.mjs +0 -201
  56. package/dist/magic-string.es-CBih0MJw.mjs +0 -1014
  57. package/dist/magic-string.es-KiPEzMtt.cjs +0 -1015
  58. package/dist/memory-Csy6J-Ak.cjs +0 -39
  59. package/dist/memory-QDTnSEX5.mjs +0 -33
  60. package/dist/upstash-CXy_duOa.mjs +0 -31
  61. package/dist/upstash-u8Hs4sZG.cjs +0 -37
  62. package/dist/vi.bdSIJ99Y-B-qkABPq.cjs +0 -14955
  63. package/dist/vi.bdSIJ99Y-CB5havZF.mjs +0 -14951
@@ -5,9 +5,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __commonJS = (cb, mod) => function() {
9
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
- };
11
8
  var __copyProps = (to, from, except, desc) => {
12
9
  if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
13
10
  key = keys[i];
@@ -25,12 +22,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
25
22
 
26
23
  //#endregion
27
24
 
28
- Object.defineProperty(exports, '__commonJS', {
29
- enumerable: true,
30
- get: function () {
31
- return __commonJS;
32
- }
33
- });
34
25
  Object.defineProperty(exports, '__toESM', {
35
26
  enumerable: true,
36
27
  get: function () {
package/dist/expo.cjs CHANGED
@@ -1,5 +1,5 @@
1
- const require_chunk = require('./chunk-DWy1uDak.cjs');
2
- const require_src = require('./src-C7d6HaTp.cjs');
1
+ const require_chunk = require('./chunk-CUT6urMc.cjs');
2
+ const require_src = require('./src-zfYb7mdS.cjs');
3
3
  const date_fns = require_chunk.__toESM(require("date-fns"));
4
4
  const expo_secure_store = require_chunk.__toESM(require("expo-secure-store"));
5
5
 
@@ -33,4 +33,5 @@ var ExpoSecureCache = class ExpoSecureCache {
33
33
  };
34
34
 
35
35
  //#endregion
36
- exports.ExpoSecureCache = ExpoSecureCache;
36
+ exports.ExpoSecureCache = ExpoSecureCache;
37
+ //# sourceMappingURL=expo.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expo.cjs","names":["key: string","value: T","ttl: number"],"sources":["../src/expo.ts"],"sourcesContent":["import { addSeconds } from 'date-fns';\nimport * as SecureStore from 'expo-secure-store';\nimport { type Cache, getExpirationInSeconds } from './index';\n\nexport class ExpoSecureCache implements Cache {\n static getExpiryKey(key: string): string {\n return `${key}:expiresAt`;\n }\n\n async ttl(key: string): Promise<number> {\n const result = await SecureStore.getItemAsync(\n ExpoSecureCache.getExpiryKey(key),\n );\n if (!result) {\n return 0;\n }\n\n const secondsLeft = getExpirationInSeconds(result);\n\n return Math.max(secondsLeft, 0);\n }\n\n async get<T>(key: string): Promise<T | undefined> {\n const result = await SecureStore.getItemAsync(key);\n if (!result) {\n return undefined;\n }\n\n const expiresAt = await this.ttl(key);\n if (expiresAt === 0) {\n return undefined;\n }\n\n return JSON.parse(result) as T;\n }\n\n async set<T>(key: string, value: T, ttl: number = 600): Promise<void> {\n const expiresAt = addSeconds(new Date(), ttl).toISOString();\n await SecureStore.setItemAsync(key, JSON.stringify(value));\n await SecureStore.setItemAsync(\n ExpoSecureCache.getExpiryKey(key),\n expiresAt,\n );\n }\n\n async delete(key: string): Promise<void> {\n await SecureStore.deleteItemAsync(key);\n await SecureStore.deleteItemAsync(ExpoSecureCache.getExpiryKey(key));\n }\n}\n"],"mappings":";;;;;;AAIA,IAAa,kBAAb,MAAa,gBAAiC;CAC5C,OAAO,aAAaA,KAAqB;AACvC,UAAQ,EAAE,IAAI;CACf;CAED,MAAM,IAAIA,KAA8B;EACtC,MAAM,SAAS,MAAM,kBAAY,aAC/B,gBAAgB,aAAa,IAAI,CAClC;AACD,OAAK,OACH,QAAO;EAGT,MAAM,cAAc,mCAAuB,OAAO;AAElD,SAAO,KAAK,IAAI,aAAa,EAAE;CAChC;CAED,MAAM,IAAOA,KAAqC;EAChD,MAAM,SAAS,MAAM,kBAAY,aAAa,IAAI;AAClD,OAAK,OACH;EAGF,MAAM,YAAY,MAAM,KAAK,IAAI,IAAI;AACrC,MAAI,cAAc,EAChB;AAGF,SAAO,KAAK,MAAM,OAAO;CAC1B;CAED,MAAM,IAAOA,KAAaC,OAAUC,MAAc,KAAoB;EACpE,MAAM,YAAY,yCAAW,IAAI,QAAQ,IAAI,CAAC,aAAa;AAC3D,QAAM,kBAAY,aAAa,KAAK,KAAK,UAAU,MAAM,CAAC;AAC1D,QAAM,kBAAY,aAChB,gBAAgB,aAAa,IAAI,EACjC,UACD;CACF;CAED,MAAM,OAAOF,KAA4B;AACvC,QAAM,kBAAY,gBAAgB,IAAI;AACtC,QAAM,kBAAY,gBAAgB,gBAAgB,aAAa,IAAI,CAAC;CACrE;AACF"}
package/dist/expo.d.cts CHANGED
@@ -1,12 +1,13 @@
1
- import { Cache } from "./index-B6eF5Yxi.cjs";
1
+ import { Cache } from "./index-CExVL1mV.cjs";
2
2
 
3
3
  //#region src/expo.d.ts
4
- declare class ExpoSecureCache<T> implements Cache<T> {
4
+ declare class ExpoSecureCache implements Cache {
5
5
  static getExpiryKey(key: string): string;
6
6
  ttl(key: string): Promise<number>;
7
- get(key: string): Promise<T | undefined>;
8
- set(key: string, value: T, ttl?: number): Promise<void>;
7
+ get<T>(key: string): Promise<T | undefined>;
8
+ set<T>(key: string, value: T, ttl?: number): Promise<void>;
9
9
  delete(key: string): Promise<void>;
10
10
  }
11
11
  //#endregion
12
- export { ExpoSecureCache };
12
+ export { ExpoSecureCache };
13
+ //# sourceMappingURL=expo.d.cts.map
package/dist/expo.d.mts CHANGED
@@ -1,12 +1,13 @@
1
- import { Cache } from "./index-Bv61mxVT.mjs";
1
+ import { Cache } from "./index-M02PehQA.mjs";
2
2
 
3
3
  //#region src/expo.d.ts
4
- declare class ExpoSecureCache<T> implements Cache<T> {
4
+ declare class ExpoSecureCache implements Cache {
5
5
  static getExpiryKey(key: string): string;
6
6
  ttl(key: string): Promise<number>;
7
- get(key: string): Promise<T | undefined>;
8
- set(key: string, value: T, ttl?: number): Promise<void>;
7
+ get<T>(key: string): Promise<T | undefined>;
8
+ set<T>(key: string, value: T, ttl?: number): Promise<void>;
9
9
  delete(key: string): Promise<void>;
10
10
  }
11
11
  //#endregion
12
- export { ExpoSecureCache };
12
+ export { ExpoSecureCache };
13
+ //# sourceMappingURL=expo.d.mts.map
package/dist/expo.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { getExpirationInSeconds } from "./src-DVpoP03o.mjs";
1
+ import { getExpirationInSeconds } from "./src-CrE4Nl5T.mjs";
2
2
  import { addSeconds } from "date-fns";
3
3
  import * as SecureStore from "expo-secure-store";
4
4
 
@@ -32,4 +32,5 @@ var ExpoSecureCache = class ExpoSecureCache {
32
32
  };
33
33
 
34
34
  //#endregion
35
- export { ExpoSecureCache };
35
+ export { ExpoSecureCache };
36
+ //# sourceMappingURL=expo.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expo.mjs","names":["key: string","value: T","ttl: number"],"sources":["../src/expo.ts"],"sourcesContent":["import { addSeconds } from 'date-fns';\nimport * as SecureStore from 'expo-secure-store';\nimport { type Cache, getExpirationInSeconds } from './index';\n\nexport class ExpoSecureCache implements Cache {\n static getExpiryKey(key: string): string {\n return `${key}:expiresAt`;\n }\n\n async ttl(key: string): Promise<number> {\n const result = await SecureStore.getItemAsync(\n ExpoSecureCache.getExpiryKey(key),\n );\n if (!result) {\n return 0;\n }\n\n const secondsLeft = getExpirationInSeconds(result);\n\n return Math.max(secondsLeft, 0);\n }\n\n async get<T>(key: string): Promise<T | undefined> {\n const result = await SecureStore.getItemAsync(key);\n if (!result) {\n return undefined;\n }\n\n const expiresAt = await this.ttl(key);\n if (expiresAt === 0) {\n return undefined;\n }\n\n return JSON.parse(result) as T;\n }\n\n async set<T>(key: string, value: T, ttl: number = 600): Promise<void> {\n const expiresAt = addSeconds(new Date(), ttl).toISOString();\n await SecureStore.setItemAsync(key, JSON.stringify(value));\n await SecureStore.setItemAsync(\n ExpoSecureCache.getExpiryKey(key),\n expiresAt,\n );\n }\n\n async delete(key: string): Promise<void> {\n await SecureStore.deleteItemAsync(key);\n await SecureStore.deleteItemAsync(ExpoSecureCache.getExpiryKey(key));\n }\n}\n"],"mappings":";;;;;AAIA,IAAa,kBAAb,MAAa,gBAAiC;CAC5C,OAAO,aAAaA,KAAqB;AACvC,UAAQ,EAAE,IAAI;CACf;CAED,MAAM,IAAIA,KAA8B;EACtC,MAAM,SAAS,MAAM,YAAY,aAC/B,gBAAgB,aAAa,IAAI,CAClC;AACD,OAAK,OACH,QAAO;EAGT,MAAM,cAAc,uBAAuB,OAAO;AAElD,SAAO,KAAK,IAAI,aAAa,EAAE;CAChC;CAED,MAAM,IAAOA,KAAqC;EAChD,MAAM,SAAS,MAAM,YAAY,aAAa,IAAI;AAClD,OAAK,OACH;EAGF,MAAM,YAAY,MAAM,KAAK,IAAI,IAAI;AACrC,MAAI,cAAc,EAChB;AAGF,SAAO,KAAK,MAAM,OAAO;CAC1B;CAED,MAAM,IAAOA,KAAaC,OAAUC,MAAc,KAAoB;EACpE,MAAM,YAAY,2BAAW,IAAI,QAAQ,IAAI,CAAC,aAAa;AAC3D,QAAM,YAAY,aAAa,KAAK,KAAK,UAAU,MAAM,CAAC;AAC1D,QAAM,YAAY,aAChB,gBAAgB,aAAa,IAAI,EACjC,UACD;CACF;CAED,MAAM,OAAOF,KAA4B;AACvC,QAAM,YAAY,gBAAgB,IAAI;AACtC,QAAM,YAAY,gBAAgB,gBAAgB,aAAa,IAAI,CAAC;CACrE;AACF"}
@@ -1,12 +1,13 @@
1
1
  //#region src/index.d.ts
2
2
  declare const getExpirationInSeconds: (expiresAt: string | number | Date | undefined | null) => number;
3
- interface Cache<T> {
3
+ interface Cache {
4
4
  /**
5
5
  * Retrieves a value from the cache.
6
6
  *
7
7
  * @param key The key to retrieve the value for.
8
+ * @returns The cached value, or undefined if not found.
8
9
  */
9
- get(key: string): Promise<T | undefined>;
10
+ get<T>(key: string): Promise<T | undefined>;
10
11
  /**
11
12
  * Sets a value in the cache.
12
13
  *
@@ -14,7 +15,7 @@ interface Cache<T> {
14
15
  * @param value The value to cache.
15
16
  * @param ttl Optional time-to-live in seconds. If not provided, the value will not expire.
16
17
  */
17
- set(key: string, value: T, ttl?: number): Promise<void>;
18
+ set<T>(key: string, value: T, ttl?: number): Promise<void>;
18
19
  /**
19
20
  * Deletes a value from the cache.
20
21
  *
@@ -25,9 +26,10 @@ interface Cache<T> {
25
26
  * Retrieves the time-to-live (TTL) for a cached value.
26
27
  *
27
28
  * @param key The key to retrieve the TTL for.
28
- * @returns The TTL in seconds, or undefined if the key does not exist.
29
+ * @returns The TTL in seconds, or 0 if the key does not exist.
29
30
  */
30
31
  ttl(key: string): Promise<number>;
31
32
  }
32
33
  //#endregion
33
- export { Cache, getExpirationInSeconds };
34
+ export { Cache, getExpirationInSeconds };
35
+ //# sourceMappingURL=index-CExVL1mV.d.cts.map
@@ -1,12 +1,13 @@
1
1
  //#region src/index.d.ts
2
2
  declare const getExpirationInSeconds: (expiresAt: string | number | Date | undefined | null) => number;
3
- interface Cache<T> {
3
+ interface Cache {
4
4
  /**
5
5
  * Retrieves a value from the cache.
6
6
  *
7
7
  * @param key The key to retrieve the value for.
8
+ * @returns The cached value, or undefined if not found.
8
9
  */
9
- get(key: string): Promise<T | undefined>;
10
+ get<T>(key: string): Promise<T | undefined>;
10
11
  /**
11
12
  * Sets a value in the cache.
12
13
  *
@@ -14,7 +15,7 @@ interface Cache<T> {
14
15
  * @param value The value to cache.
15
16
  * @param ttl Optional time-to-live in seconds. If not provided, the value will not expire.
16
17
  */
17
- set(key: string, value: T, ttl?: number): Promise<void>;
18
+ set<T>(key: string, value: T, ttl?: number): Promise<void>;
18
19
  /**
19
20
  * Deletes a value from the cache.
20
21
  *
@@ -25,9 +26,10 @@ interface Cache<T> {
25
26
  * Retrieves the time-to-live (TTL) for a cached value.
26
27
  *
27
28
  * @param key The key to retrieve the TTL for.
28
- * @returns The TTL in seconds, or undefined if the key does not exist.
29
+ * @returns The TTL in seconds, or 0 if the key does not exist.
29
30
  */
30
31
  ttl(key: string): Promise<number>;
31
32
  }
32
33
  //#endregion
33
- export { Cache, getExpirationInSeconds };
34
+ export { Cache, getExpirationInSeconds };
35
+ //# sourceMappingURL=index-M02PehQA.d.mts.map
package/dist/index.cjs CHANGED
@@ -1,3 +1,3 @@
1
- const require_src = require('./src-C7d6HaTp.cjs');
1
+ const require_src = require('./src-zfYb7mdS.cjs');
2
2
 
3
3
  exports.getExpirationInSeconds = require_src.getExpirationInSeconds;
package/dist/index.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- import { Cache, getExpirationInSeconds } from "./index-B6eF5Yxi.cjs";
1
+ import { Cache, getExpirationInSeconds } from "./index-CExVL1mV.cjs";
2
2
  export { Cache, getExpirationInSeconds };
package/dist/index.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { Cache, getExpirationInSeconds } from "./index-Bv61mxVT.mjs";
1
+ import { Cache, getExpirationInSeconds } from "./index-M02PehQA.mjs";
2
2
  export { Cache, getExpirationInSeconds };
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { getExpirationInSeconds } from "./src-DVpoP03o.mjs";
1
+ import { getExpirationInSeconds } from "./src-CrE4Nl5T.mjs";
2
2
 
3
3
  export { getExpirationInSeconds };
package/dist/memory.cjs CHANGED
@@ -1,4 +1,36 @@
1
- require('./src-C7d6HaTp.cjs');
2
- const require_memory = require('./memory-Csy6J-Ak.cjs');
1
+ const require_chunk = require('./chunk-CUT6urMc.cjs');
2
+ const require_src = require('./src-zfYb7mdS.cjs');
3
+ const date_fns = require_chunk.__toESM(require("date-fns"));
3
4
 
4
- exports.InMemoryCache = require_memory.InMemoryCache;
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
+ this.expirations.delete(key);
31
+ }
32
+ };
33
+
34
+ //#endregion
35
+ exports.InMemoryCache = InMemoryCache;
36
+ //# sourceMappingURL=memory.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.cjs","names":["key: string","value: T","ttl: number"],"sources":["../src/memory.ts"],"sourcesContent":["import { addSeconds } from 'date-fns';\nimport { type Cache, getExpirationInSeconds } from './';\n\nexport class InMemoryCache implements Cache {\n private store: Map<string, unknown> = new Map();\n private expirations: Map<string, Date> = new Map();\n\n async get<T>(key: string): Promise<T | undefined> {\n const expiration = await this.ttl(key);\n if (!expiration) {\n await this.delete(key);\n return undefined;\n }\n\n return this.store.get(key) as T | undefined;\n }\n\n async ttl(key: string): Promise<number> {\n const expiration = this.expirations.get(key);\n if (!expiration) {\n return 0;\n }\n\n return getExpirationInSeconds(expiration);\n }\n\n async set<T>(key: string, value: T, ttl: number = 600): Promise<void> {\n this.store.set(key, value);\n const now = new Date();\n const expiresAt = addSeconds(now, ttl);\n this.expirations.set(key, expiresAt);\n }\n\n async delete(key: string): Promise<void> {\n this.store.delete(key);\n this.expirations.delete(key);\n }\n}\n"],"mappings":";;;;;AAGA,IAAa,gBAAb,MAA4C;CAC1C,AAAQ,wBAA8B,IAAI;CAC1C,AAAQ,8BAAiC,IAAI;CAE7C,MAAM,IAAOA,KAAqC;EAChD,MAAM,aAAa,MAAM,KAAK,IAAI,IAAI;AACtC,OAAK,YAAY;AACf,SAAM,KAAK,OAAO,IAAI;AACtB;EACD;AAED,SAAO,KAAK,MAAM,IAAI,IAAI;CAC3B;CAED,MAAM,IAAIA,KAA8B;EACtC,MAAM,aAAa,KAAK,YAAY,IAAI,IAAI;AAC5C,OAAK,WACH,QAAO;AAGT,SAAO,mCAAuB,WAAW;CAC1C;CAED,MAAM,IAAOA,KAAaC,OAAUC,MAAc,KAAoB;AACpE,OAAK,MAAM,IAAI,KAAK,MAAM;EAC1B,MAAM,sBAAM,IAAI;EAChB,MAAM,YAAY,yBAAW,KAAK,IAAI;AACtC,OAAK,YAAY,IAAI,KAAK,UAAU;CACrC;CAED,MAAM,OAAOF,KAA4B;AACvC,OAAK,MAAM,OAAO,IAAI;AACtB,OAAK,YAAY,OAAO,IAAI;CAC7B;AACF"}
package/dist/memory.d.cts CHANGED
@@ -1,13 +1,14 @@
1
- import { Cache } from "./index-B6eF5Yxi.cjs";
1
+ import { Cache } from "./index-CExVL1mV.cjs";
2
2
 
3
3
  //#region src/memory.d.ts
4
- declare class InMemoryCache<T> implements Cache<T> {
4
+ declare class InMemoryCache implements Cache {
5
5
  private store;
6
6
  private expirations;
7
- get(key: string): Promise<T | undefined>;
7
+ get<T>(key: string): Promise<T | undefined>;
8
8
  ttl(key: string): Promise<number>;
9
- set(key: string, value: T, ttl?: number): Promise<void>;
9
+ set<T>(key: string, value: T, ttl?: number): Promise<void>;
10
10
  delete(key: string): Promise<void>;
11
11
  }
12
12
  //#endregion
13
- export { InMemoryCache };
13
+ export { InMemoryCache };
14
+ //# sourceMappingURL=memory.d.cts.map
package/dist/memory.d.mts CHANGED
@@ -1,13 +1,14 @@
1
- import { Cache } from "./index-Bv61mxVT.mjs";
1
+ import { Cache } from "./index-M02PehQA.mjs";
2
2
 
3
3
  //#region src/memory.d.ts
4
- declare class InMemoryCache<T> implements Cache<T> {
4
+ declare class InMemoryCache implements Cache {
5
5
  private store;
6
6
  private expirations;
7
- get(key: string): Promise<T | undefined>;
7
+ get<T>(key: string): Promise<T | undefined>;
8
8
  ttl(key: string): Promise<number>;
9
- set(key: string, value: T, ttl?: number): Promise<void>;
9
+ set<T>(key: string, value: T, ttl?: number): Promise<void>;
10
10
  delete(key: string): Promise<void>;
11
11
  }
12
12
  //#endregion
13
- export { InMemoryCache };
13
+ export { InMemoryCache };
14
+ //# sourceMappingURL=memory.d.mts.map
package/dist/memory.mjs CHANGED
@@ -1,4 +1,35 @@
1
- import "./src-DVpoP03o.mjs";
2
- import { InMemoryCache } from "./memory-QDTnSEX5.mjs";
1
+ import { getExpirationInSeconds } from "./src-CrE4Nl5T.mjs";
2
+ import { addSeconds } from "date-fns";
3
3
 
4
- export { InMemoryCache };
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
+ this.expirations.delete(key);
30
+ }
31
+ };
32
+
33
+ //#endregion
34
+ export { InMemoryCache };
35
+ //# sourceMappingURL=memory.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.mjs","names":["key: string","value: T","ttl: number"],"sources":["../src/memory.ts"],"sourcesContent":["import { addSeconds } from 'date-fns';\nimport { type Cache, getExpirationInSeconds } from './';\n\nexport class InMemoryCache implements Cache {\n private store: Map<string, unknown> = new Map();\n private expirations: Map<string, Date> = new Map();\n\n async get<T>(key: string): Promise<T | undefined> {\n const expiration = await this.ttl(key);\n if (!expiration) {\n await this.delete(key);\n return undefined;\n }\n\n return this.store.get(key) as T | undefined;\n }\n\n async ttl(key: string): Promise<number> {\n const expiration = this.expirations.get(key);\n if (!expiration) {\n return 0;\n }\n\n return getExpirationInSeconds(expiration);\n }\n\n async set<T>(key: string, value: T, ttl: number = 600): Promise<void> {\n this.store.set(key, value);\n const now = new Date();\n const expiresAt = addSeconds(now, ttl);\n this.expirations.set(key, expiresAt);\n }\n\n async delete(key: string): Promise<void> {\n this.store.delete(key);\n this.expirations.delete(key);\n }\n}\n"],"mappings":";;;;AAGA,IAAa,gBAAb,MAA4C;CAC1C,AAAQ,wBAA8B,IAAI;CAC1C,AAAQ,8BAAiC,IAAI;CAE7C,MAAM,IAAOA,KAAqC;EAChD,MAAM,aAAa,MAAM,KAAK,IAAI,IAAI;AACtC,OAAK,YAAY;AACf,SAAM,KAAK,OAAO,IAAI;AACtB;EACD;AAED,SAAO,KAAK,MAAM,IAAI,IAAI;CAC3B;CAED,MAAM,IAAIA,KAA8B;EACtC,MAAM,aAAa,KAAK,YAAY,IAAI,IAAI;AAC5C,OAAK,WACH,QAAO;AAGT,SAAO,uBAAuB,WAAW;CAC1C;CAED,MAAM,IAAOA,KAAaC,OAAUC,MAAc,KAAoB;AACpE,OAAK,MAAM,IAAI,KAAK,MAAM;EAC1B,MAAM,sBAAM,IAAI;EAChB,MAAM,YAAY,WAAW,KAAK,IAAI;AACtC,OAAK,YAAY,IAAI,KAAK,UAAU;CACrC;CAED,MAAM,OAAOF,KAA4B;AACvC,OAAK,MAAM,OAAO,IAAI;AACtB,OAAK,YAAY,OAAO,IAAI;CAC7B;AACF"}
@@ -10,4 +10,5 @@ const getExpirationInSeconds = (expiresAt) => {
10
10
  };
11
11
 
12
12
  //#endregion
13
- export { getExpirationInSeconds };
13
+ export { getExpirationInSeconds };
14
+ //# sourceMappingURL=src-CrE4Nl5T.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"src-CrE4Nl5T.mjs","names":["expiresAt: string | number | Date | undefined | null"],"sources":["../src/index.ts"],"sourcesContent":["import { differenceInSeconds } from 'date-fns';\n\nexport const getExpirationInSeconds = (\n expiresAt: string | number | Date | undefined | null,\n): number => {\n if (!expiresAt) {\n return 0;\n }\n\n const expirationDate = new Date(expiresAt);\n const now = new Date();\n\n const secondsLeft = differenceInSeconds(expirationDate, now);\n\n return Math.max(secondsLeft, 0);\n};\n\nexport interface Cache {\n /**\n * Retrieves a value from the cache.\n *\n * @param key The key to retrieve the value for.\n * @returns The cached value, or undefined if not found.\n */\n get<T>(key: string): Promise<T | undefined>;\n /**\n * Sets a value in the cache.\n *\n * @param key The key to set the value for.\n * @param value The value to cache.\n * @param ttl Optional time-to-live in seconds. If not provided, the value will not expire.\n */\n set<T>(key: string, value: T, ttl?: number): Promise<void>;\n /**\n * Deletes a value from the cache.\n *\n * @param key The key to delete from the cache.\n */\n delete(key: string): Promise<void>;\n\n /**\n * Retrieves the time-to-live (TTL) for a cached value.\n *\n * @param key The key to retrieve the TTL for.\n * @returns The TTL in seconds, or 0 if the key does not exist.\n */\n ttl(key: string): Promise<number>;\n}\n"],"mappings":";;;AAEA,MAAa,yBAAyB,CACpCA,cACW;AACX,MAAK,UACH,QAAO;CAGT,MAAM,iBAAiB,IAAI,KAAK;CAChC,MAAM,sBAAM,IAAI;CAEhB,MAAM,cAAc,oBAAoB,gBAAgB,IAAI;AAE5D,QAAO,KAAK,IAAI,aAAa,EAAE;AAChC"}
@@ -1,4 +1,4 @@
1
- const require_chunk = require('./chunk-DWy1uDak.cjs');
1
+ const require_chunk = require('./chunk-CUT6urMc.cjs');
2
2
  const date_fns = require_chunk.__toESM(require("date-fns"));
3
3
 
4
4
  //#region src/index.ts
@@ -16,4 +16,5 @@ Object.defineProperty(exports, 'getExpirationInSeconds', {
16
16
  get: function () {
17
17
  return getExpirationInSeconds;
18
18
  }
19
- });
19
+ });
20
+ //# sourceMappingURL=src-zfYb7mdS.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"src-zfYb7mdS.cjs","names":["expiresAt: string | number | Date | undefined | null"],"sources":["../src/index.ts"],"sourcesContent":["import { differenceInSeconds } from 'date-fns';\n\nexport const getExpirationInSeconds = (\n expiresAt: string | number | Date | undefined | null,\n): number => {\n if (!expiresAt) {\n return 0;\n }\n\n const expirationDate = new Date(expiresAt);\n const now = new Date();\n\n const secondsLeft = differenceInSeconds(expirationDate, now);\n\n return Math.max(secondsLeft, 0);\n};\n\nexport interface Cache {\n /**\n * Retrieves a value from the cache.\n *\n * @param key The key to retrieve the value for.\n * @returns The cached value, or undefined if not found.\n */\n get<T>(key: string): Promise<T | undefined>;\n /**\n * Sets a value in the cache.\n *\n * @param key The key to set the value for.\n * @param value The value to cache.\n * @param ttl Optional time-to-live in seconds. If not provided, the value will not expire.\n */\n set<T>(key: string, value: T, ttl?: number): Promise<void>;\n /**\n * Deletes a value from the cache.\n *\n * @param key The key to delete from the cache.\n */\n delete(key: string): Promise<void>;\n\n /**\n * Retrieves the time-to-live (TTL) for a cached value.\n *\n * @param key The key to retrieve the TTL for.\n * @returns The TTL in seconds, or 0 if the key does not exist.\n */\n ttl(key: string): Promise<number>;\n}\n"],"mappings":";;;;AAEA,MAAa,yBAAyB,CACpCA,cACW;AACX,MAAK,UACH,QAAO;CAGT,MAAM,iBAAiB,IAAI,KAAK;CAChC,MAAM,sBAAM,IAAI;CAEhB,MAAM,cAAc,kCAAoB,gBAAgB,IAAI;AAE5D,QAAO,KAAK,IAAI,aAAa,EAAE;AAChC"}
package/dist/upstash.cjs CHANGED
@@ -1,3 +1,33 @@
1
- const require_upstash = require('./upstash-u8Hs4sZG.cjs');
1
+ const require_chunk = require('./chunk-CUT6urMc.cjs');
2
+ const __upstash_redis = require_chunk.__toESM(require("@upstash/redis"));
2
3
 
3
- exports.UpstashCache = require_upstash.UpstashCache;
4
+ //#region src/upstash.ts
5
+ var UpstashCache = class {
6
+ client;
7
+ constructor(url, token) {
8
+ this.client = new __upstash_redis.Redis({
9
+ url,
10
+ token
11
+ });
12
+ }
13
+ async ttl(key) {
14
+ const ttl = await this.client.ttl(key);
15
+ if (ttl === -2) return 0;
16
+ return ttl;
17
+ }
18
+ async get(key) {
19
+ const v = await this.client.get(key);
20
+ if (v === null) return void 0;
21
+ return v;
22
+ }
23
+ async set(key, value, ttl = 3600) {
24
+ await this.client.set(key, value, { ex: ttl });
25
+ }
26
+ async delete(key) {
27
+ await this.client.del(key);
28
+ }
29
+ };
30
+
31
+ //#endregion
32
+ exports.UpstashCache = UpstashCache;
33
+ //# sourceMappingURL=upstash.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upstash.cjs","names":["url: string","token: string","Redis","key: string","value: T"],"sources":["../src/upstash.ts"],"sourcesContent":["import { Redis } from '@upstash/redis';\nimport type { Cache } from './';\n\nexport class UpstashCache implements Cache {\n private client: Redis;\n\n constructor(url: string, token: string) {\n this.client = new Redis({\n url,\n token,\n });\n }\n\n async ttl(key: string): Promise<number> {\n const ttl = await this.client.ttl(key);\n\n if (ttl === -2) {\n return 0; // Key does not exist\n }\n\n return ttl; // Returns TTL in seconds\n }\n\n async get<T>(key: string): Promise<T | undefined> {\n const v = await this.client.get(key);\n\n if (v === null) {\n return undefined;\n }\n\n return v as T;\n }\n\n async set<T>(key: string, value: T, ttl = 3600): Promise<void> {\n await this.client.set(key, value, { ex: ttl });\n }\n\n async delete(key: string): Promise<void> {\n await this.client.del(key);\n }\n}\n"],"mappings":";;;;AAGA,IAAa,eAAb,MAA2C;CACzC,AAAQ;CAER,YAAYA,KAAaC,OAAe;AACtC,OAAK,SAAS,IAAIC,sBAAM;GACtB;GACA;EACD;CACF;CAED,MAAM,IAAIC,KAA8B;EACtC,MAAM,MAAM,MAAM,KAAK,OAAO,IAAI,IAAI;AAEtC,MAAI,QAAQ,GACV,QAAO;AAGT,SAAO;CACR;CAED,MAAM,IAAOA,KAAqC;EAChD,MAAM,IAAI,MAAM,KAAK,OAAO,IAAI,IAAI;AAEpC,MAAI,MAAM,KACR;AAGF,SAAO;CACR;CAED,MAAM,IAAOA,KAAaC,OAAU,MAAM,MAAqB;AAC7D,QAAM,KAAK,OAAO,IAAI,KAAK,OAAO,EAAE,IAAI,IAAK,EAAC;CAC/C;CAED,MAAM,OAAOD,KAA4B;AACvC,QAAM,KAAK,OAAO,IAAI,IAAI;CAC3B;AACF"}
@@ -1,13 +1,14 @@
1
- import { Cache } from "./index-B6eF5Yxi.cjs";
1
+ import { Cache } from "./index-CExVL1mV.cjs";
2
2
 
3
3
  //#region src/upstash.d.ts
4
- declare class UpstashCache<T> implements Cache<T> {
4
+ declare class UpstashCache implements Cache {
5
5
  private client;
6
6
  constructor(url: string, token: string);
7
7
  ttl(key: string): Promise<number>;
8
- get(key: string): Promise<T | undefined>;
9
- set(key: string, value: T, ttl?: number): Promise<void>;
8
+ get<T>(key: string): Promise<T | undefined>;
9
+ set<T>(key: string, value: T, ttl?: number): Promise<void>;
10
10
  delete(key: string): Promise<void>;
11
11
  }
12
12
  //#endregion
13
- export { UpstashCache };
13
+ export { UpstashCache };
14
+ //# sourceMappingURL=upstash.d.cts.map
@@ -1,13 +1,14 @@
1
- import { Cache } from "./index-Bv61mxVT.mjs";
1
+ import { Cache } from "./index-M02PehQA.mjs";
2
2
 
3
3
  //#region src/upstash.d.ts
4
- declare class UpstashCache<T> implements Cache<T> {
4
+ declare class UpstashCache implements Cache {
5
5
  private client;
6
6
  constructor(url: string, token: string);
7
7
  ttl(key: string): Promise<number>;
8
- get(key: string): Promise<T | undefined>;
9
- set(key: string, value: T, ttl?: number): Promise<void>;
8
+ get<T>(key: string): Promise<T | undefined>;
9
+ set<T>(key: string, value: T, ttl?: number): Promise<void>;
10
10
  delete(key: string): Promise<void>;
11
11
  }
12
12
  //#endregion
13
- export { UpstashCache };
13
+ export { UpstashCache };
14
+ //# sourceMappingURL=upstash.d.mts.map
package/dist/upstash.mjs CHANGED
@@ -1,3 +1,32 @@
1
- import { UpstashCache } from "./upstash-CXy_duOa.mjs";
1
+ import { Redis } from "@upstash/redis";
2
2
 
3
- export { UpstashCache };
3
+ //#region src/upstash.ts
4
+ var UpstashCache = class {
5
+ client;
6
+ constructor(url, token) {
7
+ this.client = new Redis({
8
+ url,
9
+ token
10
+ });
11
+ }
12
+ async ttl(key) {
13
+ const ttl = await this.client.ttl(key);
14
+ if (ttl === -2) return 0;
15
+ return ttl;
16
+ }
17
+ async get(key) {
18
+ const v = await this.client.get(key);
19
+ if (v === null) return void 0;
20
+ return v;
21
+ }
22
+ async set(key, value, ttl = 3600) {
23
+ await this.client.set(key, value, { ex: ttl });
24
+ }
25
+ async delete(key) {
26
+ await this.client.del(key);
27
+ }
28
+ };
29
+
30
+ //#endregion
31
+ export { UpstashCache };
32
+ //# sourceMappingURL=upstash.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upstash.mjs","names":["url: string","token: string","key: string","value: T"],"sources":["../src/upstash.ts"],"sourcesContent":["import { Redis } from '@upstash/redis';\nimport type { Cache } from './';\n\nexport class UpstashCache implements Cache {\n private client: Redis;\n\n constructor(url: string, token: string) {\n this.client = new Redis({\n url,\n token,\n });\n }\n\n async ttl(key: string): Promise<number> {\n const ttl = await this.client.ttl(key);\n\n if (ttl === -2) {\n return 0; // Key does not exist\n }\n\n return ttl; // Returns TTL in seconds\n }\n\n async get<T>(key: string): Promise<T | undefined> {\n const v = await this.client.get(key);\n\n if (v === null) {\n return undefined;\n }\n\n return v as T;\n }\n\n async set<T>(key: string, value: T, ttl = 3600): Promise<void> {\n await this.client.set(key, value, { ex: ttl });\n }\n\n async delete(key: string): Promise<void> {\n await this.client.del(key);\n }\n}\n"],"mappings":";;;AAGA,IAAa,eAAb,MAA2C;CACzC,AAAQ;CAER,YAAYA,KAAaC,OAAe;AACtC,OAAK,SAAS,IAAI,MAAM;GACtB;GACA;EACD;CACF;CAED,MAAM,IAAIC,KAA8B;EACtC,MAAM,MAAM,MAAM,KAAK,OAAO,IAAI,IAAI;AAEtC,MAAI,QAAQ,GACV,QAAO;AAGT,SAAO;CACR;CAED,MAAM,IAAOA,KAAqC;EAChD,MAAM,IAAI,MAAM,KAAK,OAAO,IAAI,IAAI;AAEpC,MAAI,MAAM,KACR;AAGF,SAAO;CACR;CAED,MAAM,IAAOA,KAAaC,OAAU,MAAM,MAAqB;AAC7D,QAAM,KAAK,OAAO,IAAI,KAAK,OAAO,EAAE,IAAI,IAAK,EAAC;CAC/C;CAED,MAAM,OAAOD,KAA4B;AACvC,QAAM,KAAK,OAAO,IAAI,IAAI;CAC3B;AACF"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@geekmidas/cache",
3
- "version": "0.0.7",
4
- "description": "Type-safe email client with SMTP support and React templates",
3
+ "version": "0.1.0",
4
+ "description": "Unified cache interface with multiple storage implementations",
5
5
  "private": false,
6
6
  "type": "module",
7
7
  "exports": {
@@ -26,6 +26,10 @@
26
26
  "require": "./dist/expo.cjs"
27
27
  }
28
28
  },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/geekmidas/toolbox"
32
+ },
29
33
  "publishConfig": {
30
34
  "registry": "https://registry.npmjs.org/",
31
35
  "access": "public"
@@ -0,0 +1,51 @@
1
+ import { bench, describe } from 'vitest';
2
+ import { InMemoryCache } from '../memory';
3
+
4
+ describe('InMemoryCache', () => {
5
+ const cache = new InMemoryCache<string>();
6
+
7
+ bench('set with TTL', async () => {
8
+ await cache.set('key', 'value', 3600);
9
+ });
10
+
11
+ bench('get (cache hit)', async () => {
12
+ await cache.set('hit-key', 'value', 3600);
13
+ await cache.get('hit-key');
14
+ });
15
+
16
+ bench('get (cache miss)', async () => {
17
+ await cache.get('nonexistent-key');
18
+ });
19
+
20
+ bench('delete', async () => {
21
+ await cache.set('delete-key', 'value', 3600);
22
+ await cache.delete('delete-key');
23
+ });
24
+
25
+ bench('set + get cycle', async () => {
26
+ const key = `cycle-${Math.random()}`;
27
+ await cache.set(key, 'value', 3600);
28
+ await cache.get(key);
29
+ });
30
+ });
31
+
32
+ describe('InMemoryCache - Large Scale', () => {
33
+ bench('1000 sequential sets', async () => {
34
+ const cache = new InMemoryCache<string>();
35
+ for (let i = 0; i < 1000; i++) {
36
+ await cache.set(`key-${i}`, `value-${i}`, 3600);
37
+ }
38
+ });
39
+
40
+ bench('1000 sequential gets', async () => {
41
+ const cache = new InMemoryCache<string>();
42
+ // Pre-populate
43
+ for (let i = 0; i < 1000; i++) {
44
+ await cache.set(`key-${i}`, `value-${i}`, 3600);
45
+ }
46
+ // Benchmark gets
47
+ for (let i = 0; i < 1000; i++) {
48
+ await cache.get(`key-${i}`);
49
+ }
50
+ });
51
+ });