Skip to main content

multi backend asyncio cache

Project description

Asyncio cache supporting multiple backends (memory, redis and memcached).

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 https://img.shields.io/pypi/pyversions/aiocache.svg https://api.codacy.com/project/badge/Grade/96f772e38e63489ca884dbaf6e9fb7fd https://img.shields.io/badge/code%20style-black-000000.svg

This library aims for simplicity over specialization. All caches contain the same minimum interface which consists on the following functions:

  • add: Only adds key/value if key does not exist.

  • get: Retrieve value identified by key.

  • set: Sets key/value.

  • multi_get: Retrieves multiple key/values.

  • multi_set: Sets multiple key/values.

  • exists: Returns True if key exists False otherwise.

  • increment: Increment the value stored in the given key.

  • delete: Deletes key and returns number of deleted items.

  • clear: Clears the items stored.

  • raw: Executes the specified command using the underlying client.

Installing

  • pip install aiocache

  • pip install aiocache[redis]

  • pip install aiocache[memcached]

  • pip install aiocache[redis,memcached]

  • pip install aiocache[msgpack]

Usage

Using a cache is as simple as

>>> import asyncio
>>> loop = asyncio.get_event_loop()
>>> from aiocache import SimpleMemoryCache  # Here you can also use RedisCache and MemcachedCache
>>> cache = SimpleMemoryCache()
>>> loop.run_until_complete(cache.set('key', 'value'))
True
>>> loop.run_until_complete(cache.get('key'))
'value'

Or as a decorator

import asyncio

from collections import namedtuple

from aiocache import cached, RedisCache
from aiocache.serializers import PickleSerializer
# With this we can store python objects in backends like Redis!

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


@cached(
    ttl=10, cache=RedisCache, key="key", serializer=PickleSerializer(), port=6379, namespace="main")
async def cached_call():
    print("Sleeping for three seconds zzzz.....")
    await asyncio.sleep(3)
    return Result("content", 200)


def run():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(cached_call())
    loop.run_until_complete(cached_call())
    loop.run_until_complete(cached_call())
    cache = RedisCache(endpoint="127.0.0.1", port=6379, namespace="main")
    loop.run_until_complete(cache.delete("key"))

if __name__ == "__main__":
    run()

You can also setup cache aliases so its easy to reuse configurations

import asyncio

from aiocache import caches, SimpleMemoryCache, RedisCache
from aiocache.serializers import StringSerializer, PickleSerializer

# You can use either classes or strings for referencing classes
caches.set_config({
    'default': {
        'cache': "aiocache.SimpleMemoryCache",
        'serializer': {
            'class': "aiocache.serializers.StringSerializer"
        }
    },
    'redis_alt': {
        'cache': "aiocache.RedisCache",
        'endpoint': "127.0.0.1",
        'port': 6379,
        'timeout': 1,
        'serializer': {
            'class': "aiocache.serializers.PickleSerializer"
        },
        'plugins': [
            {'class': "aiocache.plugins.HitMissRatioPlugin"},
            {'class': "aiocache.plugins.TimingPlugin"}
        ]
    }
})


async def default_cache():
    cache = caches.get('default')   # This always returns the SAME instance
    await cache.set("key", "value")
    assert await cache.get("key") == "value"


async def alt_cache():
    cache = caches.create('redis_alt')   # This creates a NEW instance on every call
    await cache.set("key", "value")
    assert await cache.get("key") == "value"


def test_alias():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(default_cache())
    loop.run_until_complete(alt_cache())

    loop.run_until_complete(caches.get('redis_alt').delete("key"))


if __name__ == "__main__":
    test_alias()

How does it work

Aiocache provides 3 main entities:

  • backends: Allow you specify which backend you want to use for your cache. Currently supporting: SimpleMemoryCache, RedisCache using aioredis and MemCache using aiomcache.

  • serializers: Serialize and deserialize the data between your code and the backends. This allows you to save any Python object into your cache. Currently supporting: StringSerializer, PickleSerializer, JsonSerializer, and MsgPackSerializer. But you can also build custom ones.

  • plugins: Implement a hooks system that allows to execute extra behavior before and after of each command.

If you are missing an implementation of backend, serializer or plugin you think it could be interesting for the package, do not hesitate to open a new issue.

docs/images/architecture.png

Those 3 entities combine during some of the cache operations to apply the desired command (backend), data transformation (serializer) and pre/post hooks (plugins). To have a better vision of what happens, here you can check how set function works in aiocache:

docs/images/set_operation_flow.png

Amazing examples

In examples folder you can check different use cases:

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.10.1.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiocache-0.10.1-py2.py3-none-any.whl (24.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file aiocache-0.10.1.tar.gz.

File metadata

  • Download URL: aiocache-0.10.1.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for aiocache-0.10.1.tar.gz
Algorithm Hash digest
SHA256 11eaef6c1746d21ecc8ad551c6140f3b10d9ec1900d6b5bb42ccfc885dafc7a1
MD5 ab8709e94315e0f4886a02740777b29d
BLAKE2b-256 c8d3e1710a6da6dc93920fc949b4a10d3ca9ca4b9dc30898158a6c9d77c80f76

See more details on using hashes here.

File details

Details for the file aiocache-0.10.1-py2.py3-none-any.whl.

File metadata

  • Download URL: aiocache-0.10.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 24.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for aiocache-0.10.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0e95a86d092e6c440e786cfac7b560d215dd712cdafe82ddce376050ca758763
MD5 90d3b8d45f5f4c5db6ea2a2975f4a63e
BLAKE2b-256 61187ddc03efbbe49b47d2b55ef8ece494c446090b9ae404fed39e4285eaf2e5

See more details on using hashes here.

Supported by

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