ngx-cacheable
Advanced tools
Comparing version 1.2.4 to 1.2.5-beta.0
@@ -5,2 +5,3 @@ import { ICacheBusterConfig } from './ICacheBusterConfig'; | ||
import { IStorageStrategy } from './IStorageStrategy'; | ||
import { IAsyncStorageStrategy } from './IAsyncStorageStrategy'; | ||
export declare const DEFAULT_CACHE_RESOLVER: (oldParams: any[], newParams: any[]) => boolean; | ||
@@ -12,3 +13,3 @@ export declare type ICacheRequestResolver = (oldParameters: Array<any>, newParameters: Array<any>) => boolean; | ||
export declare const GlobalCacheConfig: { | ||
storageStrategy: new () => IStorageStrategy; | ||
storageStrategy: new () => IStorageStrategy | IAsyncStorageStrategy; | ||
globalCacheKey: string; | ||
@@ -15,0 +16,0 @@ promiseImplementation: (() => PromiseConstructorLike) | PromiseConstructorLike; |
{ | ||
"name": "ngx-cacheable", | ||
"version": "1.2.4", | ||
"version": "1.2.5-beta.0", | ||
"description": "Promise/Observable cache decorators", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -6,2 +6,76 @@ "use strict"; | ||
exports.promiseGlobalCacheBusterNotifier = new rxjs_1.Subject(); | ||
var getResponse = function (oldMethod, cacheKey, cacheConfig, context, cachePairs, _parameters, pendingCachePairs, storageStrategy, promiseImplementation) { | ||
var parameters = _parameters.map(function (param) { return param !== undefined ? JSON.parse(JSON.stringify(param)) : param; }); | ||
var _foundCachePair = cachePairs.find(function (cp) { | ||
return cacheConfig.cacheResolver(cp.parameters, parameters); | ||
}); | ||
var _foundPendingCachePair = pendingCachePairs.find(function (cp) { | ||
return cacheConfig.cacheResolver(cp.parameters, parameters); | ||
}); | ||
/** | ||
* check if maxAge is passed and cache has actually expired | ||
*/ | ||
if (cacheConfig.maxAge && _foundCachePair && _foundCachePair.created) { | ||
if (new Date().getTime() - new Date(_foundCachePair.created).getTime() > | ||
cacheConfig.maxAge) { | ||
/** | ||
* cache duration has expired - remove it from the cachePairs array | ||
*/ | ||
storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey); | ||
_foundCachePair = null; | ||
} | ||
else if (cacheConfig.slidingExpiration) { | ||
/** | ||
* renew cache duration | ||
*/ | ||
_foundCachePair.created = new Date(); | ||
storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey); | ||
} | ||
} | ||
if (_foundCachePair) { | ||
return promiseImplementation.resolve(_foundCachePair.response); | ||
} | ||
else if (_foundPendingCachePair) { | ||
return _foundPendingCachePair.response; | ||
} | ||
else { | ||
var response$ = oldMethod.call.apply(oldMethod, [context].concat(parameters)) | ||
.then(function (response) { | ||
removeCachePair(pendingCachePairs, parameters, cacheConfig); | ||
/** | ||
* if no maxCacheCount has been passed | ||
* if maxCacheCount has not been passed, just shift the cachePair to make room for the new one | ||
* if maxCacheCount has been passed, respect that and only shift the cachePairs if the new cachePair will make them exceed the count | ||
*/ | ||
if (!cacheConfig.shouldCacheDecider || | ||
cacheConfig.shouldCacheDecider(response)) { | ||
if (!cacheConfig.maxCacheCount || | ||
cacheConfig.maxCacheCount === 1 || | ||
(cacheConfig.maxCacheCount && | ||
cacheConfig.maxCacheCount < cachePairs.length + 1)) { | ||
storageStrategy.removeAtIndex(0, cacheKey); | ||
} | ||
storageStrategy.add({ | ||
parameters: parameters, | ||
response: response, | ||
created: cacheConfig.maxAge ? new Date() : null | ||
}, cacheKey); | ||
} | ||
return response; | ||
}) | ||
.catch(function (error) { | ||
removeCachePair(pendingCachePairs, parameters, cacheConfig); | ||
return promiseImplementation.reject(error); | ||
}); | ||
/** | ||
* cache the stream | ||
*/ | ||
pendingCachePairs.push({ | ||
parameters: parameters, | ||
response: response$, | ||
created: new Date() | ||
}); | ||
return response$; | ||
} | ||
}; | ||
var removeCachePair = function (cachePairs, parameters, cacheConfig) { | ||
@@ -52,78 +126,7 @@ /** | ||
if (!(cachePairs instanceof promiseImplementation)) { | ||
cachePairs = promiseImplementation.resolve(cachePairs); | ||
return getResponse(oldMethod, cacheKey, cacheConfig, this, cachePairs, _parameters, pendingCachePairs_1, storageStrategy_1, promiseImplementation); | ||
} | ||
return cachePairs.then(function (cachePairs) { | ||
var parameters = _parameters.map(function (param) { return param !== undefined ? JSON.parse(JSON.stringify(param)) : param; }); | ||
var _foundCachePair = cachePairs.find(function (cp) { | ||
return cacheConfig.cacheResolver(cp.parameters, parameters); | ||
}); | ||
var _foundPendingCachePair = pendingCachePairs_1.find(function (cp) { | ||
return cacheConfig.cacheResolver(cp.parameters, parameters); | ||
}); | ||
/** | ||
* check if maxAge is passed and cache has actually expired | ||
*/ | ||
if (cacheConfig.maxAge && _foundCachePair && _foundCachePair.created) { | ||
if (new Date().getTime() - new Date(_foundCachePair.created).getTime() > | ||
cacheConfig.maxAge) { | ||
/** | ||
* cache duration has expired - remove it from the cachePairs array | ||
*/ | ||
storageStrategy_1.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey); | ||
_foundCachePair = null; | ||
} | ||
else if (cacheConfig.slidingExpiration) { | ||
/** | ||
* renew cache duration | ||
*/ | ||
_foundCachePair.created = new Date(); | ||
storageStrategy_1.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey); | ||
} | ||
} | ||
if (_foundCachePair) { | ||
return promiseImplementation.resolve(_foundCachePair.response); | ||
} | ||
else if (_foundPendingCachePair) { | ||
return _foundPendingCachePair.response; | ||
} | ||
else { | ||
var response$ = oldMethod.call.apply(oldMethod, [_this].concat(parameters)) | ||
.then(function (response) { | ||
removeCachePair(pendingCachePairs_1, parameters, cacheConfig); | ||
/** | ||
* if no maxCacheCount has been passed | ||
* if maxCacheCount has not been passed, just shift the cachePair to make room for the new one | ||
* if maxCacheCount has been passed, respect that and only shift the cachePairs if the new cachePair will make them exceed the count | ||
*/ | ||
if (!cacheConfig.shouldCacheDecider || | ||
cacheConfig.shouldCacheDecider(response)) { | ||
if (!cacheConfig.maxCacheCount || | ||
cacheConfig.maxCacheCount === 1 || | ||
(cacheConfig.maxCacheCount && | ||
cacheConfig.maxCacheCount < cachePairs.length + 1)) { | ||
storageStrategy_1.removeAtIndex(0, cacheKey); | ||
} | ||
storageStrategy_1.add({ | ||
parameters: parameters, | ||
response: response, | ||
created: cacheConfig.maxAge ? new Date() : null | ||
}, cacheKey); | ||
} | ||
return response; | ||
}) | ||
.catch(function (error) { | ||
removeCachePair(pendingCachePairs_1, parameters, cacheConfig); | ||
return Promise.reject(error); | ||
}); | ||
/** | ||
* cache the stream | ||
*/ | ||
pendingCachePairs_1.push({ | ||
parameters: parameters, | ||
response: response$, | ||
created: new Date() | ||
}); | ||
return response$; | ||
} | ||
}); | ||
else { | ||
return cachePairs.then(function (cachePairs) { return getResponse(oldMethod, cacheKey, cacheConfig, _this, cachePairs, _parameters, pendingCachePairs_1, storageStrategy_1, promiseImplementation); }); | ||
} | ||
}; | ||
@@ -130,0 +133,0 @@ } |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
@@ -52,4 +62,36 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
var InMemoryStorageStrategy_1 = require("../common/InMemoryStorageStrategy"); | ||
var IAsyncStorageStrategy_1 = require("../common/IAsyncStorageStrategy"); | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000; | ||
var strategies = [null, DOMStorageStrategy_1.DOMStorageStrategy]; | ||
var AsyncStorageStrategy = /** @class */ (function (_super) { | ||
__extends(AsyncStorageStrategy, _super); | ||
function AsyncStorageStrategy() { | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.cachePairs = []; | ||
return _this; | ||
} | ||
AsyncStorageStrategy.prototype.add = function (cachePair) { | ||
this.cachePairs.push(cachePair); | ||
return Promise.resolve(); | ||
}; | ||
; | ||
AsyncStorageStrategy.prototype.updateAtIndex = function (index, entity) { | ||
var updatee = this.cachePairs[index]; | ||
Object.assign(updatee, entity); | ||
return Promise.resolve(); | ||
}; | ||
AsyncStorageStrategy.prototype.getAll = function () { | ||
return Promise.resolve(this.cachePairs); | ||
}; | ||
; | ||
AsyncStorageStrategy.prototype.removeAtIndex = function (index) { | ||
this.cachePairs.splice(index, 1); | ||
return Promise.resolve(); | ||
}; | ||
AsyncStorageStrategy.prototype.removeAll = function () { | ||
this.cachePairs.length = 0; | ||
return Promise.resolve(); | ||
}; | ||
return AsyncStorageStrategy; | ||
}(IAsyncStorageStrategy_1.IAsyncStorageStrategy)); | ||
var strategies = [null, AsyncStorageStrategy, DOMStorageStrategy_1.DOMStorageStrategy]; | ||
strategies.forEach(function (s) { | ||
@@ -56,0 +98,0 @@ var cacheBusterNotifier = new rxjs_1.Subject(); |
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
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
180547
2291
0
2