Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ngx-cacheable

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ngx-cacheable - npm Package Compare versions

Comparing version 1.3.0 to 1.3.1

specs/cat.d.ts

16

cacheable.decorator.js

@@ -53,5 +53,5 @@ "use strict";

*/
if (cacheConfig.maxAge && _foundCachePair && _foundCachePair.created) {
if ((cacheConfig.maxAge || common_1.GlobalCacheConfig.maxAge) && _foundCachePair && _foundCachePair.created) {
if (new Date().getTime() - new Date(_foundCachePair.created).getTime() >
cacheConfig.maxAge) {
(cacheConfig.maxAge || common_1.GlobalCacheConfig.maxAge)) {
/**

@@ -63,3 +63,3 @@ * cache duration has expired - remove it from the cachePairs array

}
else if (cacheConfig.slidingExpiration) {
else if (cacheConfig.slidingExpiration || common_1.GlobalCacheConfig.slidingExpiration) {
/**

@@ -95,6 +95,6 @@ * renew cache duration

cacheConfig.shouldCacheDecider(response)) {
if (!cacheConfig.maxCacheCount ||
cacheConfig.maxCacheCount === 1 ||
(cacheConfig.maxCacheCount &&
cacheConfig.maxCacheCount < cachePairs.length + 1)) {
if (!(cacheConfig.maxCacheCount || common_1.GlobalCacheConfig.maxCacheCount) ||
(cacheConfig.maxCacheCount || common_1.GlobalCacheConfig.maxCacheCount) === 1 ||
((cacheConfig.maxCacheCount || common_1.GlobalCacheConfig.maxCacheCount) &&
(cacheConfig.maxCacheCount || common_1.GlobalCacheConfig.maxCacheCount) < cachePairs.length + 1)) {
storageStrategy_1.removeAtIndex(0, cacheKey);

@@ -105,3 +105,3 @@ }

response: response,
created: cacheConfig.maxAge ? new Date() : null
created: (cacheConfig.maxAge || common_1.GlobalCacheConfig.maxAge) ? new Date() : null
}, cacheKey);

@@ -108,0 +108,0 @@ }

@@ -14,2 +14,5 @@ import { ICacheBusterConfig } from './ICacheBusterConfig';

export declare const GlobalCacheConfig: {
maxAge?: number;
slidingExpiration?: boolean;
maxCacheCount?: number;
cacheResolver?: ICacheResolver;

@@ -16,0 +19,0 @@ cacheHasher?: ICacheHasher;

{
"name": "ngx-cacheable",
"version": "1.3.0",
"version": "1.3.1",
"description": "Promise/Observable cache decorators",

@@ -5,0 +5,0 @@ "main": "./index.js",

@@ -17,5 +17,5 @@ "use strict";

*/
if (cacheConfig.maxAge && _foundCachePair && _foundCachePair.created) {
if ((cacheConfig.maxAge || common_1.GlobalCacheConfig.maxAge) && _foundCachePair && _foundCachePair.created) {
if (new Date().getTime() - new Date(_foundCachePair.created).getTime() >
cacheConfig.maxAge) {
(cacheConfig.maxAge || common_1.GlobalCacheConfig.maxAge)) {
/**

@@ -27,3 +27,3 @@ * cache duration has expired - remove it from the cachePairs array

}
else if (cacheConfig.slidingExpiration) {
else if (cacheConfig.slidingExpiration || common_1.GlobalCacheConfig.slidingExpiration) {
/**

@@ -53,6 +53,6 @@ * renew cache duration

cacheConfig.shouldCacheDecider(response)) {
if (!cacheConfig.maxCacheCount ||
cacheConfig.maxCacheCount === 1 ||
(cacheConfig.maxCacheCount &&
cacheConfig.maxCacheCount < cachePairs.length + 1)) {
if (!(cacheConfig.maxCacheCount || common_1.GlobalCacheConfig.maxCacheCount) ||
(cacheConfig.maxCacheCount || common_1.GlobalCacheConfig.maxCacheCount) === 1 ||
((cacheConfig.maxCacheCount || common_1.GlobalCacheConfig.maxCacheCount) &&
(cacheConfig.maxCacheCount || common_1.GlobalCacheConfig.maxCacheCount) < cachePairs.length + 1)) {
storageStrategy.removeAtIndex(0, cacheKey);

@@ -63,3 +63,3 @@ }

response: response,
created: cacheConfig.maxAge ? new Date() : null
created: (cacheConfig.maxAge || common_1.GlobalCacheConfig.maxAge) ? new Date() : null
}, cacheKey);

@@ -66,0 +66,0 @@ }

@@ -82,2 +82,49 @@ [![Build Status](https://travis-ci.org/angelnikolov/ngx-cacheable.svg?branch=master)](https://travis-ci.org/angelnikolov/ngx-cacheable)

## Global Configuration
Some of the local cache config options (passed to specific decorators) can also be used as global ones. All the local configurations will of course take precedence over the global ones.
Here are all the possible global configurations:
```ts
/**
* whether should use a sliding expiration strategy on caches
* this will reset the cache created property and keep the cache alive for @param maxAge milliseconds more
*/
slidingExpiration: boolean;
/**
* max cacheCount for different parameters
* @description maximum allowed unique caches (same parameters)
*/
maxCacheCount: number;
/**
* @description request cache resolver which will get old and new paramaters passed to and based on those
* will figure out if we need to bail out of cache or not
*/
cacheResolver: ICacheRequestResolver;
/**
* @description cache hasher which will be called to hash the parameters into a cache key
*/
cacheHasher: ICacheHasher;
/**
* @description cache decider that will figure out if the response should be cached or not, based on it
*/
shouldCacheDecider: IShouldCacheDecider;
/**
* maxAge of cache in milliseconds
* @description if time between method calls is larger - we bail out of cache
*/
maxAge: number;
/**
* @description storage strategy to be used for setting, evicting and updating cache.
*/
storageStrategy: new () => IStorageStrategy | IAsyncStorageStrategy;
/**
* @description global cache key which will be used to namespace the cache.
* for example, it will be used in the DOMStorageStrategy for now.
*/
globalCacheKey: string;
/**
* @description a custom promise implementation. For example, if you use angularjs, you might like to provide $q's promise here change detection still works properly.
*/
promiseImplementation: (() => PromiseConstructorLike) | PromiseConstructorLike;
```
## Examples

@@ -84,0 +131,0 @@ ### Cache Busting

@@ -55,3 +55,7 @@ "use strict";

var InMemoryStorageStrategy_1 = require("../common/InMemoryStorageStrategy");
var strategies = [null, DOMStorageStrategy_1.DOMStorageStrategy];
var cat_1 = require("./cat");
var strategies = [
null,
DOMStorageStrategy_1.DOMStorageStrategy
];
strategies.forEach(function (s) {

@@ -61,190 +65,2 @@ if (s) {

}
var cacheBusterNotifier = new rxjs_2.Subject();
var Cat = /** @class */ (function () {
function Cat() {
}
Cat.prototype.meow = function (phrase) { return this.name + ' says ' + phrase; };
;
return Cat;
}());
var Service = /** @class */ (function () {
function Service() {
}
Service.prototype.mockServiceCall = function (parameter) {
return rxjs_2.timer(1000).pipe(operators_2.mapTo({ payload: parameter }));
};
Service.prototype.mockSaveServiceCall = function () {
return rxjs_2.timer(1000).pipe(operators_2.mapTo('SAVED'));
};
Service.prototype.mockServiceCallWithMultipleParameters = function (parameter1, parameter2) {
return rxjs_2.timer(1000).pipe(operators_2.mapTo({ payload: [parameter1, parameter2] }));
};
Service.prototype.getData = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithParamsObj = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataAndReturnCachedStream = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataAsync = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithExpiration = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithSlidingExpiration = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithMaxCacheCount = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithMaxCacheCountAndExpiration = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithMaxCacheCountAndSlidingExpiration = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithCustomCacheResolver = function (parameter, _cacheRerouterParameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithCustomCacheResolverAndHasher = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getWithAComplexType = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithCustomCacheDecider = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.saveDataAndCacheBust = function () {
return this.mockSaveServiceCall();
};
Service.prototype.getDataWithCacheBusting = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithUndefinedParameter = function (parameter) {
if (parameter === void 0) { parameter = ''; }
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithMultipleUndefinedParameters = function (parameter, parameter1) {
if (parameter === void 0) { parameter = 'Parameter1'; }
if (parameter1 === void 0) { parameter1 = 'Parameter2'; }
return this.mockServiceCallWithMultipleParameters(parameter, parameter1);
};
Service.prototype.getData1 = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getData2 = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getData3 = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDateWithCustomStorageStrategyProvided = function (parameter) {
return this.mockServiceCall(parameter);
};
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getData", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getDataWithParamsObj", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getDataAndReturnCachedStream", null);
__decorate([
cacheable_decorator_2.Cacheable({
async: true
})
], Service.prototype, "getDataAsync", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500
})
], Service.prototype, "getDataWithExpiration", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500,
slidingExpiration: true
})
], Service.prototype, "getDataWithSlidingExpiration", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxCacheCount: 5
})
], Service.prototype, "getDataWithMaxCacheCount", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500,
maxCacheCount: 5
})
], Service.prototype, "getDataWithMaxCacheCountAndExpiration", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500,
maxCacheCount: 5,
slidingExpiration: true
})
], Service.prototype, "getDataWithMaxCacheCountAndSlidingExpiration", null);
__decorate([
cacheable_decorator_2.Cacheable({
cacheResolver: function (_oldParameters, newParameters) {
return newParameters.find(function (param) { return !!param.straightToLastCache; });
}
})
], Service.prototype, "getDataWithCustomCacheResolver", null);
__decorate([
cacheable_decorator_2.Cacheable({
cacheHasher: function (_parameters) { return _parameters[0] * 2; },
cacheResolver: function (oldParameter, newParameter) {
return newParameter > 5;
}
})
], Service.prototype, "getDataWithCustomCacheResolverAndHasher", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getWithAComplexType", null);
__decorate([
cacheable_decorator_2.Cacheable({
shouldCacheDecider: function (response) {
return response.payload === 'test';
}
})
], Service.prototype, "getDataWithCustomCacheDecider", null);
__decorate([
cache_buster_decorator_1.CacheBuster({
cacheBusterNotifier: cacheBusterNotifier
})
], Service.prototype, "saveDataAndCacheBust", null);
__decorate([
cacheable_decorator_2.Cacheable({
cacheBusterObserver: cacheBusterNotifier.asObservable()
})
], Service.prototype, "getDataWithCacheBusting", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getDataWithUndefinedParameter", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getDataWithMultipleUndefinedParameters", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getData1", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getData2", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getData3", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500,
slidingExpiration: true,
storageStrategy: InMemoryStorageStrategy_1.InMemoryStorageStrategy
})
], Service.prototype, "getDateWithCustomStorageStrategyProvided", null);
return Service;
}());
describe('CacheableDecorator', function () {

@@ -254,2 +70,165 @@ var service = null;

beforeEach(function () {
var cacheBusterNotifier = new rxjs_2.Subject();
var Service = /** @class */ (function () {
function Service() {
}
Service.prototype.mockServiceCall = function (parameter) {
return rxjs_2.timer(1000).pipe(operators_2.mapTo({ payload: parameter }));
};
Service.prototype.mockSaveServiceCall = function () {
return rxjs_2.timer(1000).pipe(operators_2.mapTo('SAVED'));
};
Service.prototype.mockServiceCallWithMultipleParameters = function (parameter1, parameter2) {
return rxjs_2.timer(1000).pipe(operators_2.mapTo({ payload: [parameter1, parameter2] }));
};
Service.prototype.getData = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithParamsObj = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataAndReturnCachedStream = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataAsync = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithExpiration = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithSlidingExpiration = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithMaxCacheCount = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithMaxCacheCountAndExpiration = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithMaxCacheCountAndSlidingExpiration = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithCustomCacheResolver = function (parameter, _cacheRerouterParameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithCustomCacheResolverAndHasher = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getWithAComplexType = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithCustomCacheDecider = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.saveDataAndCacheBust = function () {
return this.mockSaveServiceCall();
};
Service.prototype.getDataWithCacheBusting = function (parameter) {
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithUndefinedParameter = function (parameter) {
if (parameter === void 0) { parameter = ''; }
return this.mockServiceCall(parameter);
};
Service.prototype.getDataWithMultipleUndefinedParameters = function (parameter, parameter1) {
if (parameter === void 0) { parameter = 'Parameter1'; }
if (parameter1 === void 0) { parameter1 = 'Parameter2'; }
return this.mockServiceCallWithMultipleParameters(parameter, parameter1);
};
Service.prototype.getDateWithCustomStorageStrategyProvided = function (parameter) {
return this.mockServiceCall(parameter);
};
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getData", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getDataWithParamsObj", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getDataAndReturnCachedStream", null);
__decorate([
cacheable_decorator_2.Cacheable({
async: true
})
], Service.prototype, "getDataAsync", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500
})
], Service.prototype, "getDataWithExpiration", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500,
slidingExpiration: true
})
], Service.prototype, "getDataWithSlidingExpiration", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxCacheCount: 5
})
], Service.prototype, "getDataWithMaxCacheCount", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500,
maxCacheCount: 5
})
], Service.prototype, "getDataWithMaxCacheCountAndExpiration", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500,
maxCacheCount: 5,
slidingExpiration: true
})
], Service.prototype, "getDataWithMaxCacheCountAndSlidingExpiration", null);
__decorate([
cacheable_decorator_2.Cacheable({
cacheResolver: function (_oldParameters, newParameters) {
return newParameters.find(function (param) { return !!param.straightToLastCache; });
}
})
], Service.prototype, "getDataWithCustomCacheResolver", null);
__decorate([
cacheable_decorator_2.Cacheable({
cacheHasher: function (_parameters) { return _parameters[0] * 2; },
cacheResolver: function (oldParameter, newParameter) {
return newParameter > 5;
}
})
], Service.prototype, "getDataWithCustomCacheResolverAndHasher", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getWithAComplexType", null);
__decorate([
cacheable_decorator_2.Cacheable({
shouldCacheDecider: function (response) {
return response.payload === 'test';
}
})
], Service.prototype, "getDataWithCustomCacheDecider", null);
__decorate([
cache_buster_decorator_1.CacheBuster({
cacheBusterNotifier: cacheBusterNotifier
})
], Service.prototype, "saveDataAndCacheBust", null);
__decorate([
cacheable_decorator_2.Cacheable({
cacheBusterObserver: cacheBusterNotifier.asObservable()
})
], Service.prototype, "getDataWithCacheBusting", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getDataWithUndefinedParameter", null);
__decorate([
cacheable_decorator_2.Cacheable()
], Service.prototype, "getDataWithMultipleUndefinedParameters", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500,
slidingExpiration: true,
storageStrategy: InMemoryStorageStrategy_1.InMemoryStorageStrategy
})
], Service.prototype, "getDateWithCustomStorageStrategyProvided", null);
return Service;
}());
jasmine.clock().install();

@@ -667,3 +646,3 @@ service = new Service();

it('should call a function with a complex instance which should not be touched and passed to the original method as it is', function () {
var complexObject = new Cat();
var complexObject = new cat_1.Cat();
complexObject.name = 'Felix';

@@ -735,22 +714,22 @@ var response = _timedStreamAsyncAwait(service.getWithAComplexType(complexObject), 1000);

*/
service.getData1('test1');
var asyncFreshData1 = _timedStreamAsyncAwait(service.getData1('test1'), 1000);
service.getData('test1');
var asyncFreshData1 = _timedStreamAsyncAwait(service.getData('test1'), 1000);
expect(asyncFreshData1).toEqual({ payload: 'test1' });
var cachedResponse1 = _timedStreamAsyncAwait(service.getData1('test1'));
var cachedResponse1 = _timedStreamAsyncAwait(service.getData('test1'));
expect(cachedResponse1).toEqual({ payload: 'test1' });
/**
* even though we called getData1 twice, this should only be called once
* even though we called getData twice, this should only be called once
* since the second call went straight to the cache
*/
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
service.getData2('test2');
var asyncFreshData2 = _timedStreamAsyncAwait(service.getData2('test2'), 1000);
service.getData('test2');
var asyncFreshData2 = _timedStreamAsyncAwait(service.getData('test2'), 1000);
expect(asyncFreshData2).toEqual({ payload: 'test2' });
var cachedResponse2 = _timedStreamAsyncAwait(service.getData2('test2'));
var cachedResponse2 = _timedStreamAsyncAwait(service.getData('test2'));
expect(cachedResponse2).toEqual({ payload: 'test2' });
expect(mockServiceCallSpy).toHaveBeenCalledTimes(2);
service.getData3('test3');
var asyncFreshData3 = _timedStreamAsyncAwait(service.getData3('test3'), 1000);
service.getData('test3');
var asyncFreshData3 = _timedStreamAsyncAwait(service.getData('test3'), 1000);
expect(asyncFreshData3).toEqual({ payload: 'test3' });
var cachedResponse3 = _timedStreamAsyncAwait(service.getData3('test3'));
var cachedResponse3 = _timedStreamAsyncAwait(service.getData('test3'));
expect(cachedResponse3).toEqual({ payload: 'test3' });

@@ -762,5 +741,5 @@ expect(mockServiceCallSpy).toHaveBeenCalledTimes(3);

cacheable_decorator_1.globalCacheBusterNotifier.next();
_timedStreamAsyncAwait(service.getData1('test1'), 1000);
_timedStreamAsyncAwait(service.getData2('test2'), 1000);
_timedStreamAsyncAwait(service.getData3('test3'), 1000);
_timedStreamAsyncAwait(service.getData('test1'), 1000);
_timedStreamAsyncAwait(service.getData('test2'), 1000);
_timedStreamAsyncAwait(service.getData('test3'), 1000);
/**

@@ -863,2 +842,144 @@ * if we didn't bust the cache, this would've been 3

});
it('use the maxAge and slidingExpiration from the GlobalCacheConfig', function () {
common_1.GlobalCacheConfig.maxAge = 7500;
common_1.GlobalCacheConfig.slidingExpiration = true;
jasmine.clock().mockDate();
var asyncFreshData = _timedStreamAsyncAwait(service.getData('test'), 1000);
expect(asyncFreshData).toEqual({ payload: 'test' });
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
var cachedResponse = _timedStreamAsyncAwait(service.getData('test'));
expect(cachedResponse).toEqual({ payload: 'test' });
/**
* call count should still be one, since we rerouted to cache, instead of service call
*/
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
/**
* travel through 3000ms of time
*/
jasmine.clock().tick(3000);
/**
* calling the method again should renew expiration for 7500 more milliseconds
*/
service.getData('test').subscribe();
jasmine.clock().tick(4501);
/**
* this should have returned null, if the cache didnt renew
*/
var cachedResponse2 = _timedStreamAsyncAwait(service.getData('test'));
expect(cachedResponse2).toEqual({ payload: 'test' });
/**
* call count is still one, because we renewed the cache 4501ms ago
*/
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
/**
* expire cache, shouldn't renew since 7501 ms have ellapsed
*/
jasmine.clock().tick(7501);
var cachedResponse3 = _timedStreamAsyncAwait(service.getData('test'));
/**
* cached has expired, request hasn't returned yet but still - the service was called
*/
expect(cachedResponse3).toEqual(null);
expect(mockServiceCallSpy).toHaveBeenCalledTimes(2);
common_1.GlobalCacheConfig.maxAge = undefined;
common_1.GlobalCacheConfig.slidingExpiration;
});
it('use the maxCacheCount from the GlobalCacheConfig', function () {
common_1.GlobalCacheConfig.maxCacheCount = 5;
/**
* call the same endpoint with 5 different parameters and cache all 5 responses, based on the maxCacheCount parameter
*/
var parameters = ['test1', 'test2', 'test3', 'test4', 'test5'];
parameters.forEach(function (param) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/, _timedStreamAsyncAwait(service.getData(param), 1000)];
}); }); });
/**
* data for all endpoints should be available through cache by now
*/
expect(mockServiceCallSpy).toHaveBeenCalledTimes(5);
var cachedResponse = _timedStreamAsyncAwait(service.getData('test1'));
expect(cachedResponse).toEqual({ payload: 'test1' });
/** call count still 5 */
expect(mockServiceCallSpy).toHaveBeenCalledTimes(5);
/**
* this should return a maximum of 5 different cached responses
*/
var cachedResponseAll = _timedStreamAsyncAwait(rxjs_1.forkJoin(parameters.map(function (param) { return service.getData(param); })));
expect(cachedResponseAll).toEqual([
{ payload: 'test1' },
{ payload: 'test2' },
{ payload: 'test3' },
{ payload: 'test4' },
{ payload: 'test5' }
]);
/** call count still 5 */
expect(mockServiceCallSpy).toHaveBeenCalledTimes(5);
var asyncData = _timedStreamAsyncAwait(service.getData('test6'), 1000);
expect(asyncData).toEqual({ payload: 'test6' });
/** call count incremented by one */
expect(mockServiceCallSpy).toHaveBeenCalledTimes(6);
/**
* by now the response for test6 should be cached and the one for test1 should be free for GC..
*/
var newParameters = ['test2', 'test3', 'test4', 'test5', 'test6'];
/**
* this should return a maximum of 5 different cached responses, with the latest one in the end
*/
var cachedResponseAll2 = _timedStreamAsyncAwait(rxjs_1.forkJoin(newParameters.map(function (param) { return service.getData(param); })), 1000);
expect(cachedResponseAll2).toEqual([
{ payload: 'test2' },
{ payload: 'test3' },
{ payload: 'test4' },
{ payload: 'test5' },
{ payload: 'test6' }
]);
/** no service calls will be made, since we have all the responses still cached even after 1s (1000ms) */
expect(mockServiceCallSpy).toHaveBeenCalledTimes(6);
/**
* fetch and cache the test7 response
*/
var nonCachedResponse = _timedStreamAsyncAwait(service.getData('test7'), 1000);
expect(nonCachedResponse).toEqual({ payload: 'test7' });
expect(mockServiceCallSpy).toHaveBeenCalledTimes(7);
/**
* since the cached response for 'test2' was now removed from cache by 'test7', it shouldn't be available in cache
*/
var cachedResponse2 = _timedStreamAsyncAwait(service.getData('test2'));
expect(cachedResponse2).toEqual(null);
/**
* service call is made anyway
*/
expect(mockServiceCallSpy).toHaveBeenCalledTimes(8);
common_1.GlobalCacheConfig.maxCacheCount = undefined;
});
it('use the maxAge from the GlobalCacheConfig', function () {
common_1.GlobalCacheConfig.maxAge = 10000;
jasmine.clock().mockDate();
var asyncFreshData = _timedStreamAsyncAwait(service.getData('test'), 1000);
expect(asyncFreshData).toEqual({ payload: 'test' });
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
var cachedResponse = _timedStreamAsyncAwait(service.getData('test'));
/**
* service shouldn't be called and we should route directly to cache
*/
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
expect(cachedResponse).toEqual({ payload: 'test' });
/**
* progress in time for 10001 ms, e.g - one millisecond after the maxAge would expire
*/
jasmine.clock().tick(10001);
/**
* no cache anymore, bail out to service call
*/
var cachedResponse2 = _timedStreamAsyncAwait(service.getData('test'));
expect(cachedResponse2).toEqual(null);
expect(mockServiceCallSpy).toHaveBeenCalledTimes(2);
var asyncFreshDataAfterCacheBust = null;
service.getData('test').subscribe(function (data) {
asyncFreshDataAfterCacheBust = data;
});
jasmine.clock().tick(1000);
expect(asyncFreshDataAfterCacheBust).toEqual({ payload: 'test' });
common_1.GlobalCacheConfig.maxAge = undefined;
});
});

@@ -865,0 +986,0 @@ function _timedStreamAsyncAwait(stream$, skipTime) {

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

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc