@acmekit/cache-inmemory 2.13.1

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/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # AcmeKit Cache In-memory
2
+
3
+ AcmeKit in-memory cache module. Use plain JS Map as a cache store.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ yarn add @acmekit/cache-inmemory
9
+ ```
10
+
11
+ ## Options
12
+
13
+ ```
14
+ {
15
+ ttl?: number // Time to keep data in cache (in seconds)
16
+ }
17
+ ```
18
+
19
+ ### Note
20
+
21
+ Recommended for testing and development. For production, use Redis cache module.
22
+
23
+ ### Other caching modules
24
+
25
+ - [AcmeKit Cache Redis](../cache-redis/README.md)
@@ -0,0 +1,6 @@
1
+ import { ModuleExports } from "@acmekit/framework/types";
2
+ declare const moduleDefinition: ModuleExports;
3
+ export default moduleDefinition;
4
+ export * from "./initialize";
5
+ export * from "./types";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAKxD,QAAA,MAAM,gBAAgB,EAAE,aAEvB,CAAA;AAED,eAAe,gBAAgB,CAAA;AAC/B,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ const inmemory_cache_1 = __importDefault(require("./services/inmemory-cache"));
21
+ const service = inmemory_cache_1.default;
22
+ const moduleDefinition = {
23
+ service,
24
+ };
25
+ exports.default = moduleDefinition;
26
+ __exportStar(require("./initialize"), exports);
27
+ __exportStar(require("./types"), exports);
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AACA,+EAA4D;AAE5D,MAAM,OAAO,GAAG,wBAAoB,CAAA;AAEpC,MAAM,gBAAgB,GAAkB;IACtC,OAAO;CACR,CAAA;AAED,kBAAe,gBAAgB,CAAA;AAC/B,+CAA4B;AAC5B,0CAAuB"}
@@ -0,0 +1,4 @@
1
+ import { ExternalModuleDeclaration, ICacheService } from "@acmekit/framework/types";
2
+ import { InMemoryCacheModuleOptions } from "../types";
3
+ export declare const initialize: (options?: InMemoryCacheModuleOptions | ExternalModuleDeclaration) => Promise<ICacheService>;
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/initialize/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,yBAAyB,EACzB,aAAa,EAEd,MAAM,0BAA0B,CAAA;AAEjC,OAAO,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAA;AAErD,eAAO,MAAM,UAAU,GACrB,UAAU,0BAA0B,GAAG,yBAAyB,KAC/D,OAAO,CAAC,aAAa,CAWvB,CAAA"}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initialize = void 0;
4
+ const modules_sdk_1 = require("@acmekit/framework/modules-sdk");
5
+ const utils_1 = require("@acmekit/framework/utils");
6
+ const initialize = async (options) => {
7
+ const serviceKey = utils_1.Modules.CACHE;
8
+ const loaded = await modules_sdk_1.AcmeKitModule.bootstrap({
9
+ moduleKey: serviceKey,
10
+ defaultPath: "@acmekit//cache-inmemory",
11
+ declaration: options,
12
+ });
13
+ return loaded[serviceKey];
14
+ };
15
+ exports.initialize = initialize;
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/initialize/index.ts"],"names":[],"mappings":";;;AAAA,gEAA8D;AAM9D,oDAAkD;AAG3C,MAAM,UAAU,GAAG,KAAK,EAC7B,OAAgE,EACxC,EAAE;IAC1B,MAAM,UAAU,GAAG,eAAO,CAAC,KAAK,CAAA;IAChC,MAAM,MAAM,GAAG,MAAM,2BAAa,CAAC,SAAS,CAAgB;QAC1D,SAAS,EAAE,UAAU;QACrB,WAAW,EAAE,0BAA0B;QACvC,WAAW,EAAE,OAEgB;KAC9B,CAAC,CAAA;IAEF,OAAO,MAAM,CAAC,UAAU,CAAC,CAAA;AAC3B,CAAC,CAAA;AAbY,QAAA,UAAU,cAatB"}
@@ -0,0 +1,2 @@
1
+ export { default as InMemoryCacheService } from "./inmemory-cache";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,oBAAoB,EAAE,MAAM,kBAAkB,CAAA"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.InMemoryCacheService = void 0;
7
+ var inmemory_cache_1 = require("./inmemory-cache");
8
+ Object.defineProperty(exports, "InMemoryCacheService", { enumerable: true, get: function () { return __importDefault(inmemory_cache_1).default; } });
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;AAAA,mDAAkE;AAAzD,uIAAA,OAAO,OAAwB"}
@@ -0,0 +1,37 @@
1
+ import { ICacheService } from "@acmekit/framework/types";
2
+ import { CacheRecord, InMemoryCacheModuleOptions } from "../types";
3
+ type InjectedDependencies = {};
4
+ /**
5
+ * Class represents basic, in-memory, cache store.
6
+ */
7
+ declare class InMemoryCacheService implements ICacheService {
8
+ protected readonly TTL: number;
9
+ protected readonly store: Map<string, CacheRecord<any>>;
10
+ protected readonly timoutRefs: Map<string, NodeJS.Timeout>;
11
+ constructor(deps: InjectedDependencies, options?: InMemoryCacheModuleOptions);
12
+ /**
13
+ * Retrieve data from the cache.
14
+ * @param key - cache key
15
+ */
16
+ get<T>(key: string): Promise<T | null>;
17
+ /**
18
+ * Set data to the cache.
19
+ * @param key - cache key under which the data is stored
20
+ * @param data - data to be stored in the cache
21
+ * @param ttl - expiration time in seconds
22
+ */
23
+ set<T>(key: string, data: T, ttl?: number): Promise<void>;
24
+ /**
25
+ * Delete data from the cache.
26
+ * Could use wildcard (*) matcher e.g. `invalidate("ps:*")` to delete all keys that start with "ps:"
27
+ *
28
+ * @param key - cache key
29
+ */
30
+ invalidate(key: string): Promise<void>;
31
+ /**
32
+ * Delete the entire cache.
33
+ */
34
+ clear(): Promise<void>;
35
+ }
36
+ export default InMemoryCacheService;
37
+ //# sourceMappingURL=inmemory-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inmemory-cache.d.ts","sourceRoot":"","sources":["../../src/services/inmemory-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AACxD,OAAO,EAAE,WAAW,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAA;AAIlE,KAAK,oBAAoB,GAAG,EAAE,CAAA;AAE9B;;GAEG;AACH,cAAM,oBAAqB,YAAW,aAAa;IACjD,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IAE9B,SAAS,CAAC,QAAQ,CAAC,KAAK,gCAAsC;IAC9D,SAAS,CAAC,QAAQ,CAAC,UAAU,8BAAoC;gBAG/D,IAAI,EAAE,oBAAoB,EAC1B,OAAO,GAAE,0BAA+B;IAK1C;;;OAGG;IACG,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAa5C;;;;;OAKG;IACG,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,GAAE,MAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAwBzE;;;;;OAKG;IACG,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAkB5C;;OAEG;IACG,KAAK;CAMZ;AAED,eAAe,oBAAoB,CAAA"}
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const DEFAULT_TTL = 30; // seconds
4
+ /**
5
+ * Class represents basic, in-memory, cache store.
6
+ */
7
+ class InMemoryCacheService {
8
+ constructor(deps, options = {}) {
9
+ this.store = new Map();
10
+ this.timoutRefs = new Map();
11
+ this.TTL = options.ttl ?? DEFAULT_TTL;
12
+ }
13
+ /**
14
+ * Retrieve data from the cache.
15
+ * @param key - cache key
16
+ */
17
+ async get(key) {
18
+ const now = Date.now();
19
+ const record = this.store.get(key);
20
+ const recordExpire = record?.expire ?? Infinity;
21
+ if (!record || recordExpire < now) {
22
+ return null;
23
+ }
24
+ return record.data;
25
+ }
26
+ /**
27
+ * Set data to the cache.
28
+ * @param key - cache key under which the data is stored
29
+ * @param data - data to be stored in the cache
30
+ * @param ttl - expiration time in seconds
31
+ */
32
+ async set(key, data, ttl = this.TTL) {
33
+ if (ttl === 0) {
34
+ return;
35
+ }
36
+ const record = { data, expire: ttl * 1000 + Date.now() };
37
+ const oldRecord = this.store.get(key);
38
+ if (oldRecord) {
39
+ clearTimeout(this.timoutRefs.get(key));
40
+ this.timoutRefs.delete(key);
41
+ }
42
+ const ref = setTimeout(async () => {
43
+ await this.invalidate(key);
44
+ }, ttl * 1000);
45
+ ref.unref();
46
+ this.timoutRefs.set(key, ref);
47
+ this.store.set(key, record);
48
+ }
49
+ /**
50
+ * Delete data from the cache.
51
+ * Could use wildcard (*) matcher e.g. `invalidate("ps:*")` to delete all keys that start with "ps:"
52
+ *
53
+ * @param key - cache key
54
+ */
55
+ async invalidate(key) {
56
+ let keys = [key];
57
+ if (key.includes("*")) {
58
+ const regExp = new RegExp(key.replace("*", ".*"));
59
+ keys = Array.from(this.store.keys()).filter((k) => k.match(regExp));
60
+ }
61
+ keys.forEach((key) => {
62
+ const timeoutRef = this.timoutRefs.get(key);
63
+ if (timeoutRef) {
64
+ clearTimeout(timeoutRef);
65
+ this.timoutRefs.delete(key);
66
+ }
67
+ this.store.delete(key);
68
+ });
69
+ }
70
+ /**
71
+ * Delete the entire cache.
72
+ */
73
+ async clear() {
74
+ this.timoutRefs.forEach((ref) => clearTimeout(ref));
75
+ this.timoutRefs.clear();
76
+ this.store.clear();
77
+ }
78
+ }
79
+ exports.default = InMemoryCacheService;
80
+ //# sourceMappingURL=inmemory-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inmemory-cache.js","sourceRoot":"","sources":["../../src/services/inmemory-cache.ts"],"names":[],"mappings":";;AAGA,MAAM,WAAW,GAAG,EAAE,CAAA,CAAC,UAAU;AAIjC;;GAEG;AACH,MAAM,oBAAoB;IAMxB,YACE,IAA0B,EAC1B,UAAsC,EAAE;QALvB,UAAK,GAAG,IAAI,GAAG,EAA4B,CAAA;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AAC;CACF;AAED,kBAAe,oBAAoB,CAAA"}
@@ -0,0 +1 @@
1
+ {"root":["../src/index.ts","../src/initialize/index.ts","../src/services/index.ts","../src/services/inmemory-cache.ts","../src/services/__tests__/inmemory-cache.js","../src/types/index.ts"],"version":"5.9.3"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Shape of a record saved in `in-memory` cache
3
+ */
4
+ export type CacheRecord<T> = {
5
+ data: T;
6
+ /**
7
+ * Timestamp in milliseconds
8
+ */
9
+ expire: number;
10
+ };
11
+ export type InMemoryCacheModuleOptions = {
12
+ /**
13
+ * Time to keep data in cache (in seconds)
14
+ */
15
+ ttl?: number;
16
+ };
17
+ declare module "@acmekit/types" {
18
+ interface ModuleOptions {
19
+ "@acmekit/cache-inmemory": InMemoryCacheModuleOptions;
20
+ "@acmekit/acmekit/cache-inmemory": InMemoryCacheModuleOptions;
21
+ }
22
+ }
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;IAC3B,IAAI,EAAE,CAAC,CAAA;IACP;;OAEG;IACH,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED,MAAM,MAAM,0BAA0B,GAAG;IACvC;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;CACb,CAAA;AAED,OAAO,QAAQ,gBAAgB,CAAC;IAC9B,UAAU,aAAa;QACrB,yBAAyB,EAAE,0BAA0B,CAAA;QACrD,iCAAiC,EAAE,0BAA0B,CAAA;KAC9D;CACF"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@acmekit/cache-inmemory",
3
+ "version": "2.13.1",
4
+ "description": "In-memory Cache Module for AcmeKit",
5
+ "main": "dist/index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/acmekit/acmekit",
9
+ "directory": "packages/modules/cache-inmemory"
10
+ },
11
+ "engines": {
12
+ "node": ">=20"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "!dist/**/__tests__",
20
+ "!dist/**/__mocks__",
21
+ "!dist/**/__fixtures__"
22
+ ],
23
+ "author": "AcmeKit",
24
+ "license": "MIT",
25
+ "devDependencies": {
26
+ "@acmekit/framework": "2.13.1"
27
+ },
28
+ "scripts": {
29
+ "watch": "yarn run -T tsc --build --watch",
30
+ "build": "yarn run -T rimraf dist && yarn run -T tsc --build",
31
+ "test": "../../../node_modules/.bin/jest --passWithNoTests"
32
+ },
33
+ "peerDependencies": {
34
+ "@acmekit/framework": "2.13.1"
35
+ }
36
+ }