Skip to content

Commit 2ae76b3

Browse files
committed
Added toArray and interval
1 parent b8c40db commit 2ae76b3

12 files changed

+679
-421
lines changed

README.md

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@
66

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

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-
169
## Features
1710

1811
- Super Fast
1912
- Lightweight
2013
- TTL support
21-
- Customised return value
14+
- Custom cache-miss value
2215

2316
## Installation
2417

@@ -39,10 +32,10 @@ $ yarn add @opensnip/lrujs
3932
A simple lru cache example:
4033

4134
```js
42-
const LRUCache = require("@opensnip/lrujs");
35+
const Cache = require("@opensnip/lrujs");
4336

4437
// Create cache object
45-
const cache = new LRUCache();
38+
const cache = new Cache();
4639

4740
// Add data in cache
4841
cache.set("a", 10);
@@ -55,9 +48,12 @@ console.log(cache.get("a"));
5548

5649
// Get all data from cache
5750
cache.forEach(function (data) {
58-
console.log(data);
51+
console.log(data); // { a: 10 }
5952
});
6053

54+
// Get all data to array
55+
console.log(cache.toArray());
56+
6157
// Delete data from cache
6258
cache.delete("a");
6359

@@ -67,36 +63,59 @@ cache.clear();
6763

6864
## Create a new cache object
6965

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

68+
Defination:
7269
```js
73-
const LRUCache = require("@opensnip/lrujs");
70+
const cache = new Cache(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+
const Cache = require("@opensnip/lrujs");
7484

7585
// Create cache object
76-
const cache = new LRUCache({
77-
maxSize: 10, // Optional
78-
ttl: 100, // Optional
86+
const cache = new Cache({
87+
maxLength: 10,
88+
ttl: 100,
89+
interval: 1000,
7990
});
8091
```
8192

8293
## Set a new data
8394

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+
8697
```js
8798
// Add new data in cache
8899
cache.set("a", 10);
89100

101+
// Add new data in cache
102+
cache.set("user", { name: "abc" });
103+
90104
// Add duplicate data
91105
cache.set("a", 20); // Replace the old value
92106

107+
## Set ttl for single data
108+
109+
By default the configuration TTL value is used for every item, but we can set TTL for a single item.
110+
111+
```js
93112
// Add new data in cache
94113
cache.set("b", 10, { ttl: 200 }); // Expires after 200 ms
95114
```
96115

97116
## Get data from cache
98117

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 for items. In this case you can return a custom value on cache miss.
100119

101120
```js
102121
// Add new data in cache
@@ -106,7 +125,7 @@ cache.set("a", 10);
106125
cache.get("a"); // 10
107126
```
108127

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:
110129

111130
```js
112131
// Add new data in cache
@@ -115,7 +134,7 @@ cache.set("a", undefined);
115134
cache.get("a"); // undefined
116135
cache.get("b"); // undefined
117136
118-
// Set return value of get function
137+
// Set custom return value
119138
cache.get("a", function (err, value) {
120139
if (err) return null;
121140
return value;
@@ -129,7 +148,7 @@ cache.get("b", function (err, value) {
129148

130149
## Check data exists in cache
131150

132-
Check weather data is exist in the cache or not.
151+
Check weather item exists in the cache or not.
133152

134153
```js
135154
// Add new data in cache
@@ -168,8 +187,24 @@ cache.set("a", 10);
168187
169188
// Get all data
170189
cache.forEach(function (data) {
171-
console.log(data); // {a: 10}
190+
console.log(data); // { a: 10 }
172191
});
192+
193+
// OR
194+
195+
for (let data of cache) {
196+
console.log(data); // { a: 10 }
197+
}
198+
```
199+
200+
## Get data as array
201+
202+
```js
203+
// Add new data in cache
204+
cache.set("a", 10);
205+
206+
// Get all data
207+
console.log(cache.toArray()); // [ { a: 10 } ]
173208
```
174209

175210
## License

index.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
const LRUCache = require("./src/cache.cjs");
2-
module.exports = LRUCache;
2+
module.exports = LRUCache;

index.d.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
declare class LRUCache {
2-
size: number;
3-
constructor(options?: { maxSize?: number; ttl?: number });
1+
declare class Cache {
2+
length: number;
3+
constructor(options?: {
4+
maxLength?: number;
5+
ttl?: number;
6+
interval?: number;
7+
enableInterval?: boolean;
8+
});
49
set: (key: any, value: any, options?: { ttl?: number }) => void;
5-
get: (key: any, callback?: (err: Error | null, value: any) => any) => any;
10+
startInterval: () => void;
11+
clearInterval: () => void;
12+
get: (
13+
key: any,
14+
callback?: (err: Error | undefined, value: any) => any
15+
) => any;
616
delete: (key: any) => void;
717
clear: () => void;
818
has: (key: any) => boolean;
919
forEach: (callback: (element: object, index?: number) => void) => void;
20+
toArray: () => any[];
1021
}
1122

12-
export default LRUCache;
23+
export default Cache;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@opensnip/lrujs",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Fast and lightweight lru cache for javascript",
55
"main": "index.mjs",
66
"type": "module",

0 commit comments

Comments
 (0)