You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
948 B
Python
36 lines
948 B
Python
2 years ago
|
from json import loads as json_loads
|
||
|
from json import dumps as json_dumps
|
||
|
from datetime import timedelta
|
||
|
|
||
|
from redis import Redis
|
||
|
|
||
|
from app.library.market import get_market_data
|
||
|
from app import config
|
||
|
|
||
|
|
||
|
class Cache(object):
|
||
|
def __init__(self):
|
||
|
self.redis = Redis(host=config.REDIS_HOST, port=config.REDIS_PORT)
|
||
|
|
||
|
def store_data(self, item_name, expiration_minutes, data):
|
||
|
self.redis.setex(
|
||
|
item_name,
|
||
|
timedelta(minutes=expiration_minutes),
|
||
|
value=data
|
||
|
)
|
||
|
|
||
|
def get_coin_price(self, coin_name):
|
||
|
key_name = f'{coin_name}_price'
|
||
|
data = self.redis.get(key_name)
|
||
|
if data:
|
||
|
return json_loads(data)
|
||
|
else:
|
||
|
d = get_market_data(coin_name)
|
||
|
data = {
|
||
|
key_name: d['market_data']['current_price'],
|
||
|
}
|
||
|
self.store_data(key_name, 4, json_dumps(data))
|
||
|
return data
|
||
|
|
||
|
cache = Cache()
|