aiocache 0.12.2-1 source package in Ubuntu
Changelog
aiocache (0.12.2-1) unstable; urgency=medium * New upstream release. -- Gianfranco Costamagna <email address hidden> Thu, 17 Aug 2023 08:26:18 +0200
Upload details
- Uploaded by:
- Gianfranco Costamagna
- Uploaded to:
- Sid
- Original maintainer:
- Gianfranco Costamagna
- Architectures:
- all
- Section:
- misc
- Urgency:
- Medium Urgency
See full publishing history Publishing
Series | Published | Component | Section | |
---|---|---|---|---|
Oracular | release | universe | misc | |
Noble | release | universe | misc | |
Mantic | release | universe | misc |
Downloads
File | Size | SHA-256 Checksum |
---|---|---|
aiocache_0.12.2-1.dsc | 1.9 KiB | e9b9874cc90d862e1cf198e6ee5707b7dc15f806d6390643d3734b456929fbc9 |
aiocache_0.12.2.orig.tar.gz | 128.8 KiB | b41c9a145b050a5dcbae1599f847db6dd445193b1f3bd172d8e0fe0cb9e96684 |
aiocache_0.12.2-1.debian.tar.xz | 3.3 KiB | d0565b9017b6df9bb47626225e6a806baef54e1382f7217b22b6079d526a51d0 |
Available diffs
- diff from 0.12.1-1 to 0.12.2-1 (2.9 KiB)
No changes file available.
Binary packages built by this source
- python3-aiocache: multi backend asyncio cache
Asyncio cache supporting multiple backends (memory, redis and memcached).
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.
.
.. role:: python(code)
:language: python
.
.. contents::
.
.. section-numbering:
.
Usage
=====
.
Using a cache is as simple as
.
.. code-block:: python
.
>>> import asyncio
>>> from aiocache import Cache
>>> cache = Cache(Cache.MEMORY) # Here you can also use Cache.REDIS and Cache.MEMCACHED, default is Cache.MEMORY
>>> with asyncio.Runner() as runner:
>>> runner.run(cache. set('key' , 'value'))
True
>>> runner.run(cache. get('key' ))
'value'
.
Or as a decorator
.
.. code-block:: python
.
import asyncio
.
from collections import namedtuple
.
from aiocache import cached, Cache
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=Cache.REDIS, key="key", serializer=PickleSerialize r(), port=6379, namespace="main")
async def cached_call():
print( "Sleeping for three seconds zzzz.....")
await asyncio.sleep(3)
return Result("content", 200)
.
.
async def run():
await cached_call()
await cached_call()
await cached_call()
cache = Cache(Cache.REDIS, endpoint="127.0. 0.1", port=6379, namespace="main")
await cache.delete("key")
.
if __name__ == "__main__":
asyncio. run(run( ))
.
The recommended approach to instantiate a new cache is using the `Cache` constructor.
However you can also instantiate directly using `aiocache.RedisCache` ,
`aiocache.SimpleMemoryCac he` or `aiocache. MemcachedCache` .
.
.
You can also setup cache aliases so its easy to reuse configurations
.
.. code-block:: python
.
import asyncio
.
from aiocache import caches
.
# You can use either classes or strings for referencing classes
caches.set_config( {
'default': {
'cache': "aiocache.SimpleMemoryCac he",
'serializer ': {
'class' : "aiocache. serializers. StringSerialize r"
}
},
'redis_alt': {
'cache': "aiocache.RedisCache" ,
'endpoint' : "127.0.0.1",
'port': 6379,
'timeout' : 1,
'serializer ': {
'class' : "aiocache. serializers. PickleSerialize r"
},
'plugins' : [
{'class' : "aiocache. plugins. HitMissRatioPlu gin"},
{'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"
.
.
async def test_alias():
await default_cache()
await alt_cache()
.
await caches.get("redis_ alt").delete( "key")
.
.
if __name__ == "__main__":
asyncio. run(test_ alias() )