@0x:contracts-integrations Improved the caches

This commit is contained in:
Alex Towle 2019-10-09 16:23:24 -07:00
parent d66101cd9d
commit 92b80fc436

View File

@ -1,12 +1,6 @@
import { ContractGetterFunction } from './function_assertions'; import { ContractGetterFunction } from './function_assertions';
import * as _ from 'lodash'; import * as _ from 'lodash';
export interface Cache {
getter: ContractGetterFunction;
callAsync: (...args: any[]) => Promise<any>;
flush: () => void;
}
export class GetterCache { export class GetterCache {
// The getter function whose values will be cached. // The getter function whose values will be cached.
public getter: ContractGetterFunction; public getter: ContractGetterFunction;
@ -32,13 +26,14 @@ export class GetterCache {
* @return Either a cached value or the queried value. * @return Either a cached value or the queried value.
*/ */
public async callAsync(...args: any[]): Promise<any> { public async callAsync(...args: any[]): Promise<any> {
const cachedResult = this.cache[this.getter.getABIEncodedTransactionData(...args)]; const key = this.getter.getABIEncodedTransactionData(...args);
const cachedResult = this.cache[key];
if (cachedResult !== undefined) { if (cachedResult !== undefined) {
return cachedResult; return cachedResult;
} else { } else {
const result = await this.getter.callAsync(...args); const result = await this.getter.callAsync(...args);
this.cache[this.getter.getABIEncodedTransactionData(...args)] = result; this.cache[key] = result;
return result; return result;
} }
} }
@ -83,4 +78,13 @@ export class GetterCacheCollection {
public registerGetter(getterName: string, getter: ContractGetterFunction): void { public registerGetter(getterName: string, getter: ContractGetterFunction): void {
this.getters[getterName] = new GetterCache(getter); this.getters[getterName] = new GetterCache(getter);
} }
/**
* Flushes all of the registered caches.
*/
public flushAll(): void {
for (const getter of Object.keys(this.getters)) {
this.getters[getter].flush();
}
}
} }