|
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 |
0 commit comments