Skip to main content

ExpiringDictx

codecov Build Status PyPI version Python version License


简介 | Introduction

这是一个带有过期时间的字典数据结构,适用于缓存和临时存储。
This is a dictionary data structure with an expiration time, suitable for caching and temporary storage.

安装 | Install

使用 pip 安装 | Install with pip

pip install expiringdictx

使用 poetry 安装 | Install with poetry

poetry add expiringdictx

使用 pdm 安装 | Install with pdm

pdm add expiringdictx

使用 | Usage

ExpiringDict

from datetime import timedelta
from expiringdictx import ExpiringDict

# 创建一个 ExpiringDict 实例 | Create an ExpiringDict instance
cache = ExpiringDict[str, str](capacity=100, default_age=60)

# 添加一个元素,过期时间为默认值 | Add an item with the default expiration time
cache["key0"] = "value0"
# 添加一个元素,过期时间为 30 秒 | Add an item with an expiration time of 30 seconds
cache["key1", 30] = "value1"
# 添加一个元素,过期时间为 2 分钟 | Add an item with an expiration time of 2 minutes
cache["key2", timedelta(minutes=2)] = "value2"
# 或者使用 set 方法 | Or use the set method
cache.set("key3", "value3", 60)

# 获取一个元素 | Get an item
value0 = cache["key0"]
value1 = cache.get('key1')
# 获取时附带过期时间 | Get with expiration time
value2, deadtime2 = cache.get_with_deadtime('key2')

# 获取并删除一个元素 | pop an item
value3 = cache.pop('key3')
value4 = cache.popitem(least_recently=True)

# 获取元素ttl(剩余存活时间) | Get the ttl(time to live) of an item
ttl = cache.ttl('key0')
# 获取元素ddl(过期时间) | Get the ddl(deadline) of an item
ddl = cache.ddl('key0')

# 按照给定keys创建一个新的ExpiringDict | Create a new ExpiringDict with the given keys
new_cache = cache.fromkeys(['key0', 'key1', 'key2'], "default_value", 60)
# 按照给定可映射对象创建一个新的ExpiringDict | Create a new ExpiringDict with the given mapping
new_cache = cache.frommapping({'key0': 'value0', 'key1': ('value1', 1), 'key2': ('value2', timedelta(hours=2))}, 60)
# 从另一个ExpiringDict创建一个新的ExpiringDict | Create a new ExpiringDict from another ExpiringDict
new_cache = cache.fromexpiringdict(cache, 60)

测试 | Tests

运行单元测试: | Run the unit tests:

pytest tests/

致谢 | Thanks

lru-dict: ExpiringDict 的内部存储实现
expiringdict: 本项目的灵感来源,在此基础上添加类型注解和其他功能

许可 | License

MIT


Download files

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

Source Distribution

expiringdictx-1.0.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

expiringdictx-1.0.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file expiringdictx-1.0.0.tar.gz.

File metadata

  • Download URL: expiringdictx-1.0.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for expiringdictx-1.0.0.tar.gz
Algorithm Hash digest
SHA256 427027afda4d743fb949d409dfc4e48188d677ae88a535a9a3af08da46023cc6
MD5 23135c85968d8aef60a7cd31edfba731
BLAKE2b-256 84612692d5236adb2c6146ec23941e7dec6d53b3cdb364c78292a40307fdd2ea

See more details on using hashes here.

File details

Details for the file expiringdictx-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: expiringdictx-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for expiringdictx-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78d5a3b0ad59fa57bb3f35e3ec3707e4256eadd83f3bae345be4cc6612f456ef
MD5 fb367a029f77e1f074caedc0d12d6856
BLAKE2b-256 7b74a3571c025bc26c27582db448478ea47073b4daa924007907746ac4e9c451

See more details on using hashes here.

Release history Release notifications | RSS feed

1.1.0

2 files

1.0.1

2 files

This release

1.0.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page