多后端异步缓存
aiocache的Python项目详细描述
支持多个后端(内存、redis和memcached)的异步缓存。
这个库的目的是简单而不是专门化。所有缓存都包含相同的最小接口,该接口包含以下功能:
- ^ TT1} $:仅在密钥不存在时添加密钥/值。
- get:检索由键标识的值。
- set:设置键/值。
- multi_get:检索多个键/值。
- multi_set:设置多个键/值。
- {TT6}$:否则,如果密钥存在错误,则返回true。
- increment:增加存储在给定密钥中的值。
- delete:删除键并返回已删除的项目数。
- clear:清除存储的项。
- raw:使用底层客户端执行指定的命令。
Installing
- pip install aiocache
- pip install aiocache[redis]
- pip install aiocache[memcached]
- pip install aiocache[redis,memcached]
- pip install aiocache[msgpack]
Usage
使用缓存非常简单
>>>importasyncio>>>loop=asyncio.get_event_loop()>>>fromaiocacheimportCache>>>cache=Cache(Cache.MEMORY)# Here you can also use Cache.REDIS and Cache.MEMCACHED, default is Cache.MEMORY>>>loop.run_until_complete(cache.set('key','value'))True>>>loop.run_until_complete(cache.get('key'))'value'
或作为装饰工
importasynciofromcollectionsimportnamedtuplefromaiocacheimportcached,Cachefromaiocache.serializersimportPickleSerializer# 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=PickleSerializer(),port=6379,namespace="main")asyncdefcached_call():print("Sleeping for three seconds zzzz.....")awaitasyncio.sleep(3)returnResult("content",200)defrun():loop=asyncio.get_event_loop()loop.run_until_complete(cached_call())loop.run_until_complete(cached_call())loop.run_until_complete(cached_call())cache=Cache(Cache.REDIS,endpoint="127.0.0.1",port=6379,namespace="main")loop.run_until_complete(cache.delete("key"))if__name__=="__main__":run()
建议使用cache构造函数实例化新缓存。但是,也可以使用aiocache.rediscache、aiocache.simplememorycache或aiocache.memcachedcache直接实例化。
您还可以设置缓存别名,使其易于重用配置
importasynciofromaiocacheimportcaches# You can use either classes or strings for referencing classescaches.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"}]}})asyncdefdefault_cache():cache=caches.get('default')# This always returns the SAME instanceawaitcache.set("key","value")assertawaitcache.get("key")=="value"asyncdefalt_cache():cache=caches.create('redis_alt')# This creates a NEW instance on every callawaitcache.set("key","value")assertawaitcache.get("key")=="value"deftest_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提供3个主要实体:
- 后端:允许指定要用于缓存的后端。当前支持:simplememorycache、使用aioredis重新缓存和使用aiomcache的memcache。
- serializers:序列化和反序列化代码和后端之间的数据。这允许您将任何python对象保存到缓存中。当前支持:StringSerializer、PickleSerializer、JsonSerializer和MsgPackSerializer。但你也可以定制。
- plugins:实现一个hooks系统,允许在每个命令之前和之后执行额外的行为。
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.
这3个实体在一些缓存操作期间合并,以应用所需的命令(后端)、数据转换(序列化器)和前/后挂钩(插件)。为了更好地了解发生了什么,您可以在这里检查set函数如何在aiocache中工作:
Amazing examples
在examples folder中,您可以检查不同的用例: