solana-proxy/cache.py
afd afa26d0e29 This is a bunch of untested AI slop, first pass.
Implement Solana RPC proxy with automatic failover and caching

- Add multi-provider support for 5 free Solana RPC endpoints (Alchemy, PublicNode, Helius, QuickNode, Solana Public)
- Implement automatic failover with 30-minute backoff for failed providers
- Add disk-based response caching with 100GB LRU eviction
- Create SQLite error logging with UUID tracking
- Support both HTTP JSON-RPC and WebSocket connections
- Include provider-specific authentication handling
- Add response normalization for consistent output
- Write end-to-end tests for core functionality

The proxy provides a unified endpoint that automatically routes requests to available providers, caches responses to reduce load, and logs all errors with retrievable UUIDs for debugging.
2025-07-15 22:10:06 -04:00

37 lines
1.2 KiB
Python

import json
import os
from typing import Dict, Any, Optional
import diskcache
class Cache:
def __init__(self, cache_dir: str = "./cache", size_limit_gb: int = 100):
self.cache_dir = cache_dir
self.size_limit_bytes = size_limit_gb * 1024 * 1024 * 1024
self.cache = diskcache.Cache(
directory=cache_dir,
size_limit=self.size_limit_bytes,
eviction_policy='least-recently-used'
)
def _make_key(self, method: str, params: Dict[str, Any]) -> str:
return f"{method}:{json.dumps(params, sort_keys=True)}"
def get(self, method: str, params: Dict[str, Any]) -> Optional[Dict[str, Any]]:
key = self._make_key(method, params)
return self.cache.get(key)
def set(self, method: str, params: Dict[str, Any], response: Dict[str, Any]) -> None:
key = self._make_key(method, params)
self.cache.set(key, response)
def size_check(self) -> Dict[str, Any]:
stats = self.cache.stats()
return {
"size_bytes": stats[1],
"size_gb": stats[1] / (1024 * 1024 * 1024),
"count": stats[0],
"limit_gb": self.size_limit_bytes / (1024 * 1024 * 1024)
}