一个与tushare Pro API完全兼容的高性能缓存系统,可以无缝替换tushare,提供数据缓存和加速功能。
- 完全兼容tushare Pro API - 所有方法调用方式与tushare完全一致
- 智能缓存系统 - 自动缓存数据,减少API调用,提高响应速度
- 多级缓存 - 内存缓存 + 磁盘缓存 + SQLite数据库
- 数据压缩 - 使用gzip压缩,节省存储空间
- 自动回退 - 未实现的方法自动调用tushare API
- 性能监控 - 提供详细的性能报告和缓存命中率统计
- 模块化设计 - 采用混入类(Mixin)架构,易于扩展和维护
pip install tushare-cache-systemgit clone https://github.com/yourusername/tushare-cache-system.git
cd tushare-cache-system
pip install -e .原tushare代码:
import tushare as ts
ts.set_token('your_token')
pro = ts.pro_api()替换为tushare_cache_system:
import tushare_cache_system as ts
pro = ts.pro_api('your_token')所有方法调用与tushare完全一致:
# 获取股票基本信息
stock_basic = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
# 获取日线数据
daily_data = pro.daily(ts_code='000001.SZ', start_date='20230101', end_date='20230105')
# 获取指数权重
index_weight = pro.index_weight(index_code='000300.SH', start_date='20230101', end_date='20230131')创建.env文件:
TUSHARE_TOKEN=your_tushare_token
DATA_ROOT=e:/code/optimized_quant_data/
CACHE_SIZE=1000
ENABLE_COMPRESSION=true
MAX_WORKERS=4然后在代码中使用:
from tushare_cache_system import TushareCacheSystem
import os
from dotenv import load_dotenv
# 加载环境变量
load_dotenv()
# 从环境变量初始化
system = TushareCacheSystem(
token=os.getenv("TUSHARE_TOKEN"),
data_root=os.getenv("DATA_ROOT", "e:/code/optimized_quant_data/"),
cache_size=int(os.getenv("CACHE_SIZE", 1000)),
enable_compression=os.getenv("ENABLE_COMPRESSION", "true").lower() == "true",
max_workers=int(os.getenv("MAX_WORKERS", 4))
)get_stock_daily()- 股票日线数据get_stock_weekly()- 股票周线数据get_stock_monthly()- 股票月线数据
get_index_daily()- 指数日线数据get_index_weekly()- 指数周线数据get_index_monthly()- 指数月线数据
get_income()- 利润表数据get_balancesheet()- 资产负债表数据get_cashflow()- 现金流量表数据get_forecast()- 业绩预告数据
get_market_basic()- 市场基本信息get_market_daily_basic()- 市场日行情基本信息
get_fund_basic()- 基金基本信息get_fund_daily()- 基金日行情数据
get_future_daily()- 期货日线数据
get_news()- 新闻数据get_announcements()- 公告数据
dividend()- 分红送股数据stock_company()- 上市公司基本信息stock_ipo()- 新股上市数据stock_suspended()- 停复牌信息margin()- 融资融券数据concept()- 概念分类concept_detail()- 概念股票列表stk_factor()- 股票因子数据daily_basic()- 每日指标数据
- 内存缓存: 最快访问,适合频繁使用的小数据集
- SQLite缓存: 中等速度,适合中等规模数据集
- 文件缓存: 较慢访问,适合大规模数据集
# 查看缓存统计
stats = system.get_cache_stats()
print(stats)
# 清理特定缓存
system.clear_cache('memory') # 清理内存缓存
system.clear_cache('sqlite') # 清理SQLite缓存
system.clear_cache('file') # 清理文件缓存
system.clear_cache('all') # 清理所有缓存
# 保存缓存到磁盘
system.save_cache()
# 预加载常用数据
system.preload_common_data()# 设置最大工作线程数
system = TushareCacheSystem(
token="your_token",
max_workers=8 # 根据CPU核心数调整
)# 启用数据压缩(减少存储空间但增加CPU开销)
system = TushareCacheSystem(
token="your_token",
enable_compression=True
)# 调整内存缓存大小(条目数)
system = TushareCacheSystem(
token="your_token",
cache_size=2000 # 默认1000
)from tushare_cache_system import TushareDataCacheCore
from tushare_cache_system.stock_data import StockDataMixin
class CustomDataSource(TushareDataCacheCore, StockDataMixin):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# 自定义初始化代码
def get_custom_data(self, symbol, start_date, end_date):
# 自定义数据获取逻辑
pass
# 使用自定义数据源
custom_system = CustomDataSource(token="your_token")
data = custom_system.get_stock_daily("000001.SZ", "20230101", "20231231")# 获取性能报告
report = system.get_performance_report()
print(f"API调用次数: {report['api_calls']}")
print(f"缓存命中率: {report['cache_hit_rate']}")
print(f"平均响应时间: {report['avg_response_time']}ms")运行测试套件:
# 安装开发依赖
pip install -e ".[dev]"
# 运行测试
pytest
# 运行测试并生成覆盖率报告
pytest --cov=tushare_cache_system --cov-report=html确保您有有效的Tushare token,并在初始化时正确设置:
# 从环境变量获取
import os
token = os.getenv("TUSHARE_TOKEN")
if not token:
raise ValueError("请设置TUSHARE_TOKEN环境变量")
system = TushareCacheSystem(token=token)确保数据目录有读写权限:
import os
data_dir = "e:/code/optimized_quant_data/"
os.makedirs(data_dir, exist_ok=True)
# 检查权限
if not os.access(data_dir, os.W_OK):
raise PermissionError(f"无法写入数据目录: {data_dir}")如果遇到内存不足问题,可以:
- 减少缓存大小:
cache_size=500 - 启用数据压缩:
enable_compression=True - 定期清理缓存:
system.clear_cache('memory')
欢迎贡献代码!请遵循以下步骤:
- Fork 本仓库
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 开启 Pull Request
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。
- 初始版本发布
- 实现核心缓存系统
- 支持股票、指数、财务等数据类型
- 添加性能监控和统计功能