Created
July 19, 2019 23:23
-
-
Save asoong/38f73aafef33b161fcc3ead80daac94b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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