You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-8Lines changed: 11 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,18 +41,18 @@ const cache = new Cache();
41
41
cache.set("a", 10);
42
42
43
43
// Check data exists in cache
44
-
cache.has("a");
44
+
cache.has("a");// true
45
45
46
46
// Get data from cache
47
-
console.log(cache.get("a"));
47
+
console.log(cache.get("a"));// 10
48
48
49
49
// Get all data from cache
50
50
cache.forEach(function (data) {
51
51
console.log(data); // { a: 10 }
52
52
});
53
53
54
54
// Get all data to array
55
-
console.log(cache.toArray());
55
+
console.log(cache.toArray());// [ { a: 10 } ]
56
56
57
57
// Delete data from cache
58
58
cache.delete("a");
@@ -61,24 +61,27 @@ cache.delete("a");
61
61
cache.clear();
62
62
```
63
63
64
-
## Create a new cache object
64
+
## Create a new cache
65
65
66
66
To create a new cache we need to create a new instance of lrujs. While creating a new cache we can set the configuration like cache max length and ttl, but it is not mandatory and if we not set any configuration then the default values are used.
67
67
68
68
Defination:
69
+
69
70
```js
70
71
constcache=newCache(options);
71
72
```
72
73
73
74
Where options are the following:
75
+
74
76
-`maxLength` : max length is a cache max length, max length is a positive integer value. The default value is 0, if the value is 0 then it will not check the max length.
75
77
-`ttl` : is cache expires time in milliseconds, the default value is 0 and if value if 0 it will not check the ttl.
76
-
-`interval` : interval is the time interval in milliseconds, after every interval all the expired values are automatically removed. Default value is 0 and if value is 0 the it will not removes expired values automatically, but don't worry expired items are treated as missing, and deleted when they are fetched.
78
+
-`interval` : interval is the time interval in milliseconds, after every interval all the expired items are automatically removed. Default value is 0 and if value is 0 the it will not removes expired items automatically, but don't worry expired items are treated as missing, and deleted when they are fetched.
77
79
-`enableInterval` : enableInterval is a boolean value that is used to enable and disable the interval, the default value is false and if value is explicitly set false then it will not run the interval even if the interval time is set.
78
80
79
-
Lrujs support TTL, but it is not a TTL cache, and also does not make strong TTL guarantees. When interval is set expired values are removed from cache periodically.
81
+
Lrujs support TTL, but it is not a TTL cache, and also does not make strong TTL guarantees. When ttl interval is set, expired items are removed from cache periodically.
80
82
81
83
Example:
84
+
82
85
```js
83
86
constCache=require("@opensnip/lrujs");
84
87
@@ -94,7 +97,7 @@ const cache = new Cache({
94
97
95
98
In lrujs any value (both objects and primitive values) may be used as either a key or a value, duplicate keys not allowed and if duplicate item is inserted it will be replaced by the new item.
96
99
97
-
```js
100
+
````js
98
101
// Add new data in cache
99
102
cache.set("a", 10);
100
103
@@ -111,7 +114,7 @@ By default the configuration TTL value is used for every item, but we can set TT
111
114
```js
112
115
// Add new data in cache
113
116
cache.set("b", 10, { ttl: 200 }); // Expires after 200 ms
0 commit comments