From ddaffc5f3b3e7559926f2c162b9cd2ed18cf0a2f Mon Sep 17 00:00:00 2001 From: Lawrence Wang Date: Mon, 19 Aug 2019 17:57:58 -0700 Subject: [PATCH] add instance cache to CdpManager --- packages/dai-plugin-mcd/src/CdpManager.js | 38 ++++++++++++++++--- .../dai-plugin-mcd/test/CdpManager.spec.js | 8 +++- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/packages/dai-plugin-mcd/src/CdpManager.js b/packages/dai-plugin-mcd/src/CdpManager.js index 6599b4e57..5f3aa8c4a 100644 --- a/packages/dai-plugin-mcd/src/CdpManager.js +++ b/packages/dai-plugin-mcd/src/CdpManager.js @@ -45,8 +45,14 @@ export default class CdpManager extends LocalService { } async getCdp(id, options) { + const cacheEnabled = !has(options, 'cache') || options.cache; + let cdp = this._getFromInstanceCache(id, cacheEnabled); + if (cdp) return cdp; + const ilk = bytesToString(await this._manager.ilks(id)); - const cdp = new ManagedCdp(id, ilk, this, options); + cdp = new ManagedCdp(id, ilk, this, options); + + this._putInInstanceCache(id, cdp, cacheEnabled); if (!has(options, 'prefetch') || options.prefetch) await cdp.prefetch(); return cdp; } @@ -78,25 +84,34 @@ export default class CdpManager extends LocalService { } @tracksTransactions - async open(ilk, { promise }) { + async open(ilk, { promise, cache = true }) { await this.get('proxy').ensureProxy({ promise }); const op = this.proxyActions.open( this._managerAddress, stringToBytes(ilk), { dsProxy: true, promise } ); - return ManagedCdp.create(await op, ilk, this); + const cdp = await ManagedCdp.create(await op, ilk, this); + this._putInInstanceCache(cdp.id, cdp, cache); + return cdp; } // ilk is required if the currency type corresponds to more than one ilk; if // it's omitted, it is inferred from lockAmount's currency type @tracksTransactions - async openLockAndDraw(ilk, lockAmount, drawAmount, { promise }) { + async openLockAndDraw( + ilk, + lockAmount, + drawAmount, + { promise, cache = true } + ) { const type = this.get(CDP_TYPE).getCdpType(lockAmount.type, ilk); const op = this.lockAndDraw(null, type.ilk, lockAmount, drawAmount, { promise }); - return ManagedCdp.create(await op, type.ilk, this); + const cdp = await ManagedCdp.create(await op, type.ilk, this); + this._putInInstanceCache(cdp.id, cdp, cache); + return cdp; } @tracksTransactionsWithOptions({ numArguments: 5 }) @@ -267,6 +282,19 @@ export default class CdpManager extends LocalService { ? 'wei' : this.get(CDP_TYPE).getCdpType(amount.type).decimals; } + + _getFromInstanceCache(id, enabled) { + if (!enabled) return; + if (!this._instanceCache) this._instanceCache = {}; + const instance = this._instanceCache[id]; + if (instance) return instance; + } + + _putInInstanceCache(id, instance, enabled) { + if (!enabled) return; + if (!this._instanceCache) this._instanceCache = {}; + this._instanceCache[id] = instance; + } } export function setMethod(isEth, id) { diff --git a/packages/dai-plugin-mcd/test/CdpManager.spec.js b/packages/dai-plugin-mcd/test/CdpManager.spec.js index 9919f937b..b70a57289 100644 --- a/packages/dai-plugin-mcd/test/CdpManager.spec.js +++ b/packages/dai-plugin-mcd/test/CdpManager.spec.js @@ -58,15 +58,19 @@ test('getCombinedDebtValue', async () => { expect(totalDebt).toEqual(MDAI(8)); }); -test('getCdp looks up ilk', async () => { +test('getCdp looks up ilk and has cache', async () => { const cdp = await cdpMgr.open('ETH-A'); const sameCdp = await cdpMgr.getCdp(cdp.id); expect(sameCdp.ilk).toEqual(cdp.ilk); + expect(cdp).toBe(sameCdp); + + const differentInstance = await cdpMgr.getCdp(cdp.id, { cache: false }); + expect(differentInstance).not.toBe(cdp); }); test('getCdp can disable prefetch', async () => { const cdp = await cdpMgr.open('ETH-A'); - const sameCdp = await cdpMgr.getCdp(cdp.id, { prefetch: false }); + const sameCdp = await cdpMgr.getCdp(cdp.id, { prefetch: false, cache: false }); expect(sameCdp._urnInfoPromise).toBeUndefined(); });