Principle:24mlight A Share investment Agent SQLite Caching Strategy
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, Caching |
| Last Updated | 2026-10-10 14:00 GMT |
Overview
A generic SQLite-based caching strategy with auto-schema creation, TTL support, upsert semantics, and thread-safe per-thread connections — used throughout the system for financial data, news, and LLM result caching.
Description
The AkshareSQLiteCache provides a lightweight but robust caching layer:
- Auto-Schema Creation: Tables are created lazily from the first inserted record. Column types are inferred from Python types (int → INTEGER, float → REAL, others → TEXT). Missing columns are added on-the-fly via ALTER TABLE.
- Upsert Semantics: Uses SQLite
INSERT ... ON CONFLICT DO UPDATEfor insert-or-update behavior. Key columns serve as the conflict target.
- TTL Support:
fetch_recordsacceptsttl_secondsto filter out stale records by comparing the缓存时间(cache time) column.
- Thread Safety: Uses
threading.local()for per-thread SQLite connections, avoiding cross-thread connection sharing. WAL (Write-Ahead Logging) journal mode enables concurrent reads and writes.
- Data Normalization: Handles pandas Timestamps, numpy types, and NaN values automatically during insertion.
- Automatic Cleanup: Registers
atexithandler to close connections on process exit.
Usage
Use this principle when:
- Caching API responses (financial data, news, LLM results) with TTL-based expiry
- Requiring thread-safe concurrent access to a shared cache from multiple agents
- Building a schema-flexible cache that adapts to changing data structures
Theoretical Basis
# Abstract caching pattern (not real implementation)
class SQLiteCache:
def upsert(self, table, records, key_columns):
# 1. Infer schema from first record
# 2. CREATE TABLE IF NOT EXISTS (with PRIMARY KEY)
# 3. INSERT ... ON CONFLICT(key) DO UPDATE SET ...
def fetch(self, table, filters, ttl_seconds, limit):
# 1. Check if table exists
# 2. Build WHERE clause from filters + TTL
# 3. SELECT * FROM table WHERE ... LIMIT ...
Cache TTL tiers used in the system:
- Real-time data: ~10 minutes
- Financial metrics: 24 hours
- Price history: permanent (no TTL)
- News: daily (keyed by symbol + date)
- LLM results: daily (keyed by cache_key + date)
Related Pages
Implemented By
Uses Heuristics
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment