Skip to content

Commit 0131971

Browse files
Update README.md
1 parent de75a4a commit 0131971

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
Lrujs is a fast and lightweight lru (least-recently-used) caching library for javascript.
88

9-
A LRU (least-recently-used) cache will 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.
1010

1111
Lrujs support TTL (Time to live) and max cache size feature.
1212
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.
1313

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.
1515

1616
## Features
1717

@@ -35,6 +35,8 @@ $ yarn add @opensnip/lrujs
3535

3636
## Example
3737

38+
A simple lru cache example:
39+
3840
```js
3941
const LRUCache = require("@opensnip/lrujs");
4042

@@ -50,41 +52,51 @@ cache.has("a");
5052
// Get data from cache
5153
console.log(cache.get("a"));
5254

53-
// Get data from cache
54-
cache.delete("a");
55-
5655
// Get all data from cache
5756
cache.forEach(function (data) {
5857
console.log(data);
5958
});
59+
60+
// Delete data from cache
61+
cache.delete("a");
62+
63+
// Delete all data from cache
64+
cache.clear();
6065
```
6166

6267
## Create a new cache object
6368

69+
When we create a new cache we set the max cache size and ttl, but it is not mandatory.
70+
6471
```js
6572
const LRUCache = require("@opensnip/lrujs");
6673

6774
// Create cache object
6875
const cache = new LRUCache({
69-
maxSize: 10,
70-
ttl: 100,
76+
maxSize: 10, // Optional
77+
ttl: 100, // Optional
7178
});
7279
```
7380

7481
## Set a new data
7582

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
7885
```js
7986
// Add new data in cache
8087
cache.set("a", 10);
8188

89+
// Add duplicate data
90+
cache.set("a", 20); // Replace the old value
91+
8292
// Add new data in cache
8393
cache.set("b", 10, { ttl: 200 }); // Expires after 200 ms
8494
```
8595

8696
## Get data from cache
8797

98+
Access data from the cache, if data is not in the cache by default lrujs will return undefined value.
99+
88100
```js
89101
// Add new data in cache
90102
cache.set("a", 10);
@@ -116,6 +128,8 @@ cache.get("b", function (err, value) {
116128

117129
## Check data exists in cache
118130

131+
Check weather data is exist in the cache or not.
132+
119133
```js
120134
// Add new data in cache
121135
cache.set("a", undefined);
@@ -127,20 +141,26 @@ cache.has("b"); // false
127141

128142
## Delete data from cache
129143

144+
Remove data from cache.
145+
130146
```js
131147
// Delete data
132148
cache.delete("a");
133149
```
134150

135151
## Delete all data from cache
136152

153+
Remove all data from the cache.
154+
137155
```js
138156
// Delete all data
139157
cache.clear();
140158
```
141159

142160
## Get all data from cache
143161

162+
Get all data from the cache.
163+
144164
```js
145165
// Add new data in cache
146166
cache.set("a", 10);

0 commit comments

Comments
 (0)