UNPKG

5.41 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");
46
47// Get data from cache
48console.log(cache.get("a"));
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());
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 object
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```js
71const cache = new Cache(options);
72```
73
74Where 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
81Cachejs 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
83Example:
84```js
85const Cache = require("@opensnip/cachejs");
86
87// Create cache object
88const cache = new Cache({
89 evictionPolicy: "LRU",
90 maxLength: 10,
91 ttl: 100,
92 interval: 1000,
93});
94```
95
96## Set a new data
97
98In 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
102cache.set("a", 10);
103
104// Add new data in cache
105cache.set("user", { name: "abc" });
106
107// Add duplicate data
108cache.set("a", 20); // Replace the old value
109
110## Set ttl for single data
111
112By 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
116cache.set("b", 10, { ttl: 200 }); // Expires after 200 ms
117```
118
119## Get data from cache
120
121By 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
125cache.set("a", 10);
126
127// Get data
128cache.get("a"); // 10
129```
130
131Customize cache miss value:
132
133```js
134// Add new data in cache
135cache.set("a", undefined);
136
137cache.get("a"); // undefined
138cache.get("b"); // undefined
139
140// Set custom return value
141cache.get("a", function (err, value) {
142 if (err) return null;
143 return value;
144}); // undefined
145
146cache.get("b", function (err, value) {
147 if (err) return null;
148 return value;
149}); // null
150```
151
152## Check data exists in cache
153
154Check weather item exists in the cache or not.
155
156```js
157// Add new data in cache
158cache.set("a", undefined);
159
160// Check data exists or not
161cache.has("a"); // true
162cache.has("b"); // false
163```
164
165## Delete data from cache
166
167Remove data from cache.
168
169```js
170// Delete data
171cache.delete("a");
172```
173
174## Delete all data from cache
175
176Remove all data from the cache.
177
178```js
179// Delete all data
180cache.clear();
181```
182
183## Get all data from cache
184
185Get all data from the cache.
186
187```js
188// Add new data in cache
189cache.set("a", 10);
190
191// Get all data
192cache.forEach(function (data) {
193 console.log(data); // { a: 10 }
194});
195
196// OR
197
198for (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
207cache.set("a", 10);
208
209// Get all data
210console.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