POC for a rough derive cache (#2293)

* POC for a rough derive cache

* With expiry

* Cache cleanups

* Memory cache

* Small cleanups
This commit is contained in:
Jaco Greeff
2020-05-19 10:29:40 +02:00
committed by GitHub
parent eb06bb6e93
commit 67a3376ef8
14 changed files with 316 additions and 88 deletions
+35
View File
@@ -0,0 +1,35 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { DeriveCache } from './types';
const mapCache: Map<string, any> = new Map();
const deriveMapCache: DeriveCache = {
del: (key: string): void => {
mapCache.delete(key);
},
forEach: (cb: (key: string, value: any) => void): void => {
const entries = mapCache.entries();
for (const entry in entries) {
cb(entry[0], entry[1]);
}
},
get: <T = any> (key: string): T | undefined => {
return mapCache.get(key);
},
set: (key: string, value: any): void => {
mapCache.set(key, value);
}
};
const deriveNoopCache: DeriveCache = {
del: (): void => undefined,
forEach: () => undefined,
get: (): undefined => undefined,
set: (_: string, value: any): any => value
};
export { deriveMapCache, deriveNoopCache };