Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase caching time #71

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apps/api/src/app/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
usdRepositorySymbol,
} from '@cowprotocol/repositories';

const DEFAULT_CACHE_VALUE_SECONDS = ms('2min') / 1000; // 2min cache time by default for values
const DEFAULT_CACHE_NULL_SECONDS = ms('30min') / 1000; // 30min cache time by default for NULL values (when the repository don't know)

import { Container } from 'inversify';
import {
SlippageService,
Expand All @@ -21,6 +24,7 @@ import {
slippageServiceSymbol,
usdServiceSymbol,
} from '@cowprotocol/services';
import ms from 'ms';

function getTokenDecimals(tokenAddress: string): number | null {
return 18; // TODO: Implement!!!
Expand All @@ -38,7 +42,9 @@ function getUsdRepositoryCow(cacheRepository: CacheRepository): UsdRepository {
return new UsdRepositoryCache(
new UsdRepositoryCow(getTokenDecimals),
cacheRepository,
'usdCow'
'usdCow',
DEFAULT_CACHE_VALUE_SECONDS,
DEFAULT_CACHE_NULL_SECONDS
);
}

Expand All @@ -48,7 +54,9 @@ function getUsdRepositoryCoingecko(
return new UsdRepositoryCache(
new UsdRepositoryCoingecko(),
cacheRepository,
'usdCoingecko'
'usdCoingecko',
DEFAULT_CACHE_VALUE_SECONDS,
DEFAULT_CACHE_NULL_SECONDS
);
}

Expand Down
7 changes: 2 additions & 5 deletions libs/repositories/src/UsdRepository/UsdRepositoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ import {
usdRepositorySymbol,
} from './UsdRepository';
import { SupportedChainId } from '../types';
import IORedis from 'ioredis';
import ms from 'ms';
import { CacheRepository } from '../CacheRepository/CacheRepository';

const DEFAULT_CACHE_VALUE_SECONDS = ms('10s') / 1000; // 2min cache time by default for values
const DEFAULT_CACHE_NULL_SECONDS = ms('30min') / 1000; // 2min cache time by default for NULL values (when the repository don't know)
const NULL_VALUE = 'null';

@injectable()
Expand All @@ -24,8 +21,8 @@ export class UsdRepositoryCache implements UsdRepository {
private proxy: UsdRepository,
@inject(usdRepositorySymbol) private cache: CacheRepository,
private cacheName: string,
private cacheTimeValueSeconds: number = DEFAULT_CACHE_VALUE_SECONDS,
private cacheTimeNullSeconds: number = DEFAULT_CACHE_NULL_SECONDS
private cacheTimeValueSeconds: number,
private cacheTimeNullSeconds: number
) {
this.baseCacheKey = `repos:${this.cacheName}`;
}
Expand Down
Loading