这是一个用于管理和缓存数据的 Python 模块。该模块支持通用缓存数据存储,具有最大条目数和条目有效时间的限制。
Project description
CappedTTLCache
这是一个用于管理和缓存数据的 Python 模块。该模块支持通用缓存数据存储,具有最大条目数和条目有效时间的限制。
特性
- 支持设置最大缓存条目数
- 支持设置缓存条目的有效时间(TTL)
- 自动删除过期条目
- 支持获取当前存储的所有缓存条目
- 支持根据标识符获取特定的缓存条目
- 支持获取当前存储中的缓存条目数量
- 支持注册和注销监听器,以便在数据更新时执行特定操作
安装
确保你已经安装了 Python 3.7 或更高版本
使用说明
1. 导入模块
首先,导入 CappedTTLCache 类:
from CappedTTLCache import CappedTTLCache # 假设你的模块名为 CappedTTLCache.py
2. 创建缓存实例
创建一个 CappedTTLCache 实例,指定最大条目数和缓存条目的有效时间(以秒为单位):
cache = CappedTTLCache(max_items=5, ttl_seconds=10)
3. 添加缓存条目
使用 add_item 方法添加一个新的缓存条目到存储中:
await cache.add_item("item1", "这是第一个缓存条目")
4. 获取缓存条目
使用 get_items 方法获取当前存储的所有缓存条目,可以选择性地按相反顺序排序和限制返回的条目数量:
items = cache.get_items(reverse=True, count=3)
print(items)
5. 根据标识符获取特定的缓存条目
使用 get_item 方法s:
items = cache.get_item(key="item1")
print(items)
6. 获取缓存条目数量
使用 get_count 方法获取当前存储中的缓存条目数量:
count = cache.get_count()
print(f"当前缓存条目数量: {count}")
7. 移除缓存条目
使用 remove_item 方法从存储中移除一个指定标识的缓存条目:
cache.remove_item("item1")
8. 注册监听器
定义一个异步函数作为监听器,当缓存更新时,该函数将被调用:
async def cache_update_listener():
print("缓存数据已更新!")
使用 register_listener 方法将监听器添加到 CappedTTLCache 实例中:
cache.register_listener(cache_update_listener)
9. 注销监听器
如果不再需要某个监听器,可以使用 unregister_listener 方法将其移除:
cache.unregister_listener(cache_update_listener)
10. 示例代码
以下是一个完整的示例,演示了如何使用 CappedTTLCache:
import asyncio
from CappedTTLCache import CappedTTLCache
async def cache_update_listener():
print("缓存数据已更新!")
async def main():
# 创建缓存实例
cache = CappedTTLCache(max_items=5, ttl_seconds=10)
# 注册监听器
cache.register_listener(cache_update_listener)
# 添加缓存条目
await cache.add_item("item1", "这是第一个缓存条目")
await cache.add_item("item2", "这是第二个缓存条目")
# 获取缓存条目
items = cache.get_items()
print("当前缓存条目:", items)
# 获取缓存条目数量
count = cache.get_count()
print(f"当前缓存条目数量: {count}")
# 等待一段时间后查看缓存条目
await asyncio.sleep(5)
items = cache.get_items()
print("5秒后缓存条目:", items)
# 等待超过缓存时间,查看缓存条目
await asyncio.sleep(6)
items = cache.get_items()
print("超过TTL后的缓存条目:", items)
# 注销监听器
cache.unregister_listener(cache_update_listener)
# 运行示例
# asyncio.run(main())
API 说明
CappedTTLCache(max_items: int, ttl_seconds: int)
- 参数:
max_items: 存储的最大条目数(整数)。ttl_seconds: 缓存条目的有效时间(以秒为单位)。
`
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cappedttlcache-0.1.0.tar.gz.
File metadata
- Download URL: cappedttlcache-0.1.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.10.8 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0460a610f9df7c7dc2e8c8b8ad34098ac36495d4f6fb9e3250b42a9ff9f3de4
|
|
| MD5 |
c4d57354bceb851cd30cad91494c42ec
|
|
| BLAKE2b-256 |
5fa0a98b4506a5e56966d21d15fcb8ae8c48d51539fcce514db82177708cbe90
|
File details
Details for the file cappedttlcache-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cappedttlcache-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.10.8 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6bef0363b3a1c1a45e3fe9873d30f43f99892a5a1ddbf43abe24ada75cc96f7
|
|
| MD5 |
3cf1ef6593d8c3ae26ccc4fda6904a07
|
|
| BLAKE2b-256 |
438de06e2ac132a49d7706ab599c70126d99b760a567e861f91423b400424a10
|