Skip to content

Commit

Permalink
Merge pull request #55 from angelnikolov/feat/remove-serialization
Browse files Browse the repository at this point in the history
Feat/remove serialization
  • Loading branch information
angelnikolov authored Oct 29, 2019
2 parents 98d5245 + 8638681 commit 0d34632
Show file tree
Hide file tree
Showing 21 changed files with 519 additions and 76 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export interface ICacheConfig {
* 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
*/
Expand Down
28 changes: 16 additions & 12 deletions cacheable.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { empty, merge, Observable, of, Subject } from 'rxjs';
import { delay, finalize, tap, publishReplay, refCount } from 'rxjs/operators';
import { DEFAULT_CACHE_RESOLVER, ICacheable, GlobalCacheConfig, IStorageStrategy } from './common';
import { DEFAULT_CACHE_RESOLVER, ICacheable, GlobalCacheConfig, IStorageStrategy, DEFAULT_HASHER } from './common';
import { IObservableCacheConfig } from './common/IObservableCacheConfig';
import { ICachePair } from './common/ICachePair';
export const globalCacheBusterNotifier = new Subject<void>();

export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
return function (
return function(
_target: Object,
_propertyKey: string,
propertyDescriptor: TypedPropertyDescriptor<ICacheable<Observable<any>>>
Expand All @@ -32,19 +32,23 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
storageStrategy.removeAll(cacheKey);
pendingCachePairs.length = 0;
});

cacheConfig.cacheResolver = cacheConfig.cacheResolver
? cacheConfig.cacheResolver
const cacheResolver = cacheConfig.cacheResolver || GlobalCacheConfig.cacheResolver;
cacheConfig.cacheResolver = cacheResolver
? cacheResolver
: DEFAULT_CACHE_RESOLVER;
const cacheHasher = cacheConfig.cacheHasher || GlobalCacheConfig.cacheHasher;
cacheConfig.cacheHasher = cacheHasher
? cacheHasher
: DEFAULT_HASHER;

/* use function instead of an arrow function to keep context of invocation */
(propertyDescriptor.value as any) = function (..._parameters: Array<any>) {
(propertyDescriptor.value as any) = function(...parameters: Array<any>) {
const cachePairs: Array<ICachePair<Observable<any>>> = storageStrategy.getAll(cacheKey);
let parameters = _parameters.map(param => param !== undefined ? JSON.parse(JSON.stringify(param)) : param);
let cacheParameters = cacheConfig.cacheHasher(parameters);
let _foundCachePair = cachePairs.find(cp =>
cacheConfig.cacheResolver(cp.parameters, parameters));
cacheConfig.cacheResolver(cp.parameters, cacheParameters));
const _foundPendingCachePair = pendingCachePairs.find(cp =>
cacheConfig.cacheResolver(cp.parameters, parameters)
cacheConfig.cacheResolver(cp.parameters, cacheParameters)
);
/**
* check if maxAge is passed and cache has actually expired
Expand Down Expand Up @@ -82,7 +86,7 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
* if there has been an observable cache pair for these parameters, when it completes or errors, remove it
*/
const _pendingCachePairToRemove = pendingCachePairs.find(cp =>
cacheConfig.cacheResolver(cp.parameters, parameters)
cacheConfig.cacheResolver(cp.parameters, cacheParameters)
);
pendingCachePairs.splice(
pendingCachePairs.indexOf(_pendingCachePairToRemove),
Expand All @@ -107,7 +111,7 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
storageStrategy.removeAtIndex(0, cacheKey);
}
storageStrategy.add({
parameters,
parameters: cacheParameters,
response,
created: cacheConfig.maxAge ? new Date() : null
}, cacheKey);
Expand All @@ -120,7 +124,7 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
* cache the stream
*/
pendingCachePairs.push({
parameters: parameters,
parameters: cacheParameters,
response: response$,
created: new Date()
});
Expand Down
10 changes: 7 additions & 3 deletions common/ICacheConfig.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Observable } from 'rxjs';
import { ICacheRequestResolver, IShouldCacheDecider } from './index';
import { ICacheResolver, IShouldCacheDecider, ICacheHasher } from './index';
import { IStorageStrategy } from './IStorageStrategy';
import { IAsyncStorageStrategy } from './IAsyncStorageStrategy';
export interface ICacheConfig {
/**
* @description request cache resolver which will get old and new paramaters passed to and based on those
* @description 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;
cacheResolver?: ICacheResolver;
/**
* @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
*/
Expand Down
16 changes: 12 additions & 4 deletions common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@ import { IStorageStrategy } from './IStorageStrategy';
import { InMemoryStorageStrategy } from './InMemoryStorageStrategy';
import { IAsyncStorageStrategy } from './IAsyncStorageStrategy';

export const DEFAULT_CACHE_RESOLVER = (oldParams: Array<any>, newParams: Array<any>) =>
export const DEFAULT_CACHE_RESOLVER = (oldParams: any, newParams: any) =>
JSON.stringify(oldParams) === JSON.stringify(newParams);

export type ICacheRequestResolver = (
oldParameters: Array<any>,
newParameters: Array<any>
export const DEFAULT_HASHER = (parameters: Array<any>) => parameters.map(param => param !== undefined ? JSON.parse(JSON.stringify(param)) : param);

export type ICacheResolver = (
oldParameters: any,
newParameters: any
) => boolean;

export type ICacheHasher = (
parameters: Array<any>
) => any;

export type IShouldCacheDecider = (response: any) => boolean;

export type ICacheable<T> = (...args: Array<any>) => T;

export { ICacheBusterConfig, ICacheConfig, ICachePair };

export const GlobalCacheConfig: {
cacheResolver?: ICacheResolver,
cacheHasher?: ICacheHasher,
storageStrategy: new () => IStorageStrategy | IAsyncStorageStrategy,
globalCacheKey: string,
promiseImplementation: (() => PromiseConstructorLike) | PromiseConstructorLike;
Expand Down
25 changes: 15 additions & 10 deletions dist/cacheable.decorator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cacheable.decorator.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions dist/common/ICacheConfig.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Observable } from 'rxjs';
import { ICacheRequestResolver, IShouldCacheDecider } from './index';
import { ICacheResolver, IShouldCacheDecider, ICacheHasher } from './index';
import { IStorageStrategy } from './IStorageStrategy';
import { IAsyncStorageStrategy } from './IAsyncStorageStrategy';
export interface ICacheConfig {
/**
* @description request cache resolver which will get old and new paramaters passed to and based on those
* @description 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;
cacheResolver?: ICacheResolver;
/**
* @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
*/
Expand Down
8 changes: 6 additions & 2 deletions dist/common/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { ICacheConfig } from './ICacheConfig';
import { ICachePair } from './ICachePair';
import { IStorageStrategy } from './IStorageStrategy';
import { IAsyncStorageStrategy } from './IAsyncStorageStrategy';
export declare const DEFAULT_CACHE_RESOLVER: (oldParams: any[], newParams: any[]) => boolean;
export declare type ICacheRequestResolver = (oldParameters: Array<any>, newParameters: Array<any>) => boolean;
export declare const DEFAULT_CACHE_RESOLVER: (oldParams: any, newParams: any) => boolean;
export declare const DEFAULT_HASHER: (parameters: any[]) => any[];
export declare type ICacheResolver = (oldParameters: any, newParameters: any) => boolean;
export declare type ICacheHasher = (parameters: Array<any>) => any;
export declare type IShouldCacheDecider = (response: any) => boolean;
export declare type ICacheable<T> = (...args: Array<any>) => T;
export { ICacheBusterConfig, ICacheConfig, ICachePair };
export declare const GlobalCacheConfig: {
cacheResolver?: ICacheResolver;
cacheHasher?: ICacheHasher;
storageStrategy: new () => IStorageStrategy | IAsyncStorageStrategy;
globalCacheKey: string;
promiseImplementation: (() => PromiseConstructorLike) | PromiseConstructorLike;
Expand Down
1 change: 1 addition & 0 deletions dist/common/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0d34632

Please sign in to comment.