cacheable-lookup
Advanced tools
Comparing version 4.0.1 to 4.1.0
@@ -9,4 +9,18 @@ import Keyv = require('keyv'); | ||
type TPromise<T> = T | Promise<T>; | ||
export interface CacheInstance { | ||
set(hostname: string, entries: EntryObject[], ttl: number): TPromise<void | boolean>; | ||
get(hostname: string): TPromise<EntryObject[] | undefined>; | ||
delete(hostname: string): TPromise<boolean>; | ||
clear(): TPromise<void>; | ||
} | ||
export interface Options { | ||
/** | ||
* Custom cache instance. If `undefined`, it will create a new one. | ||
* @default undefined | ||
*/ | ||
cache?: CacheInstance; | ||
/** | ||
* Limits the cache time (TTL). If set to `0`, it will make a new DNS query each time. | ||
@@ -13,0 +27,0 @@ * @default Infinity |
{ | ||
"name": "cacheable-lookup", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "A cacheable dns.lookup(…) that respects the TTL", | ||
@@ -12,4 +12,4 @@ "engines": { | ||
], | ||
"main": "source/index.js", | ||
"types": "index.d.ts", | ||
"main": "source/index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
@@ -39,2 +39,3 @@ "test": "xo && nyc ava && tsd" | ||
"coveralls": "^3.0.9", | ||
"keyv": "^4.0.0", | ||
"nyc": "^15.0.0", | ||
@@ -41,0 +42,0 @@ "proxyquire": "^2.1.3", |
@@ -46,2 +46,11 @@ # cacheable-lookup | ||
#### cache | ||
Type: [`TTLMap`](index.d.ts) | [`Keyv`](https://github.com/lukechilds/keyv/)<br> | ||
Default: `new TTLMap()` | ||
Custom cache instance. If `undefined`, it will create a new one. | ||
**Note**: If you decide to use Keyv instead of the native implementation, the performance will drop by 10x. Memory leaks may occur as it doesn't provide any way to remove all the deprecated values at once. | ||
#### options | ||
@@ -48,0 +57,0 @@ |
@@ -78,3 +78,3 @@ 'use strict'; | ||
this.values.delete(key); | ||
this.expiries.delete(key); | ||
return this.expiries.delete(key); | ||
} | ||
@@ -95,7 +95,12 @@ | ||
class CacheableLookup { | ||
constructor({maxTtl = Infinity, resolver, customHostsPath} = {}) { | ||
constructor({ | ||
cache = new TTLMap(), | ||
maxTtl = Infinity, | ||
resolver = new AsyncResolver(), | ||
customHostsPath | ||
} = {}) { | ||
this.maxTtl = maxTtl; | ||
this._cache = new TTLMap(); | ||
this._resolver = resolver || new AsyncResolver(); | ||
this._cache = cache; | ||
this._resolver = resolver; | ||
@@ -186,3 +191,3 @@ if (this._resolver instanceof AsyncResolver) { | ||
async query(hostname) { | ||
let cached = this._hostsResolver.hosts[hostname] || this._cache.get(hostname); | ||
let cached = this._hostsResolver.hosts[hostname] || await this._cache.get(hostname); | ||
@@ -229,3 +234,3 @@ if (!cached || cached.length === 0) { | ||
if (this.maxTtl > 0 && cacheTtl > 0) { | ||
this._cache.set(hostname, entries, cacheTtl); | ||
await this._cache.set(hostname, entries, cacheTtl); | ||
} | ||
@@ -245,7 +250,9 @@ | ||
const now = Date.now(); | ||
if (this._cache instanceof TTLMap) { | ||
const now = Date.now(); | ||
for (const [hostname, expiry] of this._cache.expiries) { | ||
if (now > expiry) { | ||
this._cache.delete(hostname); | ||
for (const [hostname, expiry] of this._cache.expiries) { | ||
if (now > expiry) { | ||
this._cache.delete(hostname); | ||
} | ||
} | ||
@@ -252,0 +259,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22209
441
222
9