@reactionary/core 0.10.32 → 0.10.33

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,50 +1,48 @@
1
1
  import { getReactionaryCacheMeter } from "../metrics/metrics.js";
2
- import { error, NotFoundErrorSchema, success } from "../schemas/index.js";
3
2
  class MemoryCache {
4
- entries = new Array();
3
+ entries = /* @__PURE__ */ new Map();
5
4
  meter = getReactionaryCacheMeter();
6
5
  async get(key, schema) {
7
- const c = this.entries.find((x) => x.key === key);
8
- if (!c) {
6
+ const entry = this.entries.get(key);
7
+ if (!entry || entry.expiresAt <= Date.now()) {
8
+ if (entry) {
9
+ this.entries.delete(key);
10
+ }
9
11
  this.meter.misses.add(1, {
10
12
  "labels.cache_type": "memory"
11
13
  });
12
14
  return null;
13
15
  }
14
- const parsed = schema.parse(c.value);
16
+ const parsed = schema.parse(entry.value);
15
17
  this.meter.hits.add(1, {
16
18
  "labels.cache_type": "memory"
17
19
  });
18
20
  return parsed;
19
21
  }
20
22
  async put(key, value, options) {
21
- this.entries.push({
22
- key,
23
+ this.entries.set(key, {
23
24
  value,
24
- options
25
+ options,
26
+ expiresAt: Date.now() + options.ttlSeconds * 1e3
25
27
  });
26
- this.meter.items.record(this.entries.length, {
28
+ this.meter.items.record(this.entries.size, {
27
29
  "labels.cache_type": "memory"
28
30
  });
29
31
  return;
30
32
  }
31
33
  async invalidate(dependencyIds) {
32
- let index = 0;
33
- for (const entry of this.entries) {
34
- for (const entryDependency of entry.options.dependencyIds) {
35
- if (dependencyIds.indexOf(entryDependency) > -1) {
36
- this.entries.splice(index, 1);
37
- }
34
+ for (const [key, entry] of this.entries) {
35
+ if (entry.options.dependencyIds.some((dependencyId) => dependencyIds.includes(dependencyId))) {
36
+ this.entries.delete(key);
38
37
  }
39
- index++;
40
38
  }
41
- this.meter.items.record(this.entries.length, {
39
+ this.meter.items.record(this.entries.size, {
42
40
  "labels.cache_type": "memory"
43
41
  });
44
42
  }
45
43
  async clear() {
46
- this.entries = [];
47
- this.meter.items.record(this.entries.length, {
44
+ this.entries = /* @__PURE__ */ new Map();
45
+ this.meter.items.record(this.entries.size, {
48
46
  "labels.cache_type": "memory"
49
47
  });
50
48
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactionary/core",
3
- "version": "0.10.32",
3
+ "version": "0.10.33",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./src/index.d.ts",
@@ -1,20 +1,21 @@
1
1
  import type { BaseModel } from '../schemas/models/index.js';
2
- import type { Result } from '../schemas/result.js';
3
2
  import type { Cache, CacheEntryOptions } from './cache.interface.js';
4
3
  import type * as z from 'zod';
4
+ interface MemoryCacheEntry {
5
+ value: unknown;
6
+ options: CacheEntryOptions;
7
+ expiresAt: number;
8
+ }
5
9
  /**
6
10
  * Memory version of the cache. Primarily useful for local development.
7
11
  * This is NOT suited for production use.
8
12
  */
9
13
  export declare class MemoryCache implements Cache {
10
- protected entries: {
11
- key: string;
12
- value: unknown;
13
- options: CacheEntryOptions;
14
- }[];
14
+ protected entries: Map<string, MemoryCacheEntry>;
15
15
  protected meter: import("../metrics/metrics.js").ReactionaryCacheMetrics;
16
16
  get<T extends BaseModel>(key: string, schema: z.ZodType<T>): Promise<T | null>;
17
- put(key: string, value: Result<unknown>, options: CacheEntryOptions): Promise<void>;
17
+ put(key: string, value: unknown, options: CacheEntryOptions): Promise<void>;
18
18
  invalidate(dependencyIds: Array<string>): Promise<void>;
19
19
  clear(): Promise<void>;
20
20
  }
21
+ export {};