一个示例包。地理分布LRU(最近最少使用)缓存

distributed-lru-cache的Python项目详细描述


概述

一个示例包。具有过期时间的地域分布LRU(最近最少使用)缓存

  • 自由软件:BSD 3条款许可

安装

pip install distributed-lru-cache

您还可以安装开发中版本:

^{pr2}$

文档

项目

我们要优化我们编写的软件的每一个部分。你的目标是编写一个可以 集成到我们的堆栈中。每天处理网络问题,延迟是我们最大的问题。 因此,您的挑战是使用 时间到期。这个图书馆将被我们的许多服务广泛使用,因此它需要满足 以下标准:

1 - Simplicity. Integration needs to be dead simple.
    [covered, this is an standard library]
2 - Resilient to network failures or crashes.
    [covered, using redis]
3 - Near real time replication of data across Geolocation. Writes need to be in real time.
    [covered, using redis, replication of the data occurs in near real time and writes on redis are considered in real time]
4 - Data consistency across regions
    [covered, using redis, the data is always consistent through the database]
5 - Locality of reference, data should almost always be available from the closest region
    [partially covered, an additional iteration is needed in order to indicate on which region/instance/etc the data will be obtained]
6 - Flexible Schema
    [is a flexible schema, keys are stored in strings and values to, but the data could be parsed from an specific structure or model]
7 - Cache can expire
    [partially covered, the expiration functionality is rusty and need to be more flexible and robust]

它是如何工作的?在

此分布式LRU缓存:

LRU缓存是一种高效的缓存数据结构,可用于确定当缓存已满时应清除的内容。目标是始终在O(1)时间内访问最近使用最少的项。在

LRU缓存实现

要创建LRU逻辑,必须使用以下集合、数据结构和工具:

Deque

双端队列(deque)支持从两端添加和删除元素。在

收藏.deque::
返回一个新的deque对象,该对象使用iterable中的数据从左到右初始化(使用append())。如果未指定iterable,则新deque为空。

Deques是堆栈和队列的泛化(名称发音为“deck”,是“double end queue”的缩写)。Deques支持线程安全、内存高效的附件和pop,在任何一个方向上都具有近似相同的O(1)性能。在

字典

字典是用来映射或关联你想存储你需要的键的东西。Python中的字典就像真实世界中的字典。Python字典定义为两个元素Keys和Values。在

Keys will be a single element Values can be a list or list within a list, numbers, etc.

Redis

Redis是一个开源(BSD许可),内存中的数据结构存储,用作数据库、缓存和消息代理。它支持数据结构,如字符串、哈希、列表、集合、带范围查询的排序集、位图、超日志、带有radius查询和流的地理空间索引。在

此库使用哈希

Redis散列存储键到值的映射。可以存储在散列中的值与可以存储为普通字符串的值相同:字符串本身。在

注意:这是为了维护内存数据库上备份的数据并加快写入和复制。在

方法的生命周期

看跌期权的生命周期:

The item is created/updated on the queue using a dictionary
LRU cache is updated (item is bumped up if it already exists)
Validates the capacity of the cache and remove the Last Recently Used key if no more space is found using the popleft() command
Validates if the key is in the cache instance and remove that key in order to create it again:
   - Create the key again on the local instance and redis
If a ttl(Time to live) was given, the item will expire in that amount of time

GET的生命周期:

LRU cache is checked for item, if it exists item is returned (item is bumped up) and the queue using the following steps:
- Remove the item from the queue
- Remove the key from redis hash
- Append the node again on the queue
- Create the key on redis hash again
- Obtain the value from the node/key and the value is returned

使用

要在项目中使用分布式LRU缓存,请执行以下操作:

from distributed_lru_cache.cache import LRUCache

lru = LRUCache(capacity=2, cache_name='lrucache', redis_host='localhost', redis_port=6379, redis_db=0, ttl=5)

lru.put('10', '1')
lru.put('20', '1', ttl=1)
lru.get('10')

其中:

capacity: The capacity of the cache instance (128 by default)
cache_name: The name of the cache instance to create ('lrucache' by default)
redis_host: The host name of redis server ('localhost' by default)
redis_port: The port of redis server (6379 by default)
redis_db: The database to use on redis (0 by default)
ttl: time to live, the expiration time (0 by default = No expiration)

方法:

put: To create a cache item into the cache instance could have an extra argument (ttl) to expire this specific item
get: The obtain a cache item altering the order of the items
peek: The obtain a cache item without altering the order of the items
set_redis_conn: To instantiate a specific redis connection after the item creation
clear_cache_instance: To clear the entire cache instance

开发

要运行所有测试运行:

tox

请注意,要合并来自所有tox环境的覆盖率数据,请执行以下操作:

Windows^{pr 10}$
Other^{pr 11}$

变更日志

  • PyPI的第一个版本。在

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

推荐PyPI第三方库


热门话题
java检查整数是0还是检查变量是null更好?   java Android Kotlin(初学者)使用File(),并从ACTION\u GET\u内容返回Uri   java JavaFx在“内部场景”和根场景之间切换   spring将XMLBean配置转换为java配置   java JPA HIBERNATE映射列两次(embeddedID和POJO)   c#单态模式模型在什么情况下适用?   java请求。getRemoteUser在特定时间后返回null?   spring boot中PUT api控制器的java my单元测试用例失败   java在字符串中互换地解析和替换值   java Android JNI在应用程序中检测到错误:调用JNI GetMethodID时出现挂起异常   JavaSpringDataMongo:使用非简单键持久化映射   爪哇玻璃鱼连接被拒绝   java如何在用户注册时发送特定电子邮件id的自动回复?   Java列表:实例化时和之后的赋值之间的差异