Skip to content

Instantly share code, notes, and snippets.

@asoong
Created July 19, 2019 23:23
Show Gist options
  • Save asoong/38f73aafef33b161fcc3ead80daac94b to your computer and use it in GitHub Desktop.
Save asoong/38f73aafef33b161fcc3ead80daac94b to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class CachedWethPriceRepository
class InvalidKey < StandardError; end
WETH_PRICE_REDIS_KEY = 'weth_price'
WETH_PRICE_REDIS_TTL = 90
def initialize(coin_repository, redis)
@coin_repository = coin_repository
@redis = redis
end
def weth_price
weth_price = @redis.get(WETH_PRICE_REDIS_KEY)
if weth_price.blank?
weth_price = update_cache_for_weth_price_from_db
end
weth_price
end
def update_cache_for_weth_price_from_db
weth = @coin_repository.weth
weth_price_usd = weth.price_usd
@redis.setex(WETH_PRICE_REDIS_KEY,
WETH_PRICE_REDIS_TTL,
weth_price_usd)
weth_price_usd
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment