fast-ttl-cache 0.1.3 → 0.1.4

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 CHANGED
@@ -20,17 +20,21 @@ import FastTTLCache from 'fast-ttl-cache';
20
20
  const cache = new FastTTLCache({
21
21
  ttl: 5 * 1000, // ttl in millseconds, get an outdated data will return null and delete it
22
22
  capacity: 1000, // max capacity, When the capacity is exceeded, the least recently updated data will be removed.
23
+ cloneLevel: 0, // 0 (no clone), 1 (shallow clone), 2 (deep clone), defaults to 0
23
24
  });
24
25
 
25
26
  cache.put('key1', 'value1');
26
27
  cache.put('key2', 'value2');
27
- cache.get('key2'); // return value2
28
+ cache.put('key3', 'value3');
29
+ console.log(cache.get('key2')); // return value2
28
30
 
29
31
  // wait for 5 seconds
32
+ await new Promise(resolve => setTimeout(resolve, 5000));
33
+
30
34
  cache.get('key1'); // return null and key1 will be removed
31
- cache.size; // return 1, key2 is outdated but is not been removed yet
35
+ cache.size; // return 2, key2 & key3 are outdated but are not been removed yet
32
36
 
33
- cache.get('key2'); // return null and key2 will be removed
37
+ cache.get('key3'); // return null and key2 & key3 will be removed
34
38
  cache.size; // return 0
