pip install aiocache
If you don't need redis or memcached support, you can install as follows:
AIOCACHE_REDIS=no pip install aiocache # Don't install redis client (aioredis)
AIOCACHE_MEMCACHED=no pip install aiocache # Don't install memcached client (aiomcache)
Using a cache is as simple as
>>> import asyncio
>>> loop = asyncio.get_event_loop()
>>> from aiocache import SimpleMemoryCache
>>> cache = SimpleMemoryCache()
>>> loop.run_until_complete(cache.set('key', 'value'))
True
>>> loop.run_until_complete(cache.get('key'))
'value'
Here we are using the :ref:`simplememorycache` but you can use any other listed in :ref:`caches`. All caches contain the same minimum interface which consists on the following functions:
add
: Only adds key/value if key does not exist. Otherwise raises ValueError.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.
You can also setup cache aliases like in Django settings:
.. literalinclude:: ../examples/cached_alias_config.py :language: python :linenos: :emphasize-lines: 6-26
In examples folder you can check different use cases:
- Using cached decorator.
- Using multi_cached decorator.
- Configuring cache class default args
- Simple LRU plugin for memory
- Using marshmallow as a serializer
- TimingPlugin and HitMissRatioPlugin demos
- Storing a python object in Redis
- Creating a custom serializer class that compresses data
- Integrations with frameworks like Sanic, Aiohttp and Tornado
.. toctree:: caches serializers plugins configuration decorators testing