tiny-lru
Advanced tools
Comparing version 11.1.1 to 11.1.2
@@ -6,3 +6,3 @@ /** | ||
* @license BSD-3-Clause | ||
* @version 11.1.1 | ||
* @version 11.1.2 | ||
*/ | ||
@@ -137,6 +137,2 @@ class LRU { | ||
if (prev !== null) { | ||
prev.next = next; | ||
} | ||
if (next !== null) { | ||
@@ -143,0 +139,0 @@ next.prev = prev; |
/*! | ||
2023 Jason Mulligan <jason.mulligan@avoidwork.com> | ||
@version 11.1.1 | ||
@version 11.1.2 | ||
*/ | ||
class t{constructor(t=0,s=0,e=!1){this.first=null,this.items=Object.create(null),this.last=null,this.max=t,this.resetTtl=e,this.size=0,this.ttl=s}clear(){return this.first=null,this.items=Object.create(null),this.last=null,this.size=0,this}delete(t){if(this.has(t)){const s=this.items[t];delete this.items[t],this.size--,null!==s.prev&&(s.prev.next=s.next),null!==s.next&&(s.next.prev=s.prev),this.first===s&&(this.first=s.next),this.last===s&&(this.last=s.prev)}return this}entries(t=this.keys()){return t.map((t=>[t,this.get(t)]))}evict(t=!1){if(t||this.size>0){const t=this.first;delete this.items[t.key],0==--this.size?(this.first=null,this.last=null):(this.first=t.next,this.first.prev=null)}return this}expiresAt(t){let s;return this.has(t)&&(s=this.items[t].expiry),s}get(t){let s;if(this.has(t)){const e=this.items[t];this.ttl>0&&e.expiry<=Date.now()?this.delete(t):(s=e.value,this.set(t,s,!0))}return s}has(t){return t in this.items}keys(){return Object.keys(this.items)}set(t,s,e=!1,i=this.resetTtl){let h;if(e||this.has(t)){if(h=this.items[t],h.value=s,!1===e&&i&&(h.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.last!==h){const t=this.last,s=h.next,e=h.prev;this.first===h&&(this.first=h.next),h.next=null,h.prev=this.last,t.next=h,null!==e&&(e.next=s),null!==s&&(s.prev=e)}}else this.max>0&&this.size===this.max&&this.evict(!0),h=this.items[t]={expiry:this.ttl>0?Date.now()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:s},1==++this.size?this.first=h:this.last.next=h;return this.last=h,this}values(t=this.keys()){return t.map((t=>this.get(t)))}}function s(s=1e3,e=0,i=!1){if(isNaN(s)||s<0)throw new TypeError("Invalid max value");if(isNaN(e)||e<0)throw new TypeError("Invalid ttl value");if("boolean"!=typeof i)throw new TypeError("Invalid resetTtl value");return new t(s,e,i)}export{s as lru};//# sourceMappingURL=tiny-lru.min.js.map | ||
class t{constructor(t=0,s=0,e=!1){this.first=null,this.items=Object.create(null),this.last=null,this.max=t,this.resetTtl=e,this.size=0,this.ttl=s}clear(){return this.first=null,this.items=Object.create(null),this.last=null,this.size=0,this}delete(t){if(this.has(t)){const s=this.items[t];delete this.items[t],this.size--,null!==s.prev&&(s.prev.next=s.next),null!==s.next&&(s.next.prev=s.prev),this.first===s&&(this.first=s.next),this.last===s&&(this.last=s.prev)}return this}entries(t=this.keys()){return t.map((t=>[t,this.get(t)]))}evict(t=!1){if(t||this.size>0){const t=this.first;delete this.items[t.key],0==--this.size?(this.first=null,this.last=null):(this.first=t.next,this.first.prev=null)}return this}expiresAt(t){let s;return this.has(t)&&(s=this.items[t].expiry),s}get(t){let s;if(this.has(t)){const e=this.items[t];this.ttl>0&&e.expiry<=Date.now()?this.delete(t):(s=e.value,this.set(t,s,!0))}return s}has(t){return t in this.items}keys(){return Object.keys(this.items)}set(t,s,e=!1,i=this.resetTtl){let h;if(e||this.has(t)){if(h=this.items[t],h.value=s,!1===e&&i&&(h.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.last!==h){const t=this.last,s=h.next,e=h.prev;this.first===h&&(this.first=h.next),h.next=null,h.prev=this.last,t.next=h,null!==s&&(s.prev=e)}}else this.max>0&&this.size===this.max&&this.evict(!0),h=this.items[t]={expiry:this.ttl>0?Date.now()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:s},1==++this.size?this.first=h:this.last.next=h;return this.last=h,this}values(t=this.keys()){return t.map((t=>this.get(t)))}}function s(s=1e3,e=0,i=!1){if(isNaN(s)||s<0)throw new TypeError("Invalid max value");if(isNaN(e)||e<0)throw new TypeError("Invalid ttl value");if("boolean"!=typeof i)throw new TypeError("Invalid resetTtl value");return new t(s,e,i)}export{s as lru};//# sourceMappingURL=tiny-lru.min.js.map |
@@ -6,3 +6,3 @@ /** | ||
* @license BSD-3-Clause | ||
* @version 11.1.1 | ||
* @version 11.1.2 | ||
*/ | ||
@@ -137,6 +137,2 @@ (function(g,f){typeof exports==='object'&&typeof module!=='undefined'?f(exports):typeof define==='function'&&define.amd?define(['exports'],f):(g=typeof globalThis!=='undefined'?globalThis:g||self,f(g.lru={}));})(this,(function(exports){'use strict';class LRU { | ||
if (prev !== null) { | ||
prev.next = next; | ||
} | ||
if (next !== null) { | ||
@@ -143,0 +139,0 @@ next.prev = prev; |
/*! | ||
2023 Jason Mulligan <jason.mulligan@avoidwork.com> | ||
@version 11.1.1 | ||
@version 11.1.2 | ||
*/ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).lru={})}(this,(function(t){"use strict";class e{constructor(t=0,e=0,s=!1){this.first=null,this.items=Object.create(null),this.last=null,this.max=t,this.resetTtl=s,this.size=0,this.ttl=e}clear(){return this.first=null,this.items=Object.create(null),this.last=null,this.size=0,this}delete(t){if(this.has(t)){const e=this.items[t];delete this.items[t],this.size--,null!==e.prev&&(e.prev.next=e.next),null!==e.next&&(e.next.prev=e.prev),this.first===e&&(this.first=e.next),this.last===e&&(this.last=e.prev)}return this}entries(t=this.keys()){return t.map((t=>[t,this.get(t)]))}evict(t=!1){if(t||this.size>0){const t=this.first;delete this.items[t.key],0==--this.size?(this.first=null,this.last=null):(this.first=t.next,this.first.prev=null)}return this}expiresAt(t){let e;return this.has(t)&&(e=this.items[t].expiry),e}get(t){let e;if(this.has(t)){const s=this.items[t];this.ttl>0&&s.expiry<=Date.now()?this.delete(t):(e=s.value,this.set(t,e,!0))}return e}has(t){return t in this.items}keys(){return Object.keys(this.items)}set(t,e,s=!1,i=this.resetTtl){let l;if(s||this.has(t)){if(l=this.items[t],l.value=e,!1===s&&i&&(l.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.last!==l){const t=this.last,e=l.next,s=l.prev;this.first===l&&(this.first=l.next),l.next=null,l.prev=this.last,t.next=l,null!==s&&(s.next=e),null!==e&&(e.prev=s)}}else this.max>0&&this.size===this.max&&this.evict(!0),l=this.items[t]={expiry:this.ttl>0?Date.now()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:e},1==++this.size?this.first=l:this.last.next=l;return this.last=l,this}values(t=this.keys()){return t.map((t=>this.get(t)))}}t.lru=function(t=1e3,s=0,i=!1){if(isNaN(t)||t<0)throw new TypeError("Invalid max value");if(isNaN(s)||s<0)throw new TypeError("Invalid ttl value");if("boolean"!=typeof i)throw new TypeError("Invalid resetTtl value");return new e(t,s,i)}}));//# sourceMappingURL=tiny-lru.umd.min.js.map | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).lru={})}(this,(function(t){"use strict";class e{constructor(t=0,e=0,s=!1){this.first=null,this.items=Object.create(null),this.last=null,this.max=t,this.resetTtl=s,this.size=0,this.ttl=e}clear(){return this.first=null,this.items=Object.create(null),this.last=null,this.size=0,this}delete(t){if(this.has(t)){const e=this.items[t];delete this.items[t],this.size--,null!==e.prev&&(e.prev.next=e.next),null!==e.next&&(e.next.prev=e.prev),this.first===e&&(this.first=e.next),this.last===e&&(this.last=e.prev)}return this}entries(t=this.keys()){return t.map((t=>[t,this.get(t)]))}evict(t=!1){if(t||this.size>0){const t=this.first;delete this.items[t.key],0==--this.size?(this.first=null,this.last=null):(this.first=t.next,this.first.prev=null)}return this}expiresAt(t){let e;return this.has(t)&&(e=this.items[t].expiry),e}get(t){let e;if(this.has(t)){const s=this.items[t];this.ttl>0&&s.expiry<=Date.now()?this.delete(t):(e=s.value,this.set(t,e,!0))}return e}has(t){return t in this.items}keys(){return Object.keys(this.items)}set(t,e,s=!1,i=this.resetTtl){let l;if(s||this.has(t)){if(l=this.items[t],l.value=e,!1===s&&i&&(l.expiry=this.ttl>0?Date.now()+this.ttl:this.ttl),this.last!==l){const t=this.last,e=l.next,s=l.prev;this.first===l&&(this.first=l.next),l.next=null,l.prev=this.last,t.next=l,null!==e&&(e.prev=s)}}else this.max>0&&this.size===this.max&&this.evict(!0),l=this.items[t]={expiry:this.ttl>0?Date.now()+this.ttl:this.ttl,key:t,prev:this.last,next:null,value:e},1==++this.size?this.first=l:this.last.next=l;return this.last=l,this}values(t=this.keys()){return t.map((t=>this.get(t)))}}t.lru=function(t=1e3,s=0,i=!1){if(isNaN(t)||t<0)throw new TypeError("Invalid max value");if(isNaN(s)||s<0)throw new TypeError("Invalid ttl value");if("boolean"!=typeof i)throw new TypeError("Invalid resetTtl value");return new e(t,s,i)}}));//# sourceMappingURL=tiny-lru.umd.min.js.map |
{ | ||
"name": "tiny-lru", | ||
"description": "Tiny LRU cache for Client or Server", | ||
"version": "11.1.1", | ||
"version": "11.1.2", | ||
"homepage": "https://github.com/avoidwork/tiny-lru", | ||
@@ -37,3 +37,3 @@ "author": "Jason Mulligan <jason.mulligan@avoidwork.com>", | ||
"lint": "eslint --fix *.js src/*.js test/*.js", | ||
"mocha": "mocha test/*.js", | ||
"mocha": "nyc mocha test/*.js", | ||
"rollup": "rollup --config", | ||
@@ -44,9 +44,10 @@ "test": "npm run lint && npm run mocha", | ||
"devDependencies": { | ||
"@rollup/plugin-terser": "^0.4.0", | ||
"@rollup/plugin-terser": "^0.4.3", | ||
"auto-changelog": "^2.4.0", | ||
"eslint": "^8.36.0", | ||
"eslint": "^8.49.0", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0", | ||
"precise": "^2.0.1", | ||
"rollup": "^3.19.1", | ||
"typescript": "^5.0.2" | ||
"rollup": "^3.29.2", | ||
"typescript": "^5.2.2" | ||
}, | ||
@@ -53,0 +54,0 @@ "keywords": [ |
189
README.md
@@ -13,3 +13,3 @@ # Tiny LRU | ||
#### Example | ||
## Example | ||
```javascript | ||
@@ -21,74 +21,82 @@ _.memoize.Cache = lru().constructor; | ||
## clear | ||
### Method | ||
## Testing | ||
Clears the contents of the cache | ||
Tiny-LRU has 100% code coverage with its tests. | ||
return {Object} LRU instance | ||
```console | ||
--------------|---------|----------|---------|---------|------------------- | ||
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ||
--------------|---------|----------|---------|---------|------------------- | ||
All files | 100 | 89.85 | 100 | 100 | | ||
tiny-lru.cjs | 100 | 89.85 | 100 | 100 | 11-31,130-138,172 | ||
--------------|---------|----------|---------|---------|------------------- | ||
``` | ||
## API | ||
## Properties | ||
### first | ||
Item in "first" or "bottom" position; default is `null` | ||
**Example** | ||
```javascript | ||
cache.clear(); | ||
const cache = lru(); | ||
cache.first; // null - it's a new cache! | ||
``` | ||
## delete | ||
### Method | ||
### last | ||
Removes item from cache | ||
Item in "last" or "top" position; default is `null` | ||
param {String} key Item key | ||
return {Object} LRU instance | ||
**Example** | ||
```javascript | ||
cache.delete("myKey"); | ||
const cache = lru(); | ||
cache.last; // null - it's a new cache! | ||
``` | ||
## entries(*["key1", "key2"]*) | ||
### Method | ||
### max | ||
Returns an `Array` cache items | ||
Max items to hold in cache; default is `1000` | ||
param {Array} keys (Optional) Cache item keys to get | ||
return {Object} LRU instance | ||
**Example** | ||
```javascript | ||
cache.entries(['myKey1', 'myKey2']); | ||
const cache = lru(500); | ||
cache.max; // 500 | ||
``` | ||
## evict | ||
### Method | ||
### resetTtl | ||
Evicts the least recently used item from cache | ||
Resets `item.expiry` with each `set()` if `true`; default is `false` | ||
return {Object} LRU instance | ||
**Example** | ||
```javascript | ||
cache.evict(); | ||
const cache = lru(500, 5*6e4, true); | ||
cache.resetTtl; // true | ||
``` | ||
## expiresAt | ||
### Method | ||
### size | ||
Gets expiration time for cached item | ||
Number of items in cache | ||
param {String} key Item key | ||
return {Mixed} Undefined or number (epoch time) | ||
**Example** | ||
```javascript | ||
const item = cache.expiresAt("myKey"); | ||
const cache = lru(); | ||
cache.size; // 0 - it's a new cache! | ||
``` | ||
## first | ||
### Property | ||
### ttl | ||
Item in "first" or "bottom" position | ||
Milliseconds an item will remain in cache; lazy expiration upon next `get()` of an item | ||
@@ -98,27 +106,27 @@ **Example** | ||
```javascript | ||
const cache = lru(); | ||
const cache = lru(100, 3e4); | ||
cache.first; // null - it's a new cache! | ||
cache.ttl; // 30000; | ||
``` | ||
## get | ||
### Method | ||
## Methods | ||
Gets cached item and moves it to the front | ||
### clear | ||
param {String} key Item key | ||
return {Mixed} Undefined or Item value | ||
Clears the contents of the cache | ||
return {Object} LRU instance | ||
**Example** | ||
```javascript | ||
const item = cache.get("myKey"); | ||
cache.clear(); | ||
``` | ||
## has | ||
### Method | ||
### delete | ||
Returns a `Boolean` indicating if `key` is in cache | ||
Removes item from cache | ||
return {Object} LRU instance | ||
param {String} key Item key | ||
return {Object} LRU instance | ||
@@ -128,11 +136,11 @@ **Example** | ||
```javascript | ||
cache.has('myKey'); | ||
cache.delete("myKey"); | ||
``` | ||
## keys | ||
### Method | ||
### entries(*["key1", "key2"]*) | ||
Returns an `Array` of cache item keys. | ||
Returns an `Array` cache items | ||
return {Array} Array of keys | ||
param {Array} keys (Optional) Cache item keys to get | ||
return {Object} LRU instance | ||
@@ -142,52 +150,48 @@ **Example** | ||
```javascript | ||
console.log(cache.keys()); | ||
cache.entries(['myKey1', 'myKey2']); | ||
``` | ||
## max | ||
### Property | ||
### evict | ||
Max items to hold in cache (1000) | ||
Evicts the least recently used item from cache | ||
return {Object} LRU instance | ||
**Example** | ||
```javascript | ||
const cache = lru(500); | ||
cache.max; // 500 | ||
cache.evict(); | ||
``` | ||
## last | ||
### Property | ||
### expiresAt | ||
Item in "last" or "top" position | ||
Gets expiration time for cached item | ||
param {String} key Item key | ||
return {Mixed} Undefined or number (epoch time) | ||
**Example** | ||
```javascript | ||
const cache = lru(); | ||
cache.last; // null - it's a new cache! | ||
const item = cache.expiresAt("myKey"); | ||
``` | ||
## resetTtl | ||
### Property | ||
### get | ||
Resets `item.expiry` with each `set()` if `true` (false) | ||
Gets cached item and moves it to the front | ||
param {String} key Item key | ||
return {Mixed} Undefined or Item value | ||
**Example** | ||
```javascript | ||
const cache = lru(); | ||
cache.resetTtl; // false | ||
const item = cache.get("myKey"); | ||
``` | ||
## set | ||
### Method | ||
### has | ||
Sets item in cache as `first` | ||
Returns a `Boolean` indicating if `key` is in cache | ||
param {String} key Item key | ||
param {Mixed} value Item value | ||
return {Object} LRU instance | ||
return {Object} LRU instance | ||
@@ -197,33 +201,32 @@ **Example** | ||
```javascript | ||
cache.set("myKey", {prop: true}); | ||
cache.has('myKey'); | ||
``` | ||
## size | ||
### Property | ||
### keys | ||
Number of items in cache | ||
Returns an `Array` of cache item keys. | ||
return {Array} Array of keys | ||
**Example** | ||
```javascript | ||
const cache = lru(); | ||
cache.size; // 0 - it's a new cache! | ||
console.log(cache.keys()); | ||
``` | ||
## ttl | ||
### Property | ||
### set | ||
Milliseconds an item will remain in cache; lazy expiration upon next `get()` of an item | ||
Sets item in cache as `first` | ||
param {String} key Item key | ||
param {Mixed} value Item value | ||
return {Object} LRU instance | ||
**Example** | ||
```javascript | ||
const cache = lru(); | ||
cache.ttl = 3e4; | ||
cache.set("myKey", {prop: true}); | ||
``` | ||
## values(*["key1", "key2"]*) | ||
### Method | ||
### values(*["key1", "key2"]*) | ||
@@ -230,0 +233,0 @@ Returns an `Array` cache items |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
35569
241
8
476