Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:24mlight A Share investment Agent SQLite Caching Strategy

From Leeroopedia
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:

  1. 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.
  1. Upsert Semantics: Uses SQLite INSERT ... ON CONFLICT DO UPDATE for insert-or-update behavior. Key columns serve as the conflict target.
  1. TTL Support: fetch_records accepts ttl_seconds to filter out stale records by comparing the 缓存时间 (cache time) column.
  1. 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.
  1. Data Normalization: Handles pandas Timestamps, numpy types, and NaN values automatically during insertion.
  1. Automatic Cleanup: Registers atexit handler 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