@travetto/cache
Advanced tools
Comparing version 0.0.14 to 0.0.15
@@ -24,3 +24,3 @@ { | ||
}, | ||
"version": "0.0.14" | ||
"version": "0.0.15" | ||
} |
import { CacheManager } from '../service'; | ||
import * as LRU from 'lru-cache'; | ||
import { Class } from '@travetto/registry'; | ||
export function Cacheable<U>(config: string | LRU.Options<string, U> & { name?: string }, keyFn?: (...args: any[]) => string) { | ||
return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => U>) => { | ||
type TypedMethodDecorator<U> = (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => U>) => void; | ||
export function Cacheable<U>(config: LRU.Options<string, U> & { name?: string, dispose: (k: string, v: U) => any }, keyFn?: (...args: any[]) => string): TypedMethodDecorator<U>; | ||
export function Cacheable(config: string | LRU.Options<string, any> & { name?: string }, keyFn?: (...args: any[]) => string): TypedMethodDecorator<any>; | ||
export function Cacheable(config: string | LRU.Options<string, any> & { name?: string }, keyFn?: (...args: any[]) => string): TypedMethodDecorator<any> { | ||
return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => any>) => { | ||
const targetName = `${target.name}-${propertyKey}`; | ||
@@ -13,6 +18,6 @@ if (typeof config !== 'string') { | ||
const orig = descriptor.value! as any as (...args: any[]) => U; | ||
const orig = descriptor.value! as any as (...args: any[]) => any; | ||
const cache = CacheManager.get<any>(config as any); | ||
descriptor.value = function (this: any, ...args: any[]): U { | ||
descriptor.value = function (this: any, ...args: any[]): any { | ||
let key = keyFn ? keyFn(args) : JSON.stringify(args || []); | ||
@@ -19,0 +24,0 @@ key = `${targetName}||${key}`; |
import * as LRU from 'lru-cache'; | ||
export class CacheManager { | ||
public static caches = new Map<string, LRU.Cache<string, any>>(); | ||
protected static defaultConfig = { | ||
@@ -10,2 +8,4 @@ max: 1000 | ||
public static caches = new Map<string, LRU.Cache<string, any>>(); | ||
static get<T>(config: string | LRU.Options<string, T> & { name: string }) { | ||
@@ -12,0 +12,0 @@ if (typeof config === 'string') { |
@@ -8,2 +8,2 @@ export const init = { | ||
} | ||
} | ||
}; |
@@ -8,4 +8,9 @@ import { Cacheable, CacheManager } from '../src'; | ||
constructor() { } | ||
@Cacheable({ | ||
max: 5 | ||
max: 5, | ||
dispose: (k: string, v: Promise<number>) => { | ||
} | ||
}) | ||
@@ -12,0 +17,0 @@ async smallAndComplex(num: number) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6804
13
166