Simple Tracker(Redis)
Simple Tracker 是一个基于 Redis 的轻量级计数跟踪系统,支持按分钟、小时、天三种时间粒度对项目进行计数统计。
功能特点
- 支持多种时间粒度跟踪:分钟(m)、小时(h)、天(d)
- 自动过期机制,避免数据无限增长
- 同步和异步两种实现方式
- 提供装饰器模式方便集成
核心组件
ItemTracker (同步版本)
from tracker import ItemTracker
# 初始化
tracker = ItemTracker(redis_client, tracker_name="my_tracker")
# 跟踪项目出现次数
tracker.track_item_occurrence("item_name")
# 获取指定时间粒度的计数
count = tracker.get_item_count("item_name", "h") # 按小时统计
# 获取分布统计
distribution = tracker.get_count_distribution("d") # 按天获取分布
AsyncItemTracker (异步版本)
from tracker import AsyncItemTracker
# 初始化
async_tracker = AsyncItemTracker(redis_client, tracker_name="my_tracker")
# 异步跟踪项目出现次数
await async_tracker.track_item_occurrence("item_name")
# 异步获取指定时间粒度的计数
count = await async_tracker.get_item_count("item_name", "h")
# 异步获取分布统计
distribution = await async_tracker.get_count_distribution("d")
使用装饰器模式
同步装饰器
from tracker import get_tracker
track_item = get_tracker(redis_client)
@track_item("api_call")
def my_function():
pass
异步装饰器
from tracker import get_async_tracker
track_item = get_async_tracker(redis_client)
@track_item("api_call")
async def my_async_function():
pass
数据存储结构
Redis 中的数据以以下格式存储:
{tracker_name}:{granularity}:{time_period} -> Hash {item_name: count}
例如:
my_tracker:h:2023120115 -> {"api_call": 25, "user_login": 10}
过期时间设置
- 分钟级数据保留 60 分钟
- 小时级数据保留 24 小时
- 天级数据保留 14 天
返回数据格式
get_count_distribution 方法返回如下格式的数据:
[
{
"item_name": "api_call",
"total_count": 25,
"freq": "h",
"period": "2023120115"
}
]
Metadata
Release files for simple-tracker-redis 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simple_tracker_redis-0.1.4.tar.gz | 4.2 kB | Without provenance | Details for simple_tracker_redis-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simple_tracker_redis-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for simple_tracker_redis-0.1.4-py3-none-any.whl |
Total release size: 8.8 kB
Release files / simple_tracker_redis-0.1.4.tar.gz
| Download URL | simple_tracker_redis-0.1.4.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
37789bee4491779eae02cabb3e8a8dc2d768683d3c0bd21d55938580590cf7e9
|
|
BLAKE2b-256 checksum How to use checksums |
5ff4a37146db0345681b2bc91afb4492caa7cd6d5d1d7abad4494b5212d5d239
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.14
|
Release files / simple_tracker_redis-0.1.4-py3-none-any.whl
| Download URL | simple_tracker_redis-0.1.4-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1f51bc46542d0e5af375b574d0e6b526852e6d44295a5dfe0059c515a18c2f2d
|
|
BLAKE2b-256 checksum How to use checksums |
08d82e1133898e884541885bf0b14dd93fba3fe2ced4e4ff941ed77c1c170bb8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.14
|
Security for simple-tracker-redis 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.