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
+58-23Lines changed: 58 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -6,19 +6,12 @@
6
6
7
7
Lrujs is a fast and lightweight lru (least-recently-used) caching library for javascript.
8
8
9
-
In LRU (least-recently-used) cache, when new data is added and if cache if full, it will automatically removes the least recently used data from cache and insert the new data in the cache.
10
-
11
-
Lrujs support TTL (Time to live) and max cache size feature.
12
-
The default value for maxSize is 1000 and for TTL is 0 milliseconds (0 means permanently), if you will not set the max cache size and ttl then default values will be used.
13
-
14
-
Lrujs support TTL, but it is not a TTL cache, and also does not make strong TTL guarantees. Lrujs will not removes the expired items from cache periodically, but you can set the default TTL on cache or on a single value. If you do so, it will treat expired items as missing, and delete them when they are fetched.
15
-
16
9
## Features
17
10
18
11
- Super Fast
19
12
- Lightweight
20
13
- TTL support
21
-
-Customised return value
14
+
-Custom cache-miss value
22
15
23
16
## Installation
24
17
@@ -39,10 +32,10 @@ $ yarn add @opensnip/lrujs
39
32
A simple lru cache example:
40
33
41
34
```js
42
-
constLRUCache=require("@opensnip/lrujs");
35
+
constCache=require("@opensnip/lrujs");
43
36
44
37
// Create cache object
45
-
constcache=newLRUCache();
38
+
constcache=newCache();
46
39
47
40
// Add data in cache
48
41
cache.set("a", 10);
@@ -55,9 +48,12 @@ console.log(cache.get("a"));
55
48
56
49
// Get all data from cache
57
50
cache.forEach(function (data) {
58
-
console.log(data);
51
+
console.log(data);// { a: 10 }
59
52
});
60
53
54
+
// Get all data to array
55
+
console.log(cache.toArray());
56
+
61
57
// Delete data from cache
62
58
cache.delete("a");
63
59
@@ -67,36 +63,59 @@ cache.clear();
67
63
68
64
## Create a new cache object
69
65
70
-
When we create a new cache we set the max cache size and ttl, but it is not mandatory.
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.
71
67
68
+
Defination:
72
69
```js
73
-
constLRUCache=require("@opensnip/lrujs");
70
+
constcache=newCache(options);
71
+
```
72
+
73
+
Where options are the following:
74
+
-`maxLength` is 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
+
-`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` 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.
77
+
-`enableInterval` is 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
+
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.
80
+
81
+
Example:
82
+
```js
83
+
constCache=require("@opensnip/lrujs");
74
84
75
85
// Create cache object
76
-
constcache=newLRUCache({
77
-
maxSize:10, // Optional
78
-
ttl:100, // Optional
86
+
constcache=newCache({
87
+
maxLength:10,
88
+
ttl:100,
89
+
interval:1000,
79
90
});
80
91
```
81
92
82
93
## Set a new data
83
94
84
-
In lrujs any value (both objects and primitive values) may be used as either a key or a value. If you set the duplicate item in the cache, then it will replace the old item.
85
-
You can also set the TTL for a single item
95
+
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
+
86
97
```js
87
98
// Add new data in cache
88
99
cache.set("a", 10);
89
100
101
+
// Add new data in cache
102
+
cache.set("user", { name:"abc" });
103
+
90
104
// Add duplicate data
91
105
cache.set("a", 20); // Replace the old value
92
106
107
+
## Set ttl for single data
108
+
109
+
By default the configuration TTL value is used for every item, but we can set TTLfor a single item.
110
+
111
+
```js
93
112
// Add new data in cache
94
113
cache.set("b", 10, { ttl: 200 }); // Expires after 200 ms
95
114
```
96
115
97
116
## Get data from cache
98
117
99
-
Access data from the cache, if data is not in the cache by default lrujs will return undefined value.
118
+
By default on cache miss lrujs returns undefined value, but undefined is also can be used as value foritems. Inthis case you can returna custom value on cache miss.
100
119
101
120
```js
102
121
// Add new data in cache
@@ -106,7 +125,7 @@ cache.set("a", 10);
106
125
cache.get("a"); // 10
107
126
```
108
127
109
-
If key is not exists in the cache it will return the undefined value, but undefined also can be the value for a key for this kind of situation you can customise the return value of `cache.get` function or check data exists in the cache or not.
128
+
Customize cache miss value:
110
129
111
130
```js
112
131
// Add new data in cache
@@ -115,7 +134,7 @@ cache.set("a", undefined);
115
134
cache.get("a"); // undefined
116
135
cache.get("b"); // undefined
117
136
118
-
// Set return value of get function
137
+
// Set custom return value
119
138
cache.get("a", function (err, value) {
120
139
if (err) return null;
121
140
return value;
@@ -129,7 +148,7 @@ cache.get("b", function (err, value) {
0 commit comments