netclock/countdown/countdown.py

46 lines
1.4 KiB
Python
Raw Normal View History

2020-09-13 13:07:11 +00:00
from time import time
from uuid import UUID, uuid4
2020-10-26 10:07:42 +00:00
from walrus import Walrus
2020-09-13 13:07:11 +00:00
class Cache:
__instance = None
@staticmethod
2020-10-26 10:07:42 +00:00
def get_instance():
if Cache.__instance is None:
2020-09-13 13:07:11 +00:00
Cache()
return Cache.__instance
def __init__(self):
2020-10-26 10:07:42 +00:00
if Cache.__instance is None:
2020-09-13 13:07:11 +00:00
raise Exception("Cache is a singleton. Use Cache.getInstance()")
Cache.__instance = self
self.db = Walrus(host='localhost', port=6379, db=0)
def add_countdown(self, total: float) -> UUID:
countdown_id = uuid4()
countdown = self.db.Hash(str(countdown_id))
countdown.update(id=str(countdown_id), start=-1, total=total)
return self.get_countdown(str(countdown_id))
def start_countdown(self, id: UUID) -> dict:
countdown = self.db.Hash(str(id))
countdown.update(start=time())
return countdown.as_dict(decode=True)
def stop_countdown(self, id: UUID) -> dict:
countdown = self.db.Hash(str(id))
countdown.update(start=-1)
return countdown.as_dict(decode=True)
def reset_countdown(self, id: UUID) -> dict:
countdown = self.db.Hash(str(id))
countdown.update(start=time())
return countdown.as_dict(decode=True)
def get_countdown(self, id: UUID) -> dict:
countdown = self.db.Hash(str(id))
return countdown.as_dict(decode=True)