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
+29-9Lines changed: 29 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,12 @@
6
6
7
7
Lrujs is a fast and lightweight lru (least-recently-used) caching library for javascript.
8
8
9
-
A LRU (least-recently-used) cachewill automatically removes the least recently used data from cache when a new data is added to the cache and cache is full.
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
10
11
11
Lrujs support TTL (Time to live) and max cache size feature.
12
12
The default value for maxSize is 1000 and TTL is 0 milliseconds (0 means permanently), if you not set the max cache size and ttl then the default values are used.
13
13
14
-
Lrujs is also support TTL, but it is not a TTL cache, and does not make strong TTL guarantees. Lrujs will not automatically removes the expired items, but you can set a default TTL on the cache or on a single value. If you do so, it will treat expired items as missing, and delete them when they are fetched. You can set TTL in milliseconds.
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
15
16
16
## Features
17
17
@@ -35,6 +35,8 @@ $ yarn add @opensnip/lrujs
35
35
36
36
## Example
37
37
38
+
A simple lru cache example:
39
+
38
40
```js
39
41
constLRUCache=require("@opensnip/lrujs");
40
42
@@ -50,41 +52,51 @@ cache.has("a");
50
52
// Get data from cache
51
53
console.log(cache.get("a"));
52
54
53
-
// Get data from cache
54
-
cache.delete("a");
55
-
56
55
// Get all data from cache
57
56
cache.forEach(function (data) {
58
57
console.log(data);
59
58
});
59
+
60
+
// Delete data from cache
61
+
cache.delete("a");
62
+
63
+
// Delete all data from cache
64
+
cache.clear();
60
65
```
61
66
62
67
## Create a new cache object
63
68
69
+
When we create a new cache we set the max cache size and ttl, but it is not mandatory.
70
+
64
71
```js
65
72
constLRUCache=require("@opensnip/lrujs");
66
73
67
74
// Create cache object
68
75
constcache=newLRUCache({
69
-
maxSize:10,
70
-
ttl:100,
76
+
maxSize:10,// Optional
77
+
ttl:100,// Optional
71
78
});
72
79
```
73
80
74
81
## Set a new data
75
82
76
-
In lrujs any value (both objects and primitive values) may be used as either a key or a value.
77
-
83
+
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.
84
+
You can also set the TTL for a single item
78
85
```js
79
86
// Add new data in cache
80
87
cache.set("a", 10);
81
88
89
+
// Add duplicate data
90
+
cache.set("a", 20); // Replace the old value
91
+
82
92
// Add new data in cache
83
93
cache.set("b", 10, { ttl:200 }); // Expires after 200 ms
84
94
```
85
95
86
96
## Get data from cache
87
97
98
+
Access data from the cache, if data is not in the cache by default lrujs will return undefined value.
99
+
88
100
```js
89
101
// Add new data in cache
90
102
cache.set("a", 10);
@@ -116,6 +128,8 @@ cache.get("b", function (err, value) {
0 commit comments