Skip to content
项目

关于

支持 Redis、Memcached 和内存后端的 asyncio 缓存管理器

数据与分析

这些数字如何统计

实测星标增长

24小时
0
7天
+1
15天
+1
1个月
0

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

aiocache

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

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.
  • pip install aiocache
  • pip install aiocache[redis]
  • pip install aiocache[memcached]
  • pip install aiocache[redis,memcached]
  • pip install aiocache[msgpack]

Using a cache is as simple as

>>> import asyncio
>>> from aiocache import SimpleMemoryCache
>>> cache = SimpleMemoryCache()  # Or RedisCache, MemcachedCache...
>>> with asyncio.Runner() as runner:
>>>     runner.run(cache.set('key', 'value'))
True
>>>     runner.run(cache.get('key'))
'value'

Or as a decorator

import asyncio

from collections import namedtuple

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

Result = namedtuple('Result', "content, status")
redis_client = redis.Redis(host="127.0.0.1", port=6379)
redis_cache = RedisCache(redis_client, namespace="main")


@cached(redis_cache, 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)


async def run():
    async with redis_client, redis_cache:
        await cached_call()
        await cached_call()
        await cached_call()
        await redis_cache.delete("key")

if __name__ == "__main__":
    asyncio.run(run())

Aiocache provides 3 main entities:

  • backends: Allow you specify which backend you want to use for your cache. See the docs for a full list of supported backends.
  • 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

In examples folder you can check different use cases:

探索相关项目

加载中…