another-cache-wrapper
Advanced tools
Comparing version
@@ -17,3 +17,4 @@ "use strict"; | ||
} | ||
if (!index_1.default.isEmpty(prefixes)) { | ||
if (!index_1.default.isNil(prefixes)) { | ||
prefixes = index_1.default.flatten([prefixes]); | ||
prefixes = index_1.default.join(prefixes, '_'); | ||
@@ -20,0 +21,0 @@ prefixes = index_1.default.toUpper(prefixes); |
@@ -37,36 +37,40 @@ "use strict"; | ||
function cachedMethodCall(prefix, method, keyExtractor = index_1.default.identity, ttl = exports.DEFAULT_TTL) { | ||
return () => __awaiter(this, arguments, void 0, function* () { | ||
const cache = yield factory_1.default(); | ||
// extract the args to compose the cache key | ||
const partial = index_1.default.attempt(keyExtractor, arguments); | ||
// if this method fails return live result | ||
// like if it wasn't wrapped | ||
if (index_1.default.isError(partial)) { | ||
Logger_1.default.error('cache :: error while extracting cachekey from args', partial); | ||
// fallback to call unwrapped method | ||
return method.apply(null, arguments); | ||
} | ||
// workout cache key for this params | ||
const cacheKey = cacheKey_1.default(partial, prefix); | ||
// lookup in cache | ||
const cachedResult = yield cache.get(cacheKey); | ||
// if result is found in cache | ||
if (!index_1.default.isNil(cachedResult)) { | ||
// return it | ||
return cachedResult; | ||
} | ||
// otherwise call the function | ||
const liveResult = index_1.default.attempt(method, arguments); | ||
// if it raises an error | ||
if (index_1.default.isError(liveResult)) { | ||
// "return" it | ||
throw liveResult; | ||
} | ||
// otherwise cache the result | ||
// for next call | ||
yield cache.set(cacheKey, liveResult, ttl); | ||
return liveResult; | ||
}); | ||
return function () { | ||
return __awaiter(this, arguments, void 0, function* () { | ||
const args = Array.prototype.slice.call(arguments, 0); | ||
const cache = yield factory_1.default(); | ||
// extract the args to compose the cache key | ||
const partial = index_1.default.attempt(keyExtractor, args); | ||
// if this method fails return live result | ||
// like if it wasn't wrapped | ||
if (index_1.default.isError(partial)) { | ||
Logger_1.default.error('cache :: error while extracting cachekey from args', partial); | ||
// fallback to call unwrapped method | ||
return method.apply(null, args); | ||
} | ||
// workout cache key for this params | ||
const cacheKey = cacheKey_1.default(partial, prefix); | ||
// lookup in cache | ||
const cachedResult = yield cache.get(cacheKey); | ||
// if result is found in cache | ||
if (!index_1.default.isNil(cachedResult)) { | ||
Logger_1.default.debug('cache :: found cached result for', cacheKey); | ||
// return it | ||
return cachedResult; | ||
} | ||
// otherwise call the function | ||
const liveResult = index_1.default.attempt(method, args); | ||
// if it raises an error | ||
if (index_1.default.isError(liveResult)) { | ||
// "return" it | ||
throw liveResult; | ||
} | ||
// otherwise cache the result | ||
// for next call | ||
yield cache.set(cacheKey, liveResult, ttl); | ||
return liveResult; | ||
}); | ||
}; | ||
} | ||
exports.cachedMethodCall = cachedMethodCall; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "another-cache-wrapper", | ||
"version": "1.0.10", | ||
"version": "1.0.12", | ||
"description": "custom cache interface", | ||
@@ -9,3 +9,3 @@ "main": "dist/index.js", | ||
"build": "npx tsc", | ||
"publish": "increase-version --package-json=./package.json && git add ." | ||
"prepublish": "npx tsc && increase-version --package-json=./package.json && git add ." | ||
}, | ||
@@ -12,0 +12,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
53431
1.06%938
0.54%