Skip to content

Commit

Permalink
Remove ttl-cache dependency (#3207)
Browse files Browse the repository at this point in the history
# Description
We currently have 2 dependencies for size and time based caching
strategies.
Given that we only have 1 usage of `ttl-cache` I replaced it with
`cached` to reduce the total number of dependencies.

One minor downside is that this now requires to take one more write lock
but since there shouldn't be significant contention on the lock to begin
with that should be okay.
  • Loading branch information
MartinquaXD authored Jan 6, 2025
1 parent c971b67 commit 0db8f43
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 25 deletions.
16 changes: 0 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ contracts = { path = "../contracts" }
dashmap = { workspace = true }
database = { path = "../database" }
derive_more = { workspace = true }
ttl_cache = "0.5"
derivative = { workspace = true }
ethcontract = { workspace = true }
ethrpc = { path = "../ethrpc" }
Expand Down
14 changes: 6 additions & 8 deletions crates/shared/src/sources/uniswap_v2/pool_fetching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use {
super::pair_provider::PairProvider,
crate::{baseline_solver::BaselineSolvable, ethrpc::Web3, recent_block_cache::Block},
anyhow::Result,
cached::{Cached, TimedCache},
contracts::{errors::EthcontractErrorType, IUniswapLikePair, ERC20},
ethcontract::{errors::MethodError, BlockId, H160, U256},
futures::{
Expand All @@ -15,7 +16,6 @@ use {
sync::{LazyLock, RwLock},
time::Duration,
},
ttl_cache::TtlCache,
};

const POOL_SWAP_GAS_COST: usize = 60_000;
Expand Down Expand Up @@ -184,17 +184,15 @@ impl BaselineSolvable for Pool {
pub struct PoolFetcher<Reader> {
pub pool_reader: Reader,
pub web3: Web3,
pub cache_time: Duration,
pub non_existent_pools: RwLock<TtlCache<TokenPair, ()>>,
pub non_existent_pools: RwLock<TimedCache<TokenPair, ()>>,
}

impl<Reader> PoolFetcher<Reader> {
pub fn new(reader: Reader, web3: Web3, cache_time: Duration) -> Self {
Self {
pool_reader: reader,
web3,
cache_time,
non_existent_pools: RwLock::new(TtlCache::new(usize::MAX)),
non_existent_pools: RwLock::new(TimedCache::with_lifespan(cache_time.as_secs())),
}
}
}
Expand All @@ -207,8 +205,8 @@ where
async fn fetch(&self, token_pairs: HashSet<TokenPair>, at_block: Block) -> Result<Vec<Pool>> {
let mut token_pairs: Vec<_> = token_pairs.into_iter().collect();
{
let non_existent_pools = self.non_existent_pools.read().unwrap();
token_pairs.retain(|pair| !non_existent_pools.contains_key(pair));
let mut non_existent_pools = self.non_existent_pools.write().unwrap();
token_pairs.retain(|pair| non_existent_pools.cache_get(pair).is_none());
}
let block = BlockId::Number(at_block.into());
let futures = token_pairs
Expand All @@ -230,7 +228,7 @@ where
tracing::debug!(token_pairs = ?new_missing_pairs, "stop indexing liquidity");
let mut non_existent_pools = self.non_existent_pools.write().unwrap();
for pair in new_missing_pairs {
non_existent_pools.insert(pair, (), self.cache_time);
non_existent_pools.cache_set(pair, ());
}
}
Ok(pools)
Expand Down

0 comments on commit 0db8f43

Please sign in to comment.