Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

2.5 KB
Raw Blame
"""
Pricing Cache System

Caches pricing data to avoid repeated API calls and improve performance.
Uses bounded TTLCache to prevent memory leaks.
"""

from typing import Any, Dict, Optional

from cachetools import TTLCache  # type: ignore[import-untyped]
from loguru import logger


class PricingCache:
    """Cache for LLM pricing data."""

    def __init__(self, cache_dir: Optional[str] = None, cache_ttl: int = 3600):
        """
        Initialize pricing cache.

        Args:
            cache_dir: Directory to store cache files (DEPRECATED - no longer used)
            cache_ttl: Cache time-to-live in seconds (default: 1 hour)
        """
        self.cache_ttl = cache_ttl
        # Bounded TTLCache to prevent memory leaks
        self._cache: TTLCache = TTLCache(maxsize=500, ttl=cache_ttl)
        logger.info("PricingCache initialized with bounded TTLCache")

    def get(self, key: str) -> Optional[Any]:
        """Get cached pricing data. TTLCache handles expiration automatically."""
        return self._cache.get(key)

    def set(self, key: str, data: Any):
        """Set cached pricing data."""
        self._cache[key] = data

    def get_model_pricing(self, model_name: str) -> Optional[Dict[str, float]]:
        """Get cached pricing for a specific model."""
        return self.get(f"model:{model_name}")

    def set_model_pricing(self, model_name: str, pricing: Dict[str, float]):
        """Cache pricing for a specific model."""
        self.set(f"model:{model_name}", pricing)

    def get_all_pricing(self) -> Optional[Dict[str, Dict[str, float]]]:
        """Get cached pricing for all models."""
        return self.get("all_models")

    def set_all_pricing(self, pricing: Dict[str, Dict[str, float]]):
        """Cache pricing for all models."""
        self.set("all_models", pricing)

    def clear(self):
        """Clear all cached data."""
        self._cache.clear()
        logger.info("Pricing cache cleared")

    def clear_expired(self):
        """Remove expired cache entries. TTLCache handles this automatically via expire()."""
        self._cache.expire()
        logger.debug("Expired cache entries cleared")

    def get_cache_stats(self) -> Dict[str, Any]:
        """Get cache statistics."""
        # TTLCache automatically evicts expired entries on access
        self._cache.expire()
        return {
            "total_entries": len(self._cache),
            "max_entries": self._cache.maxsize,
            "cache_type": "TTLCache",
            "cache_ttl": self.cache_ttl,
        }