@algolia/cache-common
Advanced tools
Comparing version 4.0.0-beta.9 to 4.0.0-beta.10
@@ -5,2 +5,31 @@ 'use strict'; | ||
// @todo Add logger on options to debug when caches go wrong. | ||
function createFallbackableCache(options) { | ||
const caches = [...options.caches]; | ||
// eslint-disable-next-line functional/immutable-data | ||
const current = caches.shift(); | ||
if (current === undefined) { | ||
return createNullCache(); | ||
} | ||
return { | ||
get(key, defaultValue, events = { | ||
miss: () => Promise.resolve(), | ||
}) { | ||
return current.get(key, defaultValue, events).catch(() => { | ||
return createFallbackableCache({ caches }).get(key, defaultValue, events); | ||
}); | ||
}, | ||
set(key, value) { | ||
return current.set(key, value).catch(() => { | ||
return createFallbackableCache({ caches }).set(key, value); | ||
}); | ||
}, | ||
delete(key) { | ||
return current.delete(key).catch(() => { | ||
return createFallbackableCache({ caches }).delete(key); | ||
}); | ||
}, | ||
}; | ||
} | ||
function createNullCache() { | ||
@@ -13,6 +42,4 @@ return { | ||
return value | ||
.then(result => { | ||
return Promise.all([events.miss(result), result]); | ||
}) | ||
.then(results => results[1]); | ||
.then(result => Promise.all([result, events.miss(result)])) | ||
.then(([result]) => result); | ||
}, | ||
@@ -25,8 +52,6 @@ set(_key, value) { | ||
}, | ||
clear() { | ||
return Promise.resolve(); | ||
}, | ||
}; | ||
} | ||
exports.createFallbackableCache = createFallbackableCache; | ||
exports.createNullCache = createNullCache; |
@@ -0,7 +1,7 @@ | ||
import { Cache as Cache_2 } from '@algolia/cache-common'; | ||
export declare type Cache = { | ||
readonly get: <TValue>(key: object, defaultValue: () => Readonly<Promise<TValue>>, events?: CacheEvents<TValue>) => Readonly<Promise<TValue>>; | ||
readonly set: <TValue>(key: object, value: TValue) => Readonly<Promise<TValue>>; | ||
readonly delete: (key: object) => Readonly<Promise<void>>; | ||
readonly clear: () => Readonly<Promise<void>>; | ||
readonly get: <TValue>(key: object | string, defaultValue: () => Readonly<Promise<TValue>>, events?: CacheEvents<TValue>) => Readonly<Promise<TValue>>; | ||
readonly set: <TValue>(key: object | string, value: TValue) => Readonly<Promise<TValue>>; | ||
readonly delete: (key: object | string) => Readonly<Promise<void>>; | ||
}; | ||
@@ -13,4 +13,10 @@ | ||
export declare function createFallbackableCache(options: FallbackableCacheOptions): Cache; | ||
export declare function createNullCache(): Cache; | ||
export declare type FallbackableCacheOptions = { | ||
readonly caches: readonly Cache_2[]; | ||
}; | ||
export { } |
@@ -0,1 +1,30 @@ | ||
// @todo Add logger on options to debug when caches go wrong. | ||
function createFallbackableCache(options) { | ||
const caches = [...options.caches]; | ||
// eslint-disable-next-line functional/immutable-data | ||
const current = caches.shift(); | ||
if (current === undefined) { | ||
return createNullCache(); | ||
} | ||
return { | ||
get(key, defaultValue, events = { | ||
miss: () => Promise.resolve(), | ||
}) { | ||
return current.get(key, defaultValue, events).catch(() => { | ||
return createFallbackableCache({ caches }).get(key, defaultValue, events); | ||
}); | ||
}, | ||
set(key, value) { | ||
return current.set(key, value).catch(() => { | ||
return createFallbackableCache({ caches }).set(key, value); | ||
}); | ||
}, | ||
delete(key) { | ||
return current.delete(key).catch(() => { | ||
return createFallbackableCache({ caches }).delete(key); | ||
}); | ||
}, | ||
}; | ||
} | ||
function createNullCache() { | ||
@@ -8,6 +37,4 @@ return { | ||
return value | ||
.then(result => { | ||
return Promise.all([events.miss(result), result]); | ||
}) | ||
.then(results => results[1]); | ||
.then(result => Promise.all([result, events.miss(result)])) | ||
.then(([result]) => result); | ||
}, | ||
@@ -20,8 +47,5 @@ set(_key, value) { | ||
}, | ||
clear() { | ||
return Promise.resolve(); | ||
}, | ||
}; | ||
} | ||
export { createNullCache }; | ||
export { createFallbackableCache, createNullCache }; |
{ | ||
"name": "@algolia/cache-common", | ||
"version": "4.0.0-beta.9", | ||
"version": "4.0.0-beta.10", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "Common interfaces for promise-based caching libraries", |
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
4570
114