Skip to main content

这是一个用于管理和缓存数据的 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cappedttlcache-0.1.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cappedttlcache-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file cappedttlcache-0.1.1.tar.gz.

File metadata

  • Download URL: cappedttlcache-0.1.1.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

Hashes for cappedttlcache-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d460670c20f3294025ba4814fa5386ade21f9f1f088e68e8cc244a2d90bc29d5
MD5 fcf1abe53d4ddfb2ad586fee372fc956
BLAKE2b-256 8a7a957c4e8794e4d9d69c6f1e5d5cfde24798d17d1dc776bf9743acb27cb5d7

See more details on using hashes here.

File details

Details for the file cappedttlcache-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: cappedttlcache-0.1.1-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

Hashes for cappedttlcache-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f929cdd4511c45cc209cd8a93e230592afb3a98dd84799f3f796147d921e56e7
MD5 b073b3806a59853b13f16d1f30e6b76f
BLAKE2b-256 ab3a1d000d1aa2770e1b40c0a95de83cdf73f1a8e4a898610a86102d50d9057f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page