-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
30 lines (22 loc) · 974 Bytes
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import redis
class Cache:
def __init__(self, username: str, password: str, host: str, port: int, db: int):
self.cache = redis.Redis(username=username, password=password,
host=host, port=port, db=db,
decode_responses=True)
def set_city(self, user_id: int, city: str):
self.cache.set(str(user_id), city)
def get_city(self, user_id: int):
response = self.cache.get(str(user_id))
return response
def set_weather_now(self, city: str, data: dict):
self.cache.hset(city, mapping=data)
self.cache.expire(city, 3600)
def get_weather_now(self, city: str):
response = self.cache.hgetall(city)
return response
def set_weather_5_days(self, city: str, data: str):
self.cache.set(city+'5', data, ex=86400)
def get_weather_5_days(self, city: str):
response = self.cache.get(city+'5')
return response