# ExpiringDictx
<div align="center">
[![codecov](https://codecov.io/gh/AzideCupric/expiringdictx/graph/badge.svg?token=879N3D5BJK)](https://codecov.io/gh/AzideCupric/expiringdictx)
[![Build Status](https://img.shields.io/github/actions/workflow/status/AzideCupric/expiringdictx/test.yml?branch=main)](https://github.com/AzideCupric/expiringdictx/actions/workflows/test.yml)
[![PyPI version](https://badge.fury.io/py/expiringdictx.svg)](https://badge.fury.io/py/expiringdictx)
[![Python version](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
[![License](https://img.shields.io/github/license/AzideCupric/expiringdictx)](https://github.com/AzideCupric/expiringdictx/blob/main/LICENSE)
</div>
---
## 简介 | Introduction
这是一个带有过期时间的字典数据结构,适用于缓存和临时存储。
This is a dictionary data structure with an expiration time, suitable for caching and temporary storage.
## 安装 | Install
### 使用 pip 安装 | Install with pip
```bash
pip install expiringdictx
```
### 使用 poetry 安装 | Install with poetry
```bash
poetry add expiringdictx
```
### 使用 pdm 安装 | Install with pdm
```bash
pdm add expiringdictx
```
## 使用 | Usage
### ExpiringDict
```python
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:
```bash
pytest tests/
```
## 致谢 | Thanks
[`lru-dict`](https://github.com/amitdev/lru-dict): ExpiringDict 的内部存储实现
[`expiringdict`](https://github.com/mailgun/expiringdict): 本项目的灵感来源,在此基础上添加类型注解和其他功能
## 许可 | License
MIT
---
Raw data
{
"_id": null,
"home_page": "https://github.com/AzideCupric/expiringdictx",
"name": "expiringdictx",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10,<4.0",
"maintainer_email": "",
"keywords": "expire,dict,lru",
"author": "AzideCupric",
"author_email": "rukuy@qq.com",
"download_url": "https://files.pythonhosted.org/packages/0b/0f/d0f6b3d8d550d307eb794e63187059f159abd750fe1d2969fe732c665130/expiringdictx-1.0.1.tar.gz",
"platform": null,
"description": "# ExpiringDictx\n\n<div align=\"center\">\n\n[![codecov](https://codecov.io/gh/AzideCupric/expiringdictx/graph/badge.svg?token=879N3D5BJK)](https://codecov.io/gh/AzideCupric/expiringdictx)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/AzideCupric/expiringdictx/test.yml?branch=main)](https://github.com/AzideCupric/expiringdictx/actions/workflows/test.yml)\n[![PyPI version](https://badge.fury.io/py/expiringdictx.svg)](https://badge.fury.io/py/expiringdictx)\n[![Python version](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)\n[![License](https://img.shields.io/github/license/AzideCupric/expiringdictx)](https://github.com/AzideCupric/expiringdictx/blob/main/LICENSE)\n\n</div>\n\n---\n\n## \u7b80\u4ecb | Introduction\n\n\u8fd9\u662f\u4e00\u4e2a\u5e26\u6709\u8fc7\u671f\u65f6\u95f4\u7684\u5b57\u5178\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u7f13\u5b58\u548c\u4e34\u65f6\u5b58\u50a8\u3002 \nThis is a dictionary data structure with an expiration time, suitable for caching and temporary storage.\n\n## \u5b89\u88c5 | Install\n\n### \u4f7f\u7528 pip \u5b89\u88c5 | Install with pip\n\n```bash\npip install expiringdictx\n```\n\n### \u4f7f\u7528 poetry \u5b89\u88c5 | Install with poetry\n\n```bash\npoetry add expiringdictx\n```\n\n### \u4f7f\u7528 pdm \u5b89\u88c5 | Install with pdm\n\n```bash\npdm add expiringdictx\n```\n\n## \u4f7f\u7528 | Usage\n\n### ExpiringDict\n\n```python\nfrom datetime import timedelta\nfrom expiringdictx import ExpiringDict\n\n# \u521b\u5efa\u4e00\u4e2a ExpiringDict \u5b9e\u4f8b | Create an ExpiringDict instance\ncache = ExpiringDict[str, str](capacity=100, default_age=60)\n\n# \u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\uff0c\u8fc7\u671f\u65f6\u95f4\u4e3a\u9ed8\u8ba4\u503c | Add an item with the default expiration time\ncache[\"key0\"] = \"value0\"\n# \u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\uff0c\u8fc7\u671f\u65f6\u95f4\u4e3a 30 \u79d2 | Add an item with an expiration time of 30 seconds\ncache[\"key1\", 30] = \"value1\"\n# \u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\uff0c\u8fc7\u671f\u65f6\u95f4\u4e3a 2 \u5206\u949f | Add an item with an expiration time of 2 minutes\ncache[\"key2\", timedelta(minutes=2)] = \"value2\"\n# \u6216\u8005\u4f7f\u7528 set \u65b9\u6cd5 | Or use the set method\ncache.set(\"key3\", \"value3\", 60)\n\n# \u83b7\u53d6\u4e00\u4e2a\u5143\u7d20 | Get an item\nvalue0 = cache[\"key0\"]\nvalue1 = cache.get('key1')\n# \u83b7\u53d6\u65f6\u9644\u5e26\u8fc7\u671f\u65f6\u95f4 | Get with expiration time\nvalue2, deadtime2 = cache.get_with_deadtime('key2')\n\n# \u83b7\u53d6\u5e76\u5220\u9664\u4e00\u4e2a\u5143\u7d20 | pop an item\nvalue3 = cache.pop('key3')\nvalue4 = cache.popitem(least_recently=True)\n\n# \u83b7\u53d6\u5143\u7d20ttl(\u5269\u4f59\u5b58\u6d3b\u65f6\u95f4) | Get the ttl(time to live) of an item\nttl = cache.ttl('key0')\n# \u83b7\u53d6\u5143\u7d20ddl(\u8fc7\u671f\u65f6\u95f4) | Get the ddl(deadline) of an item\nddl = cache.ddl('key0')\n\n# \u6309\u7167\u7ed9\u5b9akeys\u521b\u5efa\u4e00\u4e2a\u65b0\u7684ExpiringDict | Create a new ExpiringDict with the given keys\nnew_cache = cache.fromkeys(['key0', 'key1', 'key2'], \"default_value\", 60)\n# \u6309\u7167\u7ed9\u5b9a\u53ef\u6620\u5c04\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u65b0\u7684ExpiringDict | Create a new ExpiringDict with the given mapping\nnew_cache = cache.frommapping({'key0': 'value0', 'key1': ('value1', 1), 'key2': ('value2', timedelta(hours=2))}, 60)\n# \u4ece\u53e6\u4e00\u4e2aExpiringDict\u521b\u5efa\u4e00\u4e2a\u65b0\u7684ExpiringDict | Create a new ExpiringDict from another ExpiringDict\nnew_cache = cache.fromexpiringdict(cache, 60)\n```\n\n## \u6d4b\u8bd5 | Tests\n\n\u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\uff1a | Run the unit tests:\n\n```bash\npytest tests/\n```\n\n## \u81f4\u8c22 | Thanks\n\n[`lru-dict`](https://github.com/amitdev/lru-dict): ExpiringDict \u7684\u5185\u90e8\u5b58\u50a8\u5b9e\u73b0 \n[`expiringdict`](https://github.com/mailgun/expiringdict): \u672c\u9879\u76ee\u7684\u7075\u611f\u6765\u6e90\uff0c\u5728\u6b64\u57fa\u7840\u4e0a\u6dfb\u52a0\u7c7b\u578b\u6ce8\u89e3\u548c\u5176\u4ed6\u529f\u80fd\n\n## \u8bb8\u53ef | License\n\nMIT\n\n---\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "\u63d0\u4f9b\u4e00\u4e2a\u5e26\u6709\u8fc7\u671f\u65f6\u95f4\u7684\u5b57\u5178\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u7f13\u5b58\u548c\u4e34\u65f6\u5b58\u50a8\u3002",
"version": "1.0.1",
"project_urls": {
"Documentation": "https://github.com/AzideCupric/expiringdictx",
"Homepage": "https://github.com/AzideCupric/expiringdictx",
"Repository": "https://github.com/AzideCupric/expiringdictx"
},
"split_keywords": [
"expire",
"dict",
"lru"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f5608ed2da89b8f09687e1b2a5ae7be16051e2f4cfef8a7e6ffae0ed78ac7e68",
"md5": "e12787f0d7ea63eaf0a2cda20ac0f19b",
"sha256": "0b8c66f7781a066bafa842fc1af49594207d5c78dbfd09c7991c03208a37ae49"
},
"downloads": -1,
"filename": "expiringdictx-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e12787f0d7ea63eaf0a2cda20ac0f19b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10,<4.0",
"size": 7274,
"upload_time": "2023-11-10T17:24:17",
"upload_time_iso_8601": "2023-11-10T17:24:17.022220Z",
"url": "https://files.pythonhosted.org/packages/f5/60/8ed2da89b8f09687e1b2a5ae7be16051e2f4cfef8a7e6ffae0ed78ac7e68/expiringdictx-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0b0fd0f6b3d8d550d307eb794e63187059f159abd750fe1d2969fe732c665130",
"md5": "103ccd99d145f114b0a357e63a6b1a10",
"sha256": "4c077cb69774594f85bd181bfec66398971c5df50c737abb2f6c9524b7ac519f"
},
"downloads": -1,
"filename": "expiringdictx-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "103ccd99d145f114b0a357e63a6b1a10",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10,<4.0",
"size": 6775,
"upload_time": "2023-11-10T17:24:17",
"upload_time_iso_8601": "2023-11-10T17:24:17.977756Z",
"url": "https://files.pythonhosted.org/packages/0b/0f/d0f6b3d8d550d307eb794e63187059f159abd750fe1d2969fe732c665130/expiringdictx-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-10 17:24:17",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "AzideCupric",
"github_project": "expiringdictx",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "expiringdictx"
}