Install
openclaw skills install openclaw-smart-cache为 OpenClaw 提供高速内存+磁盘双层智能缓存,自动学习常用请求模式并推荐最佳工具方案。
openclaw skills install openclaw-smart-cache为 OpenClaw 设计的智能缓存系统,类似"人类小脑",能够:
cd C:\Users\Administrator\.openclaw\workspace\skills
git clone https://gitee.com/your-repo/smart-cache.git
下载本仓库到 skills/smart-cache/ 目录
from skills.smart_cache.smart_cache import cache
# 设置缓存(5 分钟过期)
cache.set('stock:600519:price', {'price': 1413.64}, ttl=300)
# 获取缓存
data = cache.get('stock:600519:price')
if data:
print(f"缓存命中:{data}")
from skills.smart_cache.smart_cache import cached
@cached(ttl=60, key_prefix='db')
def get_db_stats():
# 耗时操作...
return result
# 首次执行函数,60 秒内返回缓存
result = get_db_stats()
from skills.smart_cache.request_learner import learner
# 记录请求(自动学习)
learner.record_request(
query="查询贵州茅台最新价",
tool="mx_query_v2",
duration=0.01,
success=True
)
# 获取推荐
recs = learner.get_recommendations()
for rec in recs:
print(f"{rec['query_pattern']}: {rec['count']}次,最佳工具:{rec['best_tool']}")
# 查看缓存统计
python skills/smart-cache/cache_manager.py stats
# 查看学习推荐
python skills/smart-cache/learner_manager.py recs
# 获取最佳工具
python skills/smart-cache/learner_manager.py best "查询贵州茅台最新价"
# 清空缓存
python skills/smart-cache/cache_manager.py clear
| 文件 | 说明 | 大小 |
|---|---|---|
smart_cache.py | 缓存核心(内存 + 磁盘双层) | 10KB |
request_learner.py | 请求学习器(自动识别常用请求) | 10KB |
cache_manager.py | 缓存管理 CLI | 3KB |
learner_manager.py | 学习器管理 CLI | 3KB |
| 操作 | 无缓存 | 有缓存 | 加速比 |
|---|---|---|---|
| 读取(命中) | 2-3 秒 | 0.01ms | 200,000x |
| 读取(未命中) | 2-3 秒 | 2-3 秒 | 1x |
| 写入 | - | 10ms/条 | - |
| 类型 | TTL | 说明 |
|---|---|---|
stock:price | 5 分钟 | 股票价格 |
stock:info | 1 小时 | 股票信息 |
db:stats | 1 分钟 | 数据库统计 |
query:result | 10 分钟 | 查询结果 |
stock:list | 1 小时 | 股票列表 |
| 指标 | 阈值 | 效果 |
|---|---|---|
| 重复次数 | ≥3 次 | 标记为"常用请求" |
| 成功率 | >80% | 推荐该工具 |
| 响应时间 | 最快 | 记录为最佳方案 |
from skills.smart_cache.smart_cache import cache
from skills.smart_cache.request_learner import learner
import time
def query_stock_price_cached(stock_code: str):
"""查询股票最新价(带缓存和学习)"""
start = time.time()
cache_key = f'stock:price:{stock_code}'
# 尝试缓存
cached = cache.get(cache_key)
if cached:
learner.record_request(
query=f"查询{stock_code}最新价",
tool="mx_query_v2(cache)",
duration=time.time() - start,
success=True
)
return cached
# API 查询
result = query_mx_data(f"{stock_code}最新价")
# 缓存并记录
cache.set(cache_key, result, ttl=300)
learner.record_request(
query=f"查询{stock_code}最新价",
tool="mx_query_v2(api)",
duration=time.time() - start,
success=True
)
return result
smart-cache/
├── SKILL.md # Skill 说明(本文件)
├── README.md # 详细文档
├── smart_cache.py # 缓存核心
├── request_learner.py # 请求学习器
├── cache_manager.py # 缓存管理 CLI
├── learner_manager.py # 学习器管理 CLI
├── examples/ # 示例代码
│ └── mx_query_v2.py # 妙想查询集成示例
├── cache/ # 缓存数据目录(运行时创建)
│ ├── cache.db # 磁盘缓存(SQLite)
│ ├── patterns.json # 请求模式
│ └── request_log.jsonl # 请求日志
└── requirements.txt # Python 依赖
# 缓存管理
python cache_manager.py stats # 查看统计
python cache_manager.py clear # 清空缓存
python cache_manager.py test # 性能测试
# 学习器管理
python learner_manager.py stats # 查看统计
python learner_manager.py recs # 查看推荐
python learner_manager.py report # 导出报告
python learner_manager.py clear # 清空数据
python cache_manager.py clearfrom smart_cache import cache
# 设置自定义 TTL
cache.set('custom:key', data, ttl=3600) # 1 小时
# 只写内存(不写磁盘)
cache.memory.set('temp:key', data, ttl=60)
from request_learner import learner
# 记录请求
learner.record_request(
query="自定义查询",
tool="my_tool",
duration=1.5,
success=True,
metadata={'custom': 'data'}
)
MIT License
OpenClaw Community