Skip to content

Commit 1710557

Browse files
committed
cachejs
1 parent e5f6482 commit 1710557

25 files changed

+3409
-0
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: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
## Cachejs
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+
Cachejs is a fast and lightweight caching library for javascript.
8+
9+
## Features
10+
11+
- Super Fast
12+
- Lightweight
13+
- Multiple cache eviction policy (FIFO, LIFO, LRU, MRU)
14+
- TTL support
15+
- Custom cache-miss value
16+
17+
## Installation
18+
19+
Install using npm:
20+
21+
```console
22+
$ npm i @opensnip/cachejs
23+
```
24+
25+
Install using yarn:
26+
27+
```console
28+
$ yarn add @opensnip/cachejs
29+
```
30+
31+
## Example
32+
33+
A simple cachejs cache example:
34+
35+
```js
36+
const Cache = require("@opensnip/cachejs");
37+
38+
// Create cache object
39+
const cache = new Cache();
40+
41+
// Add data in cache
42+
cache.set("a", 10);
43+
44+
// Check data exists in cache
45+
cache.has("a");
46+
47+
// Get data from cache
48+
console.log(cache.get("a"));
49+
50+
// Get all data from cache
51+
cache.forEach(function (data) {
52+
console.log(data); // { a: 10 }
53+
});
54+
55+
// Get all data to array
56+
console.log(cache.toArray());
57+
58+
// Delete data from cache
59+
cache.delete("a");
60+
61+
// Delete all data from cache
62+
cache.clear();
63+
```
64+
65+
## Create a new cache object
66+
67+
To create a new cache we need to create a new instance of cachejs. While creating a new cache we can set the configuration like eviction policy, cache max length and ttl, but it is not mandatory and if we not set any configuration then the default values are used.
68+
69+
Defination:
70+
```js
71+
const cache = new Cache(options);
72+
```
73+
74+
Where options are the following:
75+
- `evictionPolicy` : eviction policy is can be any valid cache eviction policy, supported eviction policy are FIFO, LIFO, LRU, MRU
76+
- `maxLength` : max length is a 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.
77+
- `ttl` : is cache expires time in milliseconds, the default value is 0 and if value if 0 it will not check the ttl.
78+
- `interval` : 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.
79+
- `enableInterval` : enableInterval is a 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.
80+
81+
Cachejs 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.
82+
83+
Example:
84+
```js
85+
const Cache = require("@opensnip/cachejs");
86+
87+
// Create cache object
88+
const cache = new Cache({
89+
evictionPolicy: "LRU",
90+
maxLength: 10,
91+
ttl: 100,
92+
interval: 1000,
93+
});
94+
```
95+
96+
## Set a new data
97+
98+
In cachejs 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.
99+
100+
```js
101+
// Add new data in cache
102+
cache.set("a", 10);
103+
104+
// Add new data in cache
105+
cache.set("user", { name: "abc" });
106+
107+
// Add duplicate data
108+
cache.set("a", 20); // Replace the old value
109+
110+
## Set ttl for single data
111+
112+
By default the configuration TTL value is used for every item, but we can set TTL for a single item.
113+
114+
```js
115+
// Add new data in cache
116+
cache.set("b", 10, { ttl: 200 }); // Expires after 200 ms
117+
```
118+
119+
## Get data from cache
120+
121+
By default on cache miss cachejs returns undefined value, but undefined also can be used as a value for item. In this case you can return a custom value on cache miss.
122+
123+
```js
124+
// Add new data in cache
125+
cache.set("a", 10);
126+
127+
// Get data
128+
cache.get("a"); // 10
129+
```
130+
131+
Customize cache miss value:
132+
133+
```js
134+
// Add new data in cache
135+
cache.set("a", undefined);
136+
137+
cache.get("a"); // undefined
138+
cache.get("b"); // undefined
139+
140+
// Set custom return value
141+
cache.get("a", function (err, value) {
142+
if (err) return null;
143+
return value;
144+
}); // undefined
145+
146+
cache.get("b", function (err, value) {
147+
if (err) return null;
148+
return value;
149+
}); // null
150+
```
151+
152+
## Check data exists in cache
153+
154+
Check weather item exists in the cache or not.
155+
156+
```js
157+
// Add new data in cache
158+
cache.set("a", undefined);
159+
160+
// Check data exists or not
161+
cache.has("a"); // true
162+
cache.has("b"); // false
163+
```
164+
165+
## Delete data from cache
166+
167+
Remove data from cache.
168+
169+
```js
170+
// Delete data
171+
cache.delete("a");
172+
```
173+
174+
## Delete all data from cache
175+
176+
Remove all data from the cache.
177+
178+
```js
179+
// Delete all data
180+
cache.clear();
181+
```
182+
183+
## Get all data from cache
184+
185+
Get all data from the cache.
186+
187+
```js
188+
// Add new data in cache
189+
cache.set("a", 10);
190+
191+
// Get all data
192+
cache.forEach(function (data) {
193+
console.log(data); // { a: 10 }
194+
});
195+
196+
// OR
197+
198+
for (let data of cache) {
199+
console.log(data); // { a: 10 }
200+
}
201+
```
202+
203+
## Get data as array
204+
205+
```js
206+
// Add new data in cache
207+
cache.set("a", 10);
208+
209+
// Get all data
210+
console.log(cache.toArray()); // [ { a: 10 } ]
211+
```
212+
213+
## License
214+
215+
[MIT License](https://github.com/opensnip/cachejs/blob/main/LICENSE)
216+
217+
[npm-downloads-image]: https://badgen.net/npm/dm/@opensnip/cachejs
218+
[npm-downloads-url]: https://npmcharts.com/compare/@opensnip/cachejs?minimal=true
219+
[npm-install-size-image]: https://badgen.net/packagephobia/install/@opensnip/cachejs
220+
[npm-install-size-url]: https://packagephobia.com/result?p=@opensnip/cachejs
221+
[npm-url]: https://npmjs.org/package/@opensnip/cachejs
222+
[npm-version-image]: https://badgen.net/npm/v/@opensnip/cachejs

index.cjs

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

index.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
declare type cacheEvictionPolicy = "FIFO" | "LIFO" | "LRU" | "MRU";
2+
3+
declare class Cache {
4+
length: number;
5+
constructor(options?: {
6+
evictionPolicy?: cacheEvictionPolicy;
7+
maxLength?: number;
8+
ttl?: number;
9+
interval?: number;
10+
enableInterval?: boolean;
11+
});
12+
set: (key: any, value: any, options?: { ttl?: number }) => void;
13+
startInterval: () => void;
14+
clearInterval: () => void;
15+
get: (
16+
key: any,
17+
callback?: (err: Error | undefined, value: any) => any
18+
) => any;
19+
delete: (key: any) => void;
20+
clear: () => void;
21+
has: (key: any) => boolean;
22+
forEach: (callback: (element: object, index?: number) => void) => void;
23+
toArray: () => any[];
24+
}
25+
26+
export default Cache;

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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "@opensnip/cachejs",
3+
"version": "1.0.0",
4+
"description": "Fast and lightweight caching library 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/cachejs.git"
24+
},
25+
"keywords": [
26+
"opensnip",
27+
"cachejs",
28+
"lru cache",
29+
"mru cache",
30+
"lfu cache",
31+
"mfu cache",
32+
"fifo cache",
33+
"lifo cache",
34+
"cache",
35+
"nodejs",
36+
"library"
37+
],
38+
"author": "Rajkumar Dusad",
39+
"license": "MIT",
40+
"types": "./index.d.ts",
41+
"bugs": {
42+
"url": "https://github.com/opensnip/cachejs/issues"
43+
},
44+
"homepage": "https://github.com/opensnip/cachejs#readme",
45+
"devDependencies": {
46+
"jest": "^29.4.2"
47+
},
48+
"files": [
49+
"src/",
50+
"LICENSE",
51+
"README.md",
52+
"index.cjs",
53+
"index.mjs",
54+
"index.d.ts"
55+
]
56+
}

0 commit comments

Comments
 (0)