多后端异步缓存

aiocache的Python项目详细描述


支持多个后端(内存、redis和memcached)的异步缓存。

https://travis-ci.org/argaen/aiocache.svg?branch=masterhttps://codecov.io/gh/argaen/aiocache/branch/master/graph/badge.svghttps://badge.fury.io/py/aiocache.svghttps://img.shields.io/pypi/pyversions/aiocache.svghttps://api.codacy.com/project/badge/Grade/96f772e38e63489ca884dbaf6e9fb7fdhttps://img.shields.io/badge/code%20style-black-000000.svg

这个库的目的是简单而不是专门化。所有缓存都包含相同的最小接口,该接口包含以下功能:

  • ^ 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.rediscacheaiocache.simplememorycacheaiocache.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.
docs/images/architecture.png

这3个实体在一些缓存操作期间合并,以应用所需的命令(后端)、数据转换(序列化器)和前/后挂钩(插件)。为了更好地了解发生了什么,您可以在这里检查set函数如何在aiocache中工作:

docs/images/set_operation_flow.png

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Android HttpClient cookies   如何使用Java在远程系统上运行SSH命令?   java从字符串数组中的字符串末尾删除“,”   在One plus 3t手机上,当应用程序被终止或从最近的应用程序中刷出时,java Android FCM推送通知不起作用   java如何使垂直滚动条始终位于jtable的末尾   在java中解析迄今为止“未知”的字符串   javascript在Java中获取Nashorn JsonObject   java windows 10和ubuntu可以使用相同的JDK吗?   java在不同的文件中记录不同的日志。但所有日志都放在同一个文件中   具有特定jdk的java Gradle构建项目   xml Java web服务生成错误响应   javascript Jaggery文件更改不显示   java输出二进制搜索树数组   将BufferedReader解析为JSON对象时,java在位置处意外标记文件结尾