Skip to content

Commit acb1599

Browse files
committed
lrujs
1 parent 1f3ff44 commit acb1599

File tree

11 files changed

+806
-2
lines changed

11 files changed

+806
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
package-lock.json
3+

README.md

Lines changed: 163 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,163 @@
1-
# lrujs
2-
Fast and lightweight lru cache for javascript
1+
## lrujs
2+
3+
[![NPM Version][npm-version-image]][npm-url]
4+
[![NPM Install Size][npm-install-size-image]][npm-install-size-url]
5+
[![NPM Downloads][npm-downloads-image]][npm-downloads-url]
6+
7+
Lrujs is a fast and lightweight least-recently-used cache for [Node.js](http://nodejs.org)
8+
9+
## Features
10+
11+
- Super Fast
12+
- Lightweight
13+
- TTL support
14+
15+
## Installation
16+
17+
Install using npm:
18+
19+
```console
20+
$ npm i lrujs
21+
```
22+
23+
Install using yarn:
24+
25+
```console
26+
$ yarn add lrujs
27+
```
28+
29+
## Example
30+
31+
```js
32+
const LRUCache = require("lrujs");
33+
34+
// Create cache object
35+
const cache = new LRUCache();
36+
37+
// Add data in cache
38+
cache.set("a", 10);
39+
40+
// Check data exists in cache
41+
cache.has("a");
42+
43+
// Get data from cache
44+
console.log(cache.get("a"));
45+
46+
// Get data from cache
47+
cache.delete("a");
48+
49+
// Get all data from cache
50+
cache.forEach(function (data) {
51+
console.log(data);
52+
});
53+
```
54+
55+
## Introduction
56+
57+
Lrujs is a fast and lightweight caching library for javascript, lrujs support TTL (Time to live) and max cache size feature.
58+
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.
59+
60+
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.
61+
62+
#### Create a cache object:
63+
64+
```js
65+
const LRUCache = require("lrujs");
66+
67+
// Create cache object
68+
const cache = new LRUCache({
69+
maxSize: 10,
70+
ttl: 100,
71+
});
72+
```
73+
74+
#### Set a new data:
75+
76+
In lrujs any value (both objects and primitive values) may be used as either a key or a value.
77+
78+
```js
79+
// Add new data in cache
80+
cache.set("a", 10);
81+
82+
// Add new data in cache
83+
cache.set("b", 10, { ttl: 200 }); // Expires after 200 ms
84+
```
85+
86+
#### Get data:
87+
88+
```js
89+
// Add new data in cache
90+
cache.set("a", 10);
91+
92+
// Get data
93+
cache.get("a"); // 10
94+
```
95+
96+
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.
97+
98+
```js
99+
// Add new data in cache
100+
cache.set("a", undefined);
101+
102+
cache.get("a"); // undefined
103+
cache.get("b"); // undefined
104+
105+
// Set return value of get function
106+
cache.get("a", function (err, data) {
107+
if (err) return null;
108+
return data;
109+
}); // undefined
110+
111+
cache.get("b", function (err, data) {
112+
if (err) return null;
113+
return data;
114+
}); // null
115+
```
116+
117+
#### Check data exists in cache
118+
119+
```js
120+
// Add new data in cache
121+
cache.set("a", undefined);
122+
123+
// Check data exists or not
124+
cache.has("a"); // true
125+
cache.has("b"); // false
126+
```
127+
128+
#### Delete data:
129+
130+
```js
131+
// Delete data
132+
cache.delete("a");
133+
```
134+
135+
#### Delete all data from cache:
136+
137+
```js
138+
// Delete all data
139+
cache.clear();
140+
```
141+
142+
#### Get all data from cache:
143+
144+
```js
145+
// Add new data in cache
146+
cache.set("a", 10);
147+
148+
// Get all data
149+
cache.forEach(function (data) {
150+
console.log(data); // {a: 10}
151+
});
152+
```
153+
154+
## License
155+
156+
[MIT License](https://github.com/routejs/router/blob/main/LICENSE)
157+
158+
[npm-downloads-image]: https://badgen.net/npm/dm/lrujs
159+
[npm-downloads-url]: https://npmcharts.com/compare/lrujs?minimal=true
160+
[npm-install-size-image]: https://badgen.net/packagephobia/install/lrujs
161+
[npm-install-size-url]: https://packagephobia.com/result?p=lrujs
162+
[npm-url]: https://npmjs.org/package/lrujs
163+
[npm-version-image]: https://badgen.net/npm/v/lrujs

index.cjs

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

index.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
declare class LRUCache {
2+
size: number;
3+
constructor(options?: { maxSize?: number; ttl?: number });
4+
set: (key: any, value: any, options?: { ttl?: number }) => void;
5+
get: (key: any, callback?: (err: Error | null, value: any) => any) => any;
6+
delete: (key: any) => void;
7+
clear: () => void;
8+
has: (key: any) => boolean;
9+
forEach: (callback: (element: object, index?: number) => void) => void;
10+
}
11+
12+
export default LRUCache;

index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "./src/cache.mjs";

package.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "lrujs",
3+
"version": "1.0.0",
4+
"description": "Fast and lightweight lru cache for javascript",
5+
"main": "index.mjs",
6+
"type": "module",
7+
"exports": {
8+
"types": "./index.d.ts",
9+
"node": {
10+
"import": "./index.mjs",
11+
"require": "./index.cjs"
12+
},
13+
"default": "./index.cjs"
14+
},
15+
"directories": {
16+
"test": "tests"
17+
},
18+
"scripts": {
19+
"test": "jest"
20+
},
21+
"repository": {
22+
"type": "git",
23+
"url": "git+https://github.com/opensnip/lrujs.git"
24+
},
25+
"keywords": [
26+
"opensnip",
27+
"lrujs",
28+
"lru cache",
29+
"cache",
30+
"nodejs",
31+
"library"
32+
],
33+
"author": "Rajkumar Dusad",
34+
"license": "MIT",
35+
"types": "./index.d.ts",
36+
"bugs": {
37+
"url": "https://github.com/opensnip/lrujs/issues"
38+
},
39+
"homepage": "https://github.com/opensnip/lrujs#readme",
40+
"devDependencies": {
41+
"jest": "^29.4.2"
42+
},
43+
"files": [
44+
"src/",
45+
"LICENSE",
46+
"README.md",
47+
"index.cjs",
48+
"index.mjs",
49+
"index.d.ts"
50+
]
51+
}

0 commit comments

Comments
 (0)