Skip to main content

Asynchronous redis cache

Project description

https://travis-ci.org/argaen/aiocache.svg?branch=master https://codecov.io/gh/argaen/aiocache/branch/master/graph/badge.svg https://badge.fury.io/py/aiocache.svg

An asynchronous cache implementation with multiple backends for asyncio. Used django-redis-cache and redis-simple-cache as inspiration for the initial structure.

Disclaimer: The code is still in alpha version so new versions may introduce breaking changes. Once version 1.0 is reached, deprecation policy will be introduced.

Supported backends:

This libraries aims for simplicity over specialization. It provides a common interface for all caches which allows to store any python object. The operations supported by all backends are:

  • add

  • exists

  • get

  • set

  • multi_get

  • multi_set

  • delete

Usage

Install the package with pip install aiocache.

cached decorator

import asyncio

from collections import namedtuple

from aiocache import cached, RedisCache
from aiocache.serializers import PickleSerializer

Result = namedtuple('Result', "content, status")


@cached(ttl=10)
async def async_main():
    print("First ASYNC non cached call...")
    await asyncio.sleep(1)
    return Result("content", 200)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    print(loop.run_until_complete(async_main()))
    print(loop.run_until_complete(async_main()))
    print(loop.run_until_complete(async_main()))
    print(loop.run_until_complete(async_main()))

The decorator by default will use the SimpleMemoryCache backend and the DefaultSerializer. If you want to use a different backend, you can call it with cached(ttl=10, backend=RedisCache). Also, if you want to use a specific serializer just use cached(ttl=10, serializer=DefaultSerializer())

Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiocache-0.1.1.tar.gz (4.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page