1
0
Fork 0
mirror of synced 2024-06-28 19:20:34 +12:00

Reimplemented cache, changed up the checking for it for rare odd cases

This commit is contained in:
phxntxm 2016-08-31 22:24:21 -05:00
parent bf4041fa64
commit 5cdce2cad7

View file

@ -1,6 +1,7 @@
import ruamel.yaml as yaml
import asyncio
import rethinkdb as r
import pendulum
loop = asyncio.get_event_loop()
@ -33,10 +34,12 @@ class Cache:
def __init__(self, key):
self.key = key
self.values = {}
self.refreshed = pendulum.utcnow()
loop.create_task(self.update())
async def update(self):
self.values = await _get_content(self.key)
self.refreshed = pendulum.utcnow()
def __repr__(self):
return self.values
@ -89,8 +92,8 @@ cache = {}
sharded_data = {}
# Populate cache with each object
# for k in possible_keys:
# cache[k] = Cache(k)
for k in possible_keys:
cache[k] = Cache(k)
def command_prefix(bot, message):
@ -130,9 +133,12 @@ async def save_content(table: str, content):
await conn.close()
async def disabled_get_content(key: str):
async def get_content(key: str):
cached = cache.get('key')
if cached is None or len(cached) == 0:
# We want to check here if the key exists in cache, and it was not created more than an hour ago
# We also want to make sure that if what we're getting in cache has content
# if not, lets make sure something didn't go awry, by getting from the database instead
if cached is None or len(cached.values) == 0 or (pendulum.utcnow() - cached.refreshed).hours >= 1:
value = await _get_content(key)
# If we found this object not cached, cache it
cache['key'] = value
@ -140,10 +146,6 @@ async def disabled_get_content(key: str):
value = cached.values
return value
async def get_content(key: str):
value = await _get_content(key)
return value
# This is our internal method to get content from the database
async def _get_content(key: str):
# We need to make sure we're using asyncio