Class CachingPriceFeed

java.lang.Object
com.codename1.crypto.market.CachingPriceFeed
All Implemented Interfaces:
PriceFeed

public class CachingPriceFeed extends Object implements PriceFeed

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.

  • Constructor Summary

    Constructors
    Constructor
    Description
    CachingPriceFeed(PriceFeed source, long freshMillis, long staleMillis)
    A cache answering from memory for freshMillis, and on a source failure for up to staleMillis.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Forgets every remembered answer.
    getQuote(String base, String quote)
    The price of base in quote, by ticker (BTC, ETH, USD, EUR); case does not matter.
    protected long
    now()
    The clock, in milliseconds; a test overrides it.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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
      Description copied from interface: PriceFeed

      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
      Specified by:
      getQuote in interface PriceFeed
      Throws:
      IOException
    • clear

      public void clear()
      Forgets every remembered answer.
    • now

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