@ruiapp/rapid-core 0.5.8 → 0.5.9

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.
@@ -0,0 +1,11 @@
1
+ export interface CacheProvider {
2
+ set: (key: string, value: any, options?: SetValueOptions) => Promise<void>;
3
+ get: (key: string) => Promise<any>;
4
+ del: (key: string) => Promise<void>;
5
+ }
6
+ export interface SetValueOptions {
7
+ /**
8
+ * Time-to-live, in milliseconds
9
+ */
10
+ ttl?: number;
11
+ }
@@ -0,0 +1,8 @@
1
+ import { FacilityFactory } from "../../core/facility";
2
+ import { IRpdServer } from "../../core/server";
3
+ import MemoryCache from "./MemoryCache";
4
+ export default class CacheFactory implements FacilityFactory {
5
+ readonly name: string;
6
+ constructor();
7
+ createFacility(server: IRpdServer, options?: any): Promise<MemoryCache>;
8
+ }
@@ -0,0 +1,10 @@
1
+ import { CacheProvider, SetValueOptions } from "./CacheFacilityTypes";
2
+ export type MemoryCacheItem<TValue = any> = {
3
+ value: TValue;
4
+ expireAt: number;
5
+ };
6
+ export default class MemoryCache implements CacheProvider {
7
+ set(key: string, value: any, options?: SetValueOptions): Promise<void>;
8
+ get(key: string): Promise<any>;
9
+ del(key: string): Promise<void>;
10
+ }
package/dist/index.d.ts CHANGED
@@ -9,6 +9,8 @@ export * from "./utilities/jwtUtility";
9
9
  export * from "./deno-std/http/cookie";
10
10
  export { mapDbRowToEntity } from "./dataAccess/entityMapper";
11
11
  export * as bootstrapApplicationConfig from "./bootstrapApplicationConfig";
12
+ export { default as CacheFactory } from "./facilities/cache/CacheFactory";
13
+ export * from "./facilities/cache/CacheFacilityTypes";
12
14
  export { default as MetaManagePlugin } from "./plugins/metaManage/MetaManagePlugin";
13
15
  export { default as DataManagePlugin } from "./plugins/dataManage/DataManagePlugin";
14
16
  export { default as RouteManagePlugin } from "./plugins/routeManage/RouteManagePlugin";
package/dist/index.js CHANGED
@@ -4654,6 +4654,46 @@ async function generateJwtSecretKey() {
4654
4654
  return encode(exportedKey);
4655
4655
  }
4656
4656
 
4657
+ const values = new Map();
4658
+ class MemoryCache {
4659
+ async set(key, value, options) {
4660
+ let expireAt = -1;
4661
+ if (options && options.ttl > 0) {
4662
+ expireAt = new Date().valueOf() + options.ttl;
4663
+ }
4664
+ const cacheItem = {
4665
+ value,
4666
+ expireAt,
4667
+ };
4668
+ values.set(key, cacheItem);
4669
+ }
4670
+ async get(key) {
4671
+ const cacheItem = values.get(key);
4672
+ if (cacheItem) {
4673
+ if (cacheItem.expireAt === -1) {
4674
+ return cacheItem.value;
4675
+ }
4676
+ if (cacheItem.expireAt >= new Date().valueOf()) {
4677
+ return cacheItem.value;
4678
+ }
4679
+ }
4680
+ return undefined;
4681
+ }
4682
+ async del(key) {
4683
+ values.delete(key);
4684
+ }
4685
+ }
4686
+
4687
+ class CacheFactory {
4688
+ name;
4689
+ constructor() {
4690
+ this.name = "cache";
4691
+ }
4692
+ async createFacility(server, options) {
4693
+ return new MemoryCache();
4694
+ }
4695
+ }
4696
+
4657
4697
  const code$u = "listMetaModels";
4658
4698
  async function handler$u(plugin, ctx, options) {
4659
4699
  const { applicationConfig } = ctx;
@@ -8675,6 +8715,7 @@ class EntityAccessControlPlugin {
8675
8715
  fixBigIntJSONSerialize();
8676
8716
 
8677
8717
  exports.AuthPlugin = AuthPlugin;
8718
+ exports.CacheFactory = CacheFactory;
8678
8719
  exports.CronJobPlugin = CronJobPlugin;
8679
8720
  exports.DataManagePlugin = DataManager;
8680
8721
  exports.EntityAccessControlPlugin = EntityAccessControlPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.5.8",
3
+ "version": "0.5.9",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1,12 @@
1
+ export interface CacheProvider {
2
+ set: (key: string, value: any, options?: SetValueOptions) => Promise<void>;
3
+ get: (key: string) => Promise<any>;
4
+ del: (key: string) => Promise<void>;
5
+ }
6
+
7
+ export interface SetValueOptions {
8
+ /**
9
+ * Time-to-live, in milliseconds
10
+ */
11
+ ttl?: number;
12
+ }
@@ -0,0 +1,15 @@
1
+ import { FacilityFactory } from "~/core/facility";
2
+ import { IRpdServer } from "~/core/server";
3
+ import MemoryCache from "./MemoryCache";
4
+
5
+ export default class CacheFactory implements FacilityFactory {
6
+ readonly name: string;
7
+
8
+ constructor() {
9
+ this.name = "cache";
10
+ }
11
+
12
+ async createFacility(server: IRpdServer, options?: any) {
13
+ return new MemoryCache();
14
+ }
15
+ }
@@ -0,0 +1,42 @@
1
+ import { CacheProvider, SetValueOptions } from "./CacheFacilityTypes";
2
+
3
+ export type MemoryCacheItem<TValue = any> = {
4
+ value: TValue;
5
+ expireAt: number;
6
+ };
7
+
8
+ const values: Map<string, MemoryCacheItem> = new Map();
9
+
10
+ export default class MemoryCache implements CacheProvider {
11
+ async set(key: string, value: any, options?: SetValueOptions) {
12
+ let expireAt = -1;
13
+ if (options && options.ttl > 0) {
14
+ expireAt = new Date().valueOf() + options.ttl;
15
+ }
16
+
17
+ const cacheItem = {
18
+ value,
19
+ expireAt,
20
+ };
21
+ values.set(key, cacheItem);
22
+ }
23
+
24
+ async get(key: string) {
25
+ const cacheItem = values.get(key);
26
+ if (cacheItem) {
27
+ if (cacheItem.expireAt === -1) {
28
+ return cacheItem.value;
29
+ }
30
+
31
+ if (cacheItem.expireAt >= new Date().valueOf()) {
32
+ return cacheItem.value;
33
+ }
34
+ }
35
+
36
+ return undefined;
37
+ }
38
+
39
+ async del(key: string) {
40
+ values.delete(key);
41
+ }
42
+ }
package/src/index.ts CHANGED
@@ -18,6 +18,9 @@ export { mapDbRowToEntity } from "./dataAccess/entityMapper";
18
18
 
19
19
  export * as bootstrapApplicationConfig from "./bootstrapApplicationConfig";
20
20
 
21
+ export { default as CacheFactory } from "./facilities/cache/CacheFactory";
22
+ export * from "./facilities/cache/CacheFacilityTypes";
23
+
21
24
  export { default as MetaManagePlugin } from "./plugins/metaManage/MetaManagePlugin";
22
25
 
23
26
  export { default as DataManagePlugin } from "./plugins/dataManage/DataManagePlugin";