@travetto/cache
Advanced tools
Comparing version 1.0.0-beta.4 to 1.0.0-beta.5
@@ -31,4 +31,6 @@ { | ||
"index.ts", | ||
"config", | ||
"extension", | ||
"src" | ||
"src", | ||
"support" | ||
], | ||
@@ -39,4 +41,4 @@ "name": "@travetto/cache", | ||
}, | ||
"version": "1.0.0-beta.4", | ||
"gitHead": "d817f2e0f0420f9de1b74bad3714ac96b07f1eaa" | ||
"version": "1.0.0-beta.5", | ||
"gitHead": "1bd9e718edd9682f0cb5c30a3197fcec1c16ae62" | ||
} |
@@ -1,2 +0,2 @@ | ||
import { ValidCacheFields, CacheStore } from './store/types'; | ||
import { ValidCacheFields } from './store/types'; | ||
import { CoreCacheConfig, CacheConfig } from './types'; | ||
@@ -6,58 +6,8 @@ | ||
function generateKey(cache: CacheStore, config: CoreCacheConfig, params: any[]) { | ||
const input = config.params ? config.params(params) : params; | ||
const keyParams = config.key ? config.key(...input) : input; | ||
return cache.computeKey(keyParams); | ||
} | ||
function initConfig(config: CoreCacheConfig, target: any, fn: Function) { | ||
if (!config.keySpace) { | ||
config.keySpace = `${target.constructor.name}.${fn.name}`; | ||
} | ||
} | ||
export function Cache<U extends any>(field: ValidCacheFields<U>, config: CacheConfig = {}): TypedMethodDecorator<U, Promise<any>> { | ||
return function (target: U, propertyKey: string, descriptor: TypedPropertyDescriptor<(...params: any[]) => Promise<any>>) { | ||
const og = descriptor.value!; | ||
initConfig(config, target, og); | ||
descriptor.value = async function (this: U, ...params: any[]) { | ||
const cache = this[field] as any as CacheStore; | ||
const key = generateKey(cache, config, params); | ||
let res = await cache.getOptional(config, key); | ||
if (res === undefined) { | ||
let data = await og.apply(this, params); | ||
if (config.serialize) { | ||
data = config.serialize(data); | ||
} | ||
res = (await cache.setWithAge(config, { key, data })).data; | ||
} | ||
if (config.deserialize) { | ||
res = config.deserialize(res); | ||
} | ||
return res; | ||
}; | ||
Object.defineProperty(descriptor.value, 'name', { value: propertyKey }); | ||
}; | ||
return function (target: U, propertyKey: string, descriptor: TypedPropertyDescriptor<(...params: any[]) => Promise<any>>) { } | ||
} | ||
export function EvictCache<U extends any>(field: ValidCacheFields<U>, config: CoreCacheConfig = {}): TypedMethodDecorator<U, Promise<any>> { | ||
return function (target: U, propertyKey: string, descriptor: TypedPropertyDescriptor<(...params: any[]) => Promise<any>>) { | ||
const og = descriptor.value!; | ||
initConfig(config, target, og); | ||
descriptor.value = async function (this: U, ...params: any[]) { | ||
const cache = this[field] as any as CacheStore; | ||
const key = generateKey(cache, config, params); | ||
const val = await og.apply(this, params); | ||
await cache.delete(key); | ||
return val; | ||
}; | ||
Object.defineProperty(descriptor.value, 'name', { value: propertyKey }); | ||
}; | ||
return function (target: U, propertyKey: string, descriptor: TypedPropertyDescriptor<(...params: any[]) => Promise<any>>) { }; | ||
} |
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
26190
15
482