Example #1
0
async def create_cache(cache_key, content, minutes=30):

    pickle_protocol = pickle.HIGHEST_PROTOCOL

    cache = Cache.filter(cache_key=cache_key)
    cache_exists = await cache.exists()

    if not cache_exists:

        expires = datetime.utcnow() + timedelta(minutes=minutes)
        pickled = pickle.dumps(content, pickle_protocol)
        b64encoded = base64.b64encode(pickled).decode('latin1')

        try:
            cache = Cache(cache_key=cache_key,
                          content=b64encoded,
                          expires=expires)

            await cache.save()
            return True
        except:
            return False

    return False