Skip to content

Commit

Permalink
Use Fire&Forget caching (Uniswap#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeki authored Aug 28, 2023
1 parent f781cd8 commit ff2ee09
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/providers/v2/caching-pool-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class CachingV2PoolProvider implements IV2PoolProvider {
// we compute quotes off-chain.
// If no block is specified in the call to getPools we just return whatever is in the cache.
private cache: ICache<{ pair: Pair; block?: number }>
) {}
) {
}

public async getPools(
tokenPairs: [Token, Token][],
Expand Down Expand Up @@ -101,7 +102,8 @@ export class CachingV2PoolProvider implements IV2PoolProvider {
const pool = poolAccessor.getPoolByAddress(address);
if (pool) {
poolAddressToPool[address] = pool;
await this.cache.set(this.POOL_KEY(this.chainId, address), {
// We don't want to wait for this caching to complete before returning the pools.
this.cache.set(this.POOL_KEY(this.chainId, address), {
pair: pool,
block: blockNumber,
});
Expand Down
10 changes: 6 additions & 4 deletions src/providers/v3/caching-pool-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export class CachingV3PoolProvider implements IV3PoolProvider {
protected chainId: ChainId,
protected poolProvider: IV3PoolProvider,
private cache: ICache<Pool>
) {}
) {
}

public async getPools(
tokenPairs: [Token, Token, FeeAmount][],
Expand Down Expand Up @@ -60,12 +61,12 @@ export class CachingV3PoolProvider implements IV3PoolProvider {
this.POOL_KEY(this.chainId, poolAddress)
);
if (cachedPool) {
metric.putMetric('V3_INMEMORY_CACHING_POOL_HIT_IN_MEMORY', 1, MetricLoggerUnit.None)
metric.putMetric('V3_INMEMORY_CACHING_POOL_HIT_IN_MEMORY', 1, MetricLoggerUnit.None);
poolAddressToPool[poolAddress] = cachedPool;
continue;
}

metric.putMetric('V3_INMEMORY_CACHING_POOL_MISS_NOT_IN_MEMORY', 1, MetricLoggerUnit.None)
metric.putMetric('V3_INMEMORY_CACHING_POOL_MISS_NOT_IN_MEMORY', 1, MetricLoggerUnit.None);
poolsToGetTokenPairs.push([token0, token1, feeAmount]);
poolsToGetAddresses.push(poolAddress);
}
Expand Down Expand Up @@ -97,7 +98,8 @@ export class CachingV3PoolProvider implements IV3PoolProvider {
const pool = poolAccessor.getPoolByAddress(address);
if (pool) {
poolAddressToPool[address] = pool;
await this.cache.set(this.POOL_KEY(this.chainId, address), pool);
// We don't want to wait for this caching to complete before returning the pools.
this.cache.set(this.POOL_KEY(this.chainId, address), pool);
}
}
}
Expand Down

0 comments on commit ff2ee09

Please sign in to comment.