重定向客户端缓存

pyredis-cache的Python项目详细描述


Pyredis缓存

用于redis的python缓存客户端。

支持Python3.5及更高版本。

功能

  • 在sync_func和缓存之间自动同步数据
  • 异步/等待用于同步函数的异步执行

依赖关系

安装

  1. 通过pip:pip install pyredis-cache
  2. 手动:python setup.py install。首先安装依赖项

开始

  • 简单的redis缓存
# Import redis Connection pool and CacheClient.fromredisimportConnectionPoolfromredis_cacheimportCacheClientimportjson# Initialize the Connection pool.redis_pool=ConnectionPool()# Create a data synchronisation function# The first argument should define the uniqueness of the datadefsync_func(student_id,lots_of_args):# fetch the data however you want toreturndata# Lets assume that the data returned is of type `dict`# Initialize the cache clientcache=CacheClient(redis_pool,"Student",sync_func,set_func=json.dumps,get_func=json.loads,expire_time=10)# for persistent cache, assign 0 to expire_time (default)# This will set cache for ID: 12cache.set(12)# This will set cache for ID: 13, with custom datacache.set(13,data={"name":"John Doe","age":16})# This will get cache for ID: 12 (no need to do a set, it will automatically set the data)cache.get(12)# The get function will pass extra arguments to the sync_func, same works with set.cache.get(14,age=15)# This will delete the cache for ID: 12cache.delete(12)
  • 缓存到单个哈希中。我仍在努力赚很多钱 更灵活的缓存机制
# Import redis Connection pool and HashCacheClient.fromredisimportConnectionPoolfromredis_cacheimportCacheClientimportjson# Initialize the Connection pool.redis_pool=ConnectionPool()# Create a data synchronisation function# The first argument should define the uniqueness of the datadefsync_func(student_id,lots_of_args):# fetch the data however you want toreturndata# Lets assume that the data returned is of type `dict`# Initialize the cache client.hcache=HashCacheClient(redis_pool,"Class","Student",sync_func,set_func=dumps,get_func=loads)# expire_time not supported.# This will set cache for ID: 12 for hash_id: 3hcache.set(3,12)# This will get cache for ID: 12 for hash_id: 3 (no need to do a set, it will automatically set the data)hcache.get(3,12)# This will delete the cache for ID: 12 for hash_id: 3hcache.delete(3,12)# This will delete the hash with hash_id: 3hcache.delete_hash(3)

术语

  • key:是缓存对象的标识符。例如student(类型 字符串)
  • 标识:是特定键的索引。A的卷号 学生12(int类型)
  • sync_func:将数据存储在缓存中的函数。
  • set-func:数据解析函数,用于将数据设置到缓存中。例如,如果 如果要设置dict值,则必须将其设置为 json.dumps
  • get_func:用于将数据放入缓存的数据解析函数。例如,如果 如果要设置dict值,则必须将其设置为 json.loads
  • hash_key和hash_id:哈希的唯一标识符。如。 哈希键可以是grade12,哈希ID可以是2

密钥和标识以一种独特的方式组合在一起 存储缓存对象的对象。类似于哈希键 和散列ID。

异步

如果您的函数是异步的,即使用asyncawait 协同程序(PEP 492)。 必须使用事件循环才能运行使用此功能,如 异步和龙卷风。您可以执行以下操作:-

cache=CacheClient(redis_pool,"Student",sync_func,set_func=json.dumps,get_func=json.loads,expire_time=10,asynchronous=True)asyncdefsome_func():data=awaitcache.get(23)

注意:缓存客户端是同步的,因为redis服务器相当 快。使用正确的设置,您可以预期接近0的延迟。还有 Tornado Redis的创建者建议如果建筑减少 设置回调所引起的开销。(见帖子: http://stackoverflow.com/a/15596969/2248966

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

推荐PyPI第三方库


热门话题
java从提供的列表中获取非重复值   java为什么Thymeleaf从HashMap返回字符串?   java将矩阵推到窗口的中心   java如何包含webapp_配置。txt到播放的dist-zip?   java实现产品密钥   WebSphereJava。lang.UnsupportedClassVersionError:JVMCFRE003错误的主要版本;class=org/slf4j/impl/StaticLoggerBinder,偏移量=6   使用java创建动态树结构   java为什么JVM在一次繁忙的旋转暂停后,会对同一代码块显示更多的延迟?   java无法使用distrolessdebug访问jarfile docker   java JMeter如何将多行响应数据传递给ForEach控制器请求   java空指针异常碎片活动   java改变JFileChooser的外观   性能Java位集:高效地查找所有真实位?   java可扩展的recyclerview实现