@acuris/leprechaun-cache
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -22,3 +22,2 @@ import { Cacheable, LeprechaunCacheOptions } from './types'; | ||
private spinLock; | ||
private getLock; | ||
private updateCache; | ||
@@ -25,0 +24,0 @@ private unlock; |
@@ -46,3 +46,3 @@ "use strict"; | ||
async refresh(key) { | ||
return this.updateCache(key, this.softTtlMs, true); | ||
return this.updateCache(key, this.softTtlMs); | ||
} | ||
@@ -52,3 +52,3 @@ async doGet(key, ttl) { | ||
if (!result) { | ||
return this.updateCache(key, ttl, true); | ||
return this.updateCache(key, ttl); | ||
} | ||
@@ -58,3 +58,9 @@ if (result.expiresAt > Date.now()) { | ||
} | ||
const update = this.updateCache(key, ttl, !this.returnStale); | ||
const update = this.updateCache(key, ttl).catch(e => { | ||
if (this.returnStale) { | ||
this.onBackgroundError(e); | ||
return result.data; | ||
} | ||
throw e; | ||
}); | ||
if (!this.returnStale) { | ||
@@ -94,12 +100,4 @@ return update; | ||
} | ||
async getLock(key, doSpinLock) { | ||
return doSpinLock | ||
? this.spinLock(key) | ||
: { | ||
lockId: (await this.cacheStore.lock(this.keyPrefix + key, this.lockTtlMs)) || '', | ||
didSpin: false | ||
}; | ||
} | ||
async updateCache(key, ttl, doSpinLock) { | ||
const lock = await this.getLock(key, doSpinLock); | ||
async updateCache(key, ttl) { | ||
const lock = await this.spinLock(key); | ||
if (!lock.lockId) { | ||
@@ -106,0 +104,0 @@ throw new Error('unable to acquire lock and no data in cache'); |
{ | ||
"name": "@acuris/leprechaun-cache", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "Caching library that supports double checked caching and stale returns to avoid stampede and slow responses", |
@@ -79,3 +79,3 @@ import { CacheStore, Cacheable, OnCacheMiss, LeprechaunCacheOptions, CacheItem } from './types' | ||
public async refresh(key: string): Promise<T> { | ||
return this.updateCache(key, this.softTtlMs, true) | ||
return this.updateCache(key, this.softTtlMs) | ||
} | ||
@@ -86,3 +86,3 @@ | ||
if (!result) { | ||
return this.updateCache(key, ttl, true) | ||
return this.updateCache(key, ttl) | ||
} | ||
@@ -94,4 +94,11 @@ | ||
const update = this.updateCache(key, ttl, !this.returnStale) | ||
const update = this.updateCache(key, ttl).catch(e => { | ||
if (this.returnStale) { | ||
this.onBackgroundError(e) | ||
return result.data | ||
} | ||
throw e | ||
}) | ||
if (!this.returnStale) { | ||
@@ -138,14 +145,5 @@ return update | ||
private async getLock(key: string, doSpinLock: boolean): Promise<LockResult> { | ||
return doSpinLock | ||
? this.spinLock(key) | ||
: { | ||
lockId: (await this.cacheStore.lock(this.keyPrefix + key, this.lockTtlMs)) || '', | ||
didSpin: false | ||
} | ||
} | ||
private async updateCache(key: string, ttl: number): Promise<T> { | ||
const lock = await this.spinLock(key) | ||
private async updateCache(key: string, ttl: number, doSpinLock: boolean): Promise<T> { | ||
const lock = await this.getLock(key, doSpinLock) | ||
if (!lock.lockId) { | ||
@@ -152,0 +150,0 @@ throw new Error('unable to acquire lock and no data in cache') |
Sorry, the diff of this file is not supported yet
42044
693