daily pastebin goal
28%
SHARE
TWEET

async cache expiration check not working

a guest Dec 19th, 2018 49 in 91 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2.  
  3. CACHE_EXP_TIME = 10 # should be seconds
  4.  
  5. class Cache(object):
  6.     price_cache = {}
  7.    
  8.     # exchange: exchange object from ccxt module
  9.     # pair: str('btc/usd')
  10.     @classmethod
  11.     async def get_cached_price(ctx, exchange, pair):
  12.         key_name = exchange.name + pair  # ugly, but works - ex: 'bitmexbtc/usd'
  13.         cache_data = ctx.price_cache.get(key_name)
  14.  
  15.         if not cache_data or abs(time.time() - cache_data['timestamp']) > CACHE_EXP_TIME:
  16.             # Value for key does not exist or cache is old, refresh it
  17.             ticker = await exchange.fetch_ticker(pair)  # Get new ticker (!!!)
  18.             cache_data = {
  19.                 'ticker': ticker,
  20.                 'timestamp': time.time(),
  21.             }
  22.             ctx.price_cache[key_name] = cache_data
  23.  
  24.         return cache_data['ticker']
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top