UNPKG

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