Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public class CachingPriceFeed

  1. Object
  2. CachingPriceFeed

ImplementsPriceFeed

A PriceFeed that remembers each pair’s answer for a while. A wallet screen that refreshes every few seconds then asks the source once per pair per interval, which is what keeps it inside a public API’s rate limit. When the source fails and a remembered answer is no older than the stale limit, that answer is returned rather than the failure.

The cache keeps at most 512 pairs and is not synchronized; on the backend give each thread its own or guard it.

Constructors

public CachingPriceFeed(PriceFeed source, long freshMillis, long staleMillis)A cache answering from memory for freshMillis, and on a source failure for up to staleMillis.

Methods

public Quote getQuote(String base, String quote) throws IOExceptionThe price of base in quote, by ticker (BTC, ETH, USD, EUR); case does not matter.
public void clear()Forgets every remembered answer.
protected long now()The clock, in milliseconds; a test overrides it.

Inherited methods

Constructor details

CachingPriceFeed

public CachingPriceFeed(PriceFeed source, long freshMillis, long staleMillis)
A cache answering from memory for freshMillis, and on a source failure for up to staleMillis.

Method details

getQuote

public Quote getQuote(String base, String quote) throws IOException
The price of base in quote, by ticker (BTC, ETH, USD, EUR); case does not matter.

Throws

IOException
when the source cannot be reached or does not know the pair

clear

public void clear()
Forgets every remembered answer.

now

protected long now()
The clock, in milliseconds; a test overrides it.