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.1.6 to 1.1.7-beta.1

common/DOMStorageStrategy.ts

2

cache-buster.decorator.ts

@@ -15,3 +15,3 @@ import { ICacheBusterConfig } from './common/ICacheBusterConfig';

/* use function instead of an arrow function to keep context of invocation */
(propertyDescriptor.value as any) = function (...parameters) {
(propertyDescriptor.value as any) = function (...parameters: Array<any>) {
return (oldMethod.call(this, ...parameters) as Observable<any>).pipe(

@@ -18,0 +18,0 @@ tap(() => {

import { empty, merge, Observable, of, Subject } from 'rxjs';
import { delay, finalize, shareReplay, tap } from 'rxjs/operators';
import { DEFAULT_CACHE_RESOLVER, ICacheable } from './common';
import { DEFAULT_CACHE_RESOLVER, ICacheable, GlobalCacheConfig } from './common';
import { IObservableCacheConfig } from './common/IObservableCacheConfig';

@@ -14,5 +14,8 @@ import { ICachePair } from './common/ICachePair';

) {
const cacheKey = cacheConfig.cacheKey || _target.constructor.name + '#' + _propertyKey;
const oldMethod = propertyDescriptor.value;
if (propertyDescriptor && propertyDescriptor.value) {
const cachePairs: Array<ICachePair<Observable<any>>> = [];
if (!cacheConfig.storageStrategy) {
cacheConfig.storageStrategy = new GlobalCacheConfig.storageStrategy();
}
const pendingCachePairs: Array<ICachePair<Observable<any>>> = [];

@@ -30,3 +33,3 @@ /**

).subscribe(_ => {
cachePairs.length = 0;
cacheConfig.storageStrategy.removeAll(cacheKey);
pendingCachePairs.length = 0;

@@ -40,7 +43,7 @@ });

/* use function instead of an arrow function to keep context of invocation */
(propertyDescriptor.value as any) = function (..._parameters) {
(propertyDescriptor.value as any) = function (..._parameters:Array<any>) {
const cachePairs: Array<ICachePair<Observable<any>>> = cacheConfig.storageStrategy.getAll(cacheKey);
let parameters = _parameters.map(param => param !== undefined ? JSON.parse(JSON.stringify(param)) : param);
let _foundCachePair = cachePairs.find(cp =>
cacheConfig.cacheResolver(cp.parameters, parameters)
);
cacheConfig.cacheResolver(cp.parameters, parameters));
const _foundPendingCachePair = pendingCachePairs.find(cp =>

@@ -54,3 +57,3 @@ cacheConfig.cacheResolver(cp.parameters, parameters)

if (
new Date().getTime() - _foundCachePair.created.getTime() >
new Date().getTime() - new Date(_foundCachePair.created).getTime() >
cacheConfig.maxAge

@@ -61,3 +64,3 @@ ) {

*/
cachePairs.splice(cachePairs.indexOf(_foundCachePair), 1);
cacheConfig.storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
_foundCachePair = null;

@@ -69,2 +72,3 @@ } else if (cacheConfig.slidingExpiration) {

_foundCachePair.created = new Date();
cacheConfig.storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
}

@@ -110,9 +114,9 @@ }

) {
cachePairs.shift();
cacheConfig.storageStrategy.removeAtIndex(0, cacheKey);
}
cachePairs.push({
cacheConfig.storageStrategy.add({
parameters,
response,
created: cacheConfig.maxAge ? new Date() : null
});
}, cacheKey);
}

@@ -119,0 +123,0 @@ }),

import { Observable } from 'rxjs';
import { ICacheRequestResolver, IShouldCacheDecider } from './index';
import { IStorageStrategy } from './IStorageStrategy';
export interface ICacheConfig {

@@ -33,2 +34,13 @@ /**

cacheBusterObserver?: Observable<any>;
/**
* storage strategy
*/
storageStrategy?: IStorageStrategy;
/**
* property name under which to store the cached pairs for this method
* if not provided a combination of class name + method name will be used
*/
cacheKey?: string;
}
import { ICacheBusterConfig } from './ICacheBusterConfig';
import { ICacheConfig } from './ICacheConfig';
import { ICachePair } from './ICachePair';
import { IStorageStrategy } from './IStorageStrategy';
import { InMemoryStorageStrategy } from './InMemoryStorageStrategy';
export const DEFAULT_CACHE_RESOLVER = (oldParams, newParams) =>
export const DEFAULT_CACHE_RESOLVER = (oldParams: Array<any>, newParams: Array<any>) =>
JSON.stringify(oldParams) === JSON.stringify(newParams);

@@ -15,4 +17,14 @@

export type ICacheable<T> = (...args) => T;
export type ICacheable<T> = (...args: Array<any>) => T;
export { ICacheBusterConfig, ICacheConfig, ICachePair };
export { ICacheBusterConfig, ICacheConfig, ICachePair };
export const GlobalCacheConfig: {
storageStrategy: new () => IStorageStrategy,
globalCacheKey: string
} = {
storageStrategy: InMemoryStorageStrategy,
globalCacheKey: 'CACHE_STORAGE'
}
export { IStorageStrategy };

@@ -10,5 +10,8 @@ "use strict";

return function (_target, _propertyKey, propertyDescriptor) {
var cacheKey = cacheConfig.cacheKey || _target.constructor.name + '#' + _propertyKey;
var oldMethod = propertyDescriptor.value;
if (propertyDescriptor && propertyDescriptor.value) {
var cachePairs_1 = [];
if (!cacheConfig.storageStrategy) {
cacheConfig.storageStrategy = new common_1.GlobalCacheConfig.storageStrategy();
}
var pendingCachePairs_1 = [];

@@ -23,3 +26,3 @@ /**

: rxjs_1.empty()).subscribe(function (_) {
cachePairs_1.length = 0;
cacheConfig.storageStrategy.removeAll(cacheKey);
pendingCachePairs_1.length = 0;

@@ -36,4 +39,5 @@ });

}
var cachePairs = cacheConfig.storageStrategy.getAll(cacheKey);
var parameters = _parameters.map(function (param) { return param !== undefined ? JSON.parse(JSON.stringify(param)) : param; });
var _foundCachePair = cachePairs_1.find(function (cp) {
var _foundCachePair = cachePairs.find(function (cp) {
return cacheConfig.cacheResolver(cp.parameters, parameters);

@@ -48,3 +52,3 @@ });

if (cacheConfig.maxAge && _foundCachePair && _foundCachePair.created) {
if (new Date().getTime() - _foundCachePair.created.getTime() >
if (new Date().getTime() - new Date(_foundCachePair.created).getTime() >
cacheConfig.maxAge) {

@@ -54,3 +58,3 @@ /**

*/
cachePairs_1.splice(cachePairs_1.indexOf(_foundCachePair), 1);
cacheConfig.storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
_foundCachePair = null;

@@ -63,2 +67,3 @@ }

_foundCachePair.created = new Date();
cacheConfig.storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
}

@@ -93,10 +98,10 @@ }

(cacheConfig.maxCacheCount &&
cacheConfig.maxCacheCount < cachePairs_1.length + 1)) {
cachePairs_1.shift();
cacheConfig.maxCacheCount < cachePairs.length + 1)) {
cacheConfig.storageStrategy.removeAtIndex(0, cacheKey);
}
cachePairs_1.push({
cacheConfig.storageStrategy.add({
parameters: parameters,
response: response,
created: cacheConfig.maxAge ? new Date() : null
});
}, cacheKey);
}

@@ -103,0 +108,0 @@ }),

import { Observable } from 'rxjs';
import { ICacheRequestResolver, IShouldCacheDecider } from './index';
import { IStorageStrategy } from './IStorageStrategy';
export interface ICacheConfig {

@@ -32,2 +33,11 @@ /**

cacheBusterObserver?: Observable<any>;
/**
* storage strategy
*/
storageStrategy?: IStorageStrategy;
/**
* property name under which to store the cached pairs for this method
* if not provided a combination of class name + method name will be used
*/
cacheKey?: string;
}
import { ICacheBusterConfig } from './ICacheBusterConfig';
import { ICacheConfig } from './ICacheConfig';
import { ICachePair } from './ICachePair';
export declare const DEFAULT_CACHE_RESOLVER: (oldParams: any, newParams: any) => boolean;
import { IStorageStrategy } from './IStorageStrategy';
export declare const DEFAULT_CACHE_RESOLVER: (oldParams: any[], newParams: any[]) => boolean;
export declare type ICacheRequestResolver = (oldParameters: Array<any>, newParameters: Array<any>) => boolean;
export declare type IShouldCacheDecider = (response: any) => boolean;
export declare type ICacheable<T> = (...args) => T;
export declare type ICacheable<T> = (...args: Array<any>) => T;
export { ICacheBusterConfig, ICacheConfig, ICachePair };
export declare const GlobalCacheConfig: {
storageStrategy: new () => IStorageStrategy;
globalCacheKey: string;
};
export { IStorageStrategy };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var IStorageStrategy_1 = require("./IStorageStrategy");
exports.IStorageStrategy = IStorageStrategy_1.IStorageStrategy;
var InMemoryStorageStrategy_1 = require("./InMemoryStorageStrategy");
exports.DEFAULT_CACHE_RESOLVER = function (oldParams, newParams) {
return JSON.stringify(oldParams) === JSON.stringify(newParams);
};
exports.GlobalCacheConfig = {
storageStrategy: InMemoryStorageStrategy_1.InMemoryStorageStrategy,
globalCacheKey: 'CACHE_STORAGE'
};
//# sourceMappingURL=index.js.map

@@ -5,1 +5,3 @@ export * from './cache-buster.decorator';

export * from './promise.cacheable.decorator';
export * from './common';
export * from './common/InMemoryStorageStrategy';

@@ -10,2 +10,4 @@ "use strict";

__export(require("./promise.cacheable.decorator"));
__export(require("./common"));
__export(require("./common/InMemoryStorageStrategy"));
//# sourceMappingURL=index.js.map

@@ -18,5 +18,8 @@ "use strict";

return function (_target, _propertyKey, propertyDescriptor) {
var cacheKey = cacheConfig.cacheKey || _target.constructor.name + '#' + _propertyKey;
var oldMethod = propertyDescriptor.value;
if (propertyDescriptor && propertyDescriptor.value) {
var cachePairs_1 = [];
if (!cacheConfig.storageStrategy) {
cacheConfig.storageStrategy = new common_1.GlobalCacheConfig.storageStrategy();
}
var pendingCachePairs_1 = [];

@@ -31,3 +34,3 @@ /**

: rxjs_1.empty()).subscribe(function (_) {
cachePairs_1.length = 0;
cacheConfig.storageStrategy.removeAll(cacheKey);
pendingCachePairs_1.length = 0;

@@ -44,4 +47,5 @@ });

}
var cachePairs = cacheConfig.storageStrategy.getAll(cacheKey);
var parameters = _parameters.map(function (param) { return param !== undefined ? JSON.parse(JSON.stringify(param)) : param; });
var _foundCachePair = cachePairs_1.find(function (cp) {
var _foundCachePair = cachePairs.find(function (cp) {
return cacheConfig.cacheResolver(cp.parameters, parameters);

@@ -56,3 +60,3 @@ });

if (cacheConfig.maxAge && _foundCachePair && _foundCachePair.created) {
if (new Date().getTime() - _foundCachePair.created.getTime() >
if (new Date().getTime() - new Date(_foundCachePair.created).getTime() >
cacheConfig.maxAge) {

@@ -62,3 +66,3 @@ /**

*/
cachePairs_1.splice(cachePairs_1.indexOf(_foundCachePair), 1);
cacheConfig.storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
_foundCachePair = null;

@@ -71,2 +75,3 @@ }

_foundCachePair.created = new Date();
cacheConfig.storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
}

@@ -93,10 +98,10 @@ }

(cacheConfig.maxCacheCount &&
cacheConfig.maxCacheCount < cachePairs_1.length + 1)) {
cachePairs_1.shift();
cacheConfig.maxCacheCount < cachePairs.length + 1)) {
cacheConfig.storageStrategy.removeAtIndex(0, cacheKey);
}
cachePairs_1.push({
cacheConfig.storageStrategy.add({
parameters: parameters,
response: response,
created: cacheConfig.maxAge ? new Date() : null
});
}, cacheKey);
}

@@ -103,0 +108,0 @@ removeCachePair(pendingCachePairs_1, parameters, cacheConfig);

@@ -5,2 +5,3 @@ export * from './cache-buster.decorator';

export * from './promise.cacheable.decorator';
export * from './common';
export * from './common/InMemoryStorageStrategy';

@@ -7,9 +7,13 @@ module.exports = function (config) {

files: [
'./index.ts',
'./common/index.ts',
'./common/IStorageStrategy.ts',
'./common/DOMStorageStrategy.ts',
'./common/InMemoryStorageStrategy.ts',
'./cacheable.decorator.ts',
'./cache-buster.decorator.ts',
'./cacheable.decorator.spec.ts',
'./promise.cache-buster.decorator.ts',
'./promise.cacheable.decorator.ts',
'./promise.cacheable.decorator.spec.ts',
'./specs/promise-cacheable.decorator.spec.ts',
'./specs/observable-cacheable.decorator.spec.ts'
],

@@ -16,0 +20,0 @@ preprocessors: {

{
"name": "ngx-cacheable",
"version": "1.1.6",
"version": "1.1.7-beta.1",
"description": "Rx Observable cache decorator",

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

@@ -13,3 +13,3 @@ import { ICacheBusterConfig } from './common/ICacheBusterConfig';

/* use function instead of an arrow function to keep context of invocation */
(propertyDescriptor.value as any) = function (...parameters) {
(propertyDescriptor.value as any) = function (...parameters: Array<any>) {
return (oldMethod.call(this, ...parameters) as Promise<any>).then(

@@ -16,0 +16,0 @@ response => {

import { empty, merge, Subject } from 'rxjs';
import { DEFAULT_CACHE_RESOLVER, ICacheable } from './common';
import { DEFAULT_CACHE_RESOLVER, ICacheable, GlobalCacheConfig } from './common';
import { ICacheConfig } from './common/ICacheConfig';

@@ -27,5 +27,8 @@ import { ICachePair } from './common/ICachePair';

) {
const cacheKey = cacheConfig.cacheKey || _target.constructor.name + '#' + _propertyKey;
const oldMethod = propertyDescriptor.value;
if (propertyDescriptor && propertyDescriptor.value) {
const cachePairs: Array<ICachePair<Promise<any>>> = [];
if (!cacheConfig.storageStrategy) {
cacheConfig.storageStrategy = new GlobalCacheConfig.storageStrategy();
}
const pendingCachePairs: Array<ICachePair<Promise<any>>> = [];

@@ -43,3 +46,3 @@ /**

).subscribe(_ => {
cachePairs.length = 0;
cacheConfig.storageStrategy.removeAll(cacheKey);
pendingCachePairs.length = 0;

@@ -53,3 +56,4 @@ });

/* use function instead of an arrow function to keep context of invocation */
(propertyDescriptor.value as any) = function (..._parameters) {
(propertyDescriptor.value as any) = function (..._parameters: Array<any>) {
const cachePairs: Array<ICachePair<Promise<any>>> = cacheConfig.storageStrategy.getAll(cacheKey);
let parameters = _parameters.map(param => param !== undefined ? JSON.parse(JSON.stringify(param)) : param);

@@ -67,3 +71,3 @@ let _foundCachePair = cachePairs.find(cp =>

if (
new Date().getTime() - _foundCachePair.created.getTime() >
new Date().getTime() - new Date(_foundCachePair.created).getTime() >
cacheConfig.maxAge

@@ -74,3 +78,3 @@ ) {

*/
cachePairs.splice(cachePairs.indexOf(_foundCachePair), 1);
cacheConfig.storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
_foundCachePair = null;

@@ -82,2 +86,3 @@ } else if (cacheConfig.slidingExpiration) {

_foundCachePair.created = new Date();
cacheConfig.storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
}

@@ -108,9 +113,9 @@ }

) {
cachePairs.shift();
cacheConfig.storageStrategy.removeAtIndex(0, cacheKey);
}
cachePairs.push({
cacheConfig.storageStrategy.add({
parameters,
response,
created: cacheConfig.maxAge ? new Date() : null
});
}, cacheKey);
}

@@ -117,0 +122,0 @@ removeCachePair(pendingCachePairs, parameters, cacheConfig);

@@ -9,6 +9,7 @@ {

"experimentalDecorators": true,
"lib": ["es2016"],
"noUnusedLocals": true
"lib": ["es2016", "dom"],
"noUnusedLocals": true,
"noImplicitAny": true
},
"include": ["**/*.ts"]
}

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 not supported yet

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