Optimize a Crypto ExchangeMedium
Imagine you're the lead dev working on a cryptocurrency exchange.
Let's say the market is hot, and millions of users are looking up the current prices of various cryptocurrencies.
However there are millions of cryptocurrencies, and retrieving the prices of each could be time consuming.
The good news is that in this bull run, people are mostly looking up a handful of cryptocurrencies.
Let's say that you're allowed to set up caching, but only at a limited capacity.
LRUCacheclass (Least Recently Used) that can update and store a handful of cryptocurrency tickers and their current price.
The class should contain the following methods:
add(ticker, price)both adds or updates a ticker and its price (for example
BTC). When this happens, this ticker becomes Most recently used.
lookup(ticker)returns the cryptocurrency's price, and marks that ticker as most recently used
As items are added to your LRUCache, be sure to evict the least recently used tickers out of your cache to keep it within the provided