35
39
  ```
36
40
 
@@ -39,6 +43,7 @@ cache.size; // return 0
39
43
 
40
44
  - ```options.ttl```: number of millseconds, defaults to Infinity
41
45
  - ```options.capacity```: number of max capacity, defaults to Infinity
46
+ - ```options.cloneLevel```: number of clone level, defaults to 0 (no clone), 1 (shallow clone), 2 (deep clone)
42
47
 
43
48
  ```FastTTLCache.prototype.put(key, value)```
44
49
 
@@ -47,6 +52,9 @@ cache.size; // return 0
47
52
  ```FastTTLCache.prototype.get(key)```
48
53
 
49
54
  - Get the value of the key from cache, return null if the key is not exists or has been expired.
55
+ - if cloneLevel is 0, return the original data
56
+ - if cloneLevel is 1, return the shallow cloned data
57
+ - if cloneLevel is 2, return the deep cloned data
50
58
 
51
59
  ```FastTTLCache.prototype.size```
52
60
 
package/dist/index.d.mts CHANGED
@@ -17,6 +17,8 @@ declare class FastTTLCache {
17
17
  tail: CacheItem;
18
18
  /** 缓存大小 */
19
19
  size: number;
20
+ /** 数据 clone 方法 */
21
+ private cloneMethod;
20
22
  /**
21
23
  * 构造函数
22
24
  * @param {CacheOptions} options 配置选项,包含ttl(过期时间)和capacity(容量)
package/dist/index.d.ts CHANGED
@@ -17,6 +17,8 @@ declare class FastTTLCache {
17
17
  tail: CacheItem;
18
18
  /** 缓存大小 */
19
19
  size: number;
20
+ /** 数据 clone 方法 */
21
+ private cloneMethod;
20
22
  /**
21
23
  * 构造函数
22
24
  * @param {CacheOptions} options 配置选项,包含ttl(过期时间)和capacity(容量)
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all)
@@ -14,6 +16,14 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
18
  };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
17
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
28
 
19
29
  // src/index.ts
@@ -22,6 +32,15 @@ __export(index_exports, {
22
32
  default: () => FastTTLCache
23
33
  });
24
34
  module.exports = __toCommonJS(index_exports);
35
+ var import_lodash = __toESM(require("lodash.clonedeep"));
36
+ var import_lodash2 = __toESM(require("lodash.clone"));
37
+ var pure = (i) => i;
38
+ var NO_CLONE = 0;
39
+ var SHALLOW_CLONE = 1;
40
+ var DEEP_CLONE = 2;
41
+ function getCloneMethod(cloneLevel = NO_CLONE) {
42
+ return cloneLevel === DEEP_CLONE ? import_lodash.default : cloneLevel === SHALLOW_CLONE ? import_lodash2.default : pure;
43
+ }
25
44
  var FastTTLCache = class {
26
45
  /**
27
46
  * 构造函数
@@ -36,8 +55,11 @@ var FastTTLCache = class {
36
55
  this.tail = null;
37
56
  /** 缓存大小 */
38
57
  this.size = 0;
58
+ /** 数据 clone 方法 */
59
+ this.cloneMethod = pure;
39
60
  this.ttl = options.ttl || Infinity;
40
61
  this.capacity = options.capacity || Infinity;
62
+ this.cloneMethod = getCloneMethod(options.cloneLevel);
41
63
  Object.defineProperties(this, {
42
64
  size: {
43
65
  get() {
@@ -72,7 +94,7 @@ var FastTTLCache = class {
72
94
  this.del(item, true);
73
95
  return null;
74
96
  }
75
- return item.value;
97
+ return this.cloneMethod(item.value);
76
98
  }
77
99
  /**
78
100
  * 设置缓存,包含已存在或新增
package/dist/index.mjs CHANGED
@@ -1,4 +1,13 @@
1
1
  // src/index.ts
2
+ import cloneDeep from "lodash.clonedeep";
3
+ import clone from "lodash.clone";
4
+ var pure = (i) => i;
5
+ var NO_CLONE = 0;
6
+ var SHALLOW_CLONE = 1;
7
+ var DEEP_CLONE = 2;
8
+ function getCloneMethod(cloneLevel = NO_CLONE) {
9
+ return cloneLevel === DEEP_CLONE ? cloneDeep : cloneLevel === SHALLOW_CLONE ? clone : pure;
10
+ }
2
11
  var FastTTLCache = class {
3
12
  /**
4
13
  * 构造函数
@@ -13,8 +22,11 @@ var FastTTLCache = class {
13
22
  this.tail = null;
14
23
  /** 缓存大小 */
15
24
  this.size = 0;
25
+ /** 数据 clone 方法 */
26
+ this.cloneMethod = pure;
16
27
  this.ttl = options.ttl || Infinity;
17
28
  this.capacity = options.capacity || Infinity;
29
+ this.cloneMethod = getCloneMethod(options.cloneLevel);
18
30
  Object.defineProperties(this, {
19
31
  size: {
20
32
  get() {
@@ -49,7 +61,7 @@ var FastTTLCache = class {
49
61
  this.del(item, true);
50
62
  return null;
51
63
  }
52
- return item.value;
64
+ return this.cloneMethod(item.value);
53
65
  }
54
66
  /**
55
67
  * 设置缓存,包含已存在或新增
package/dist/types.d.mts CHANGED
@@ -21,6 +21,8 @@ interface CacheOptions {
21
21
  ttl?: number;
22
22
  /** 缓存最大容量 */
23
23
  capacity?: number;
24
+ /** 数据 clone 等级,0表示不克隆,1表示浅克隆,2表示深克隆 */
25
+ cloneLevel?: number;
24
26
  }
25
27
 
26
28
  export type { CacheItem, CacheOptions };
package/dist/types.d.ts CHANGED
@@ -21,6 +21,8 @@ interface CacheOptions {
21
21
  ttl?: number;
22
22
  /** 缓存最大容量 */
23
23
  capacity?: number;
24
+ /** 数据 clone 等级,0表示不克隆,1表示浅克隆,2表示深克隆 */
25
+ cloneLevel?: number;
24
26
  }
25
27
 
26
28
  export type { CacheItem, CacheOptions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-ttl-cache",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "ttl cache with capacity support use no timer",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -35,5 +35,9 @@
35
35
  },
36
36
  "files": [
37
37
  "dist/"
38
- ]
38
+ ],
39
+ "dependencies": {
40
+ "lodash.clone": "^4.5.0",
41
+ "lodash.clonedeep": "^4.5.0"
42
+ }
39
43
  }