@nest-omni/core 3.1.1-13 → 3.1.1-14

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.
@@ -1,7 +1,9 @@
1
1
  import { BaseCacheProvider } from './base-cache.provider';
2
2
  export declare class MemoryCacheProvider extends BaseCacheProvider {
3
- private keyv;
3
+ private cache;
4
4
  private keys;
5
+ private defaultTtl?;
6
+ private namespace;
5
7
  constructor(options?: {
6
8
  ttl?: number;
7
9
  namespace?: string;
@@ -15,6 +17,7 @@ export declare class MemoryCacheProvider extends BaseCacheProvider {
15
17
  has(key: string): Promise<boolean>;
16
18
  getAllKeys(): string[];
17
19
  getSize(): number;
20
+ cleanup(): number;
18
21
  mget<T>(keys: string[]): Promise<(T | null)[]>;
19
22
  mset(items: Array<{
20
23
  key: string;
@@ -10,26 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MemoryCacheProvider = void 0;
13
- const keyv_1 = require("keyv");
14
13
  const base_cache_provider_1 = require("./base-cache.provider");
15
14
  class MemoryCacheProvider extends base_cache_provider_1.BaseCacheProvider {
16
15
  constructor(options) {
17
16
  super();
17
+ this.cache = new Map();
18
18
  this.keys = new Set();
19
- this.keyv = new keyv_1.default({
20
- store: new Map(),
21
- ttl: options === null || options === void 0 ? void 0 : options.ttl,
22
- namespace: (options === null || options === void 0 ? void 0 : options.namespace) || 'cache:memory',
23
- });
24
- this.keyv.on('set', (key) => {
25
- this.keys.add(key);
26
- });
27
- this.keyv.on('delete', (key) => {
28
- this.keys.delete(key);
29
- });
30
- this.keyv.on('clear', () => {
31
- this.keys.clear();
32
- });
19
+ this.defaultTtl = options === null || options === void 0 ? void 0 : options.ttl;
20
+ this.namespace = (options === null || options === void 0 ? void 0 : options.namespace) || 'cache:memory';
33
21
  }
34
22
  getName() {
35
23
  return 'Memory';
@@ -37,8 +25,16 @@ class MemoryCacheProvider extends base_cache_provider_1.BaseCacheProvider {
37
25
  get(key) {
38
26
  return __awaiter(this, void 0, void 0, function* () {
39
27
  try {
40
- const value = yield this.keyv.get(key);
41
- return value !== null && value !== void 0 ? value : null;
28
+ const item = this.cache.get(key);
29
+ if (!item) {
30
+ return null;
31
+ }
32
+ if (item.expiresAt && Date.now() > item.expiresAt) {
33
+ this.cache.delete(key);
34
+ this.keys.delete(key);
35
+ return null;
36
+ }
37
+ return item.value;
42
38
  }
43
39
  catch (_a) {
44
40
  return null;
@@ -48,7 +44,14 @@ class MemoryCacheProvider extends base_cache_provider_1.BaseCacheProvider {
48
44
  set(key, value, ttl) {
49
45
  return __awaiter(this, void 0, void 0, function* () {
50
46
  try {
51
- yield this.keyv.set(key, value, ttl);
47
+ const effectiveTtl = ttl !== null && ttl !== void 0 ? ttl : this.defaultTtl;
48
+ const expiresAt = effectiveTtl ? Date.now() + effectiveTtl : undefined;
49
+ const item = {
50
+ value,
51
+ expiresAt,
52
+ };
53
+ this.cache.set(key, item);
54
+ this.keys.add(key);
52
55
  }
53
56
  catch (error) {
54
57
  throw new Error(`MemoryCacheProvider: Failed to set key ${key}: ${error instanceof Error ? error.message : String(error)}`);
@@ -59,11 +62,14 @@ class MemoryCacheProvider extends base_cache_provider_1.BaseCacheProvider {
59
62
  return __awaiter(this, void 0, void 0, function* () {
60
63
  try {
61
64
  if (Array.isArray(key)) {
62
- const promises = key.map((k) => this.keyv.delete(k));
63
- yield Promise.all(promises);
65
+ key.forEach((k) => {
66
+ this.cache.delete(k);
67
+ this.keys.delete(k);
68
+ });
64
69
  }
65
70
  else {
66
- yield this.keyv.delete(key);
71
+ this.cache.delete(key);
72
+ this.keys.delete(key);
67
73
  }
68
74
  }
69
75
  catch (error) {
@@ -89,7 +95,7 @@ class MemoryCacheProvider extends base_cache_provider_1.BaseCacheProvider {
89
95
  clear() {
90
96
  return __awaiter(this, void 0, void 0, function* () {
91
97
  try {
92
- yield this.keyv.clear();
98
+ this.cache.clear();
93
99
  this.keys.clear();
94
100
  }
95
101
  catch (error) {
@@ -100,8 +106,16 @@ class MemoryCacheProvider extends base_cache_provider_1.BaseCacheProvider {
100
106
  has(key) {
101
107
  return __awaiter(this, void 0, void 0, function* () {
102
108
  try {
103
- const value = yield this.keyv.get(key);
104
- return value !== undefined;
109
+ const item = this.cache.get(key);
110
+ if (!item) {
111
+ return false;
112
+ }
113
+ if (item.expiresAt && Date.now() > item.expiresAt) {
114
+ this.cache.delete(key);
115
+ this.keys.delete(key);
116
+ return false;
117
+ }
118
+ return true;
105
119
  }
106
120
  catch (_a) {
107
121
  return false;
@@ -114,10 +128,37 @@ class MemoryCacheProvider extends base_cache_provider_1.BaseCacheProvider {
114
128
  getSize() {
115
129
  return this.keys.size;
116
130
  }
131
+ cleanup() {
132
+ let cleaned = 0;
133
+ const now = Date.now();
134
+ for (const [key, item] of this.cache.entries()) {
135
+ if (item.expiresAt && now > item.expiresAt) {
136
+ this.cache.delete(key);
137
+ this.keys.delete(key);
138
+ cleaned++;
139
+ }
140
+ }
141
+ return cleaned;
142
+ }
117
143
  mget(keys) {
118
144
  return __awaiter(this, void 0, void 0, function* () {
119
- const promises = keys.map((key) => this.get(key));
120
- return Promise.all(promises);
145
+ const results = [];
146
+ const now = Date.now();
147
+ for (const key of keys) {
148
+ const item = this.cache.get(key);
149
+ if (!item) {
150
+ results.push(null);
151
+ continue;
152
+ }
153
+ if (item.expiresAt && now > item.expiresAt) {
154
+ this.cache.delete(key);
155
+ this.keys.delete(key);
156
+ results.push(null);
157
+ continue;
158
+ }
159
+ results.push(item.value);
160
+ }
161
+ return results;
121
162
  });
122
163
  }
123
164
  mset(items, ttl) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nest-omni/core",
3
- "version": "3.1.1-13",
3
+ "version": "3.1.1-14",
4
4
  "description": "A comprehensive NestJS framework for building enterprise-grade applications with best practices",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",