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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""
DataBus Cache Layer β€” Three-Tier Cache with SWR + Per-Type Stats
================================================================

L1: In-memory dict (sub-millisecond, 4096 keys, LRU eviction) + SWR stale buffer
L2: Redis (sub-millisecond, shared across processes, TTL-managed)
L3: Cloudflare R2 cold storage (RAG permanence, nightly snapshots)

Stale-While-Revalidate (SWR):
  - L1 stores both fresh and stale entries (stale = TTL * 2)
  - On cache read, if entry is fresh β†’ direct hit
  - If entry is stale (past TTL but within stale window) β†’ return stale data
    AND flag for background refresh via cache.stale_refresh_callback
  - User NEVER waits for a refresh β€” always gets instant data

Per-Type Stats:
  - Tracks hits/misses per data_type for tuning TTLs
  - health() flags types with hit_rate < 30% as "increase TTL"
"""

import asyncio
import contextlib
import hashlib
import json
import logging
import os
import time
from collections import OrderedDict, defaultdict
from collections.abc import Callable
from typing import Any

from dotenv import load_dotenv

load_dotenv("/app/.env", override=True)

logger = logging.getLogger("databus.cache")

REDIS_HOST = os.getenv("REDIS_HOST", "rmi-redis")
REDIS_PORT = int(os.getenv("REDIS_PORT", "6379"))
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD", "")
REDIS_DB = int(os.getenv("REDIS_DB", "0"))


class L1Cache:
    """In-memory LRU cache with Stale-While-Revalidate support.

    Entries are stored with TWO expiry windows:
      - fresh_expiry: data is fresh, return immediately (normal hit)
      - stale_expiry: data is stale but usable (SWR hit)
    Stale entries are returned instantly; the caller triggers background refresh.
    """

    def __init__(self, max_keys: int = 4096, stale_multiplier: float = 2.5):
        self._max = max_keys
        self._stale_mult = stale_multiplier
        self._store: OrderedDict[str, tuple[Any, float, float]] = OrderedDict()
        # (value, fresh_expiry, stale_expiry)
        self.hits = 0
        self.stale_hits = 0
        self.misses = 0
        self._lock = asyncio.Lock()

    async def get(self, key: str) -> tuple[Any | None, bool]:
        """Returns (value, is_stale). is_stale=True means background refresh needed."""
        async with self._lock:
            entry = self._store.get(key)
            if entry is None:
                self.misses += 1
                return None, False
            value, fresh_expiry, stale_expiry = entry
            now = time.monotonic()
            if now > stale_expiry:
                # Fully expired β€” evict
                del self._store[key]
                self.misses += 1
                return None, False
            if now > fresh_expiry:
                # Stale but usable β€” SWR hit
                self._store.move_to_end(key)
                self.stale_hits += 1
                return value, True
            # Fresh hit
            self._store.move_to_end(key)
            self.hits += 1
            return value, False

    async def set(self, key: str, value: Any, ttl: int):
        async with self._lock:
            now = time.monotonic()
            fresh = now + ttl
            stale = now + int(ttl * self._stale_mult)
            self._store[key] = (value, fresh, stale)
            self._store.move_to_end(key)
            while len(self._store) > self._max:
                self._store.popitem(last=False)

    async def delete(self, key: str):
        async with self._lock:
            self._store.pop(key, None)

    async def clear(self):
        async with self._lock:
            self._store.clear()

    def stats(self) -> dict:
        total = self.hits + self.stale_hits + self.misses
        return {
            "keys": len(self._store),
            "max_keys": self._max,
            "hits": self.hits,
            "stale_hits": self.stale_hits,
            "misses": self.misses,
            "hit_rate": round((self.hits + self.stale_hits) / total * 100, 1) if total > 0 else 0,
            "fresh_hit_rate": round(self.hits / total * 100, 1) if total > 0 else 0,
        }


class L2RedisCache:
    """Redis cache. Shared across processes. TTL-managed automatically."""

    def __init__(self):
        self._redis = None
        self._available = False
        self._prefix = "databus:"
        self.hits = 0
        self.misses = 0

    async def _connect(self):
        if self._redis and self._available:
            return True
        try:
            import redis.asyncio as aioredis

            kwargs = {
                "host": REDIS_HOST,
                "port": REDIS_PORT,
                "db": REDIS_DB,
                "socket_connect_timeout": 2,
                "socket_timeout": 2,
                "decode_responses": True,
                "protocol": 2,  # Redis 7.2 compat β€” avoid HELLO/AUTH handshake issue
            }
            if REDIS_PASSWORD:
                kwargs["password"] = REDIS_PASSWORD
            self._redis = aioredis.Redis(**kwargs)
            await self._redis.ping()
            self._available = True
            logger.info("DataBus Cache: Redis connected")
            return True
        except Exception as e:
            logger.warning(f"DataBus Cache: Redis unavailable ({e}), L2 disabled")
            self._available = False
            return False

    async def get(self, key: str) -> Any | None:
        if not self._available and not await self._connect():
            self.misses += 1
            return None
        try:
            raw = await self._redis.get(f"{self._prefix}{key}")
            if raw:
                self.hits += 1
                return json.loads(raw)
            self.misses += 1
            return None
        except Exception:
            self._available = False
            self.misses += 1
            return None

    async def set(self, key: str, value: Any, ttl: int):
        if not self._available and not await self._connect():
            return
        try:
            await self._redis.setex(f"{self._prefix}{key}", ttl, json.dumps(value, default=str))
        except Exception:
            self._available = False

    async def delete(self, key: str):
        if not self._available:
            return
        with contextlib.suppress(Exception):
            await self._redis.delete(f"{self._prefix}{key}")

    async def clear(self):
        if not self._available:
            return
        try:
            async for key in self._redis.scan_iter(f"{self._prefix}*"):
                await self._redis.delete(key)
        except Exception:
            pass

    def stats(self) -> dict:
        total = self.hits + self.misses
        return {
            "available": self._available,
            "hits": self.hits,
            "misses": self.misses,
            "hit_rate": round(self.hits / total * 100, 1) if total > 0 else 0,
        }


class CacheLayer:
    """
    Three-tier cache with Stale-While-Revalidate + per-type stats.

    L1 (memory, SWR) β†’ L2 (Redis) β†’ L3 (R2, async, background)

    Read path: L1 (fresh? β†’ done. stale? β†’ return stale + schedule refresh) β†’ L2 β†’ miss
    Write path: External API β†’ L1 + L2 (L3 batched via RAG permanence cron)

    SWR callback: When L1 returns stale data, cache fires stale_refresh_callback
    so the caller can schedule background re-fetch without blocking the user.
    """

    def __init__(self):
        self.l1 = L1Cache(max_keys=4096)
        self.l2 = L2RedisCache()
        self._l3_enabled = True
        # SWR: caller sets this to a coroutine factory for background refresh
        self.stale_refresh_callback: Callable | None = None
        # Per-type hit/miss tracking for TTL tuning
        self._type_stats: dict[str, dict[str, int]] = defaultdict(lambda: {"hits": 0, "stale_hits": 0, "misses": 0})
        # Data type TTLs β€” optimized for FREE tier usage to avoid rate limits
        # and maximize our 1-month Arkham trial + Alchemy free quota.
        self.ttl_config = {
            # ── High Frequency (Cache aggressively to save free API calls) ──
            "token_price": 60,  # Increased from 15s to 60s (better cache reuse for price data)
            "market_overview": 60,  # Increased from 30s to 60s
            "trending": 120,  # Increased from 60s to 120s
            "market_movers": 60,  # Increased from 30s to 60s
            "alerts": 30,  # Increased from 15s to 30s
            # ── Medium Frequency ──
            "token_detail": 60,  # Increased from 30s
            "token_meta": 300,  # Increased from 120s (Alchemy free tier)
            "wallet_balance": 30,  # Increased from 10s (Alchemy free tier)
            "wallet_tokens": 300,  # Increased from 60s to 300s (reduce misses on uncached provider)
            "wallet_pnl": 120,  # Increased from 60s
            "tx_history": 60,  # Increased from 30s
            "dex_data": 60,  # Increased from 30s
            "holder_data": 120,  # Increased from 60s
            # ── Low Frequency (Static or slow-moving data) ──
            "risk_scan": 600,  # Increased from 300s
            "sentinel_deep": 600,  # Increased from 300s
            "funding_source": 7200,  # Increased from 3600s (2 hours)
            "solana_funding": 7200,  # Increased from 3600s (2 hours)
            "wallet_labels": 86400,  # 24 hours (local data, no API cost)
            "entity_intel": 3600,  # Increased from 1800s (1 hour)
            "socialfi_resolve": 86400,  # 24 hours
            "cross_chain": 3600,  # Increased from 1800s
            "wallet_cluster": 3600,  # Increased from 1800s
            "bundle_detect": 600,  # Increased from 300s
            # ── ARKHAM INTELLIGENCE (1-Month Free Trial: 100k quota) ──
            # Cache aggressively to maximize the 100,000 monthly request limit
            "arkham_entity": 600,  # Increased from 300s (10 mins)
            "arkham_portfolio": 300,  # Increased from 120s (5 mins)
            "arkham_labels": 7200,  # Increased from 3600s (2 hours)
            "arkham_transfers": 300,  # Increased from 60s (5 mins)
            "arkham_counterparties": 600,  # Increased from 300s (10 mins)
            # ── Other ──
            "nansen_labels": 3600,
            "nansen_smart_money": 1800,
            "news": 600,  # Increased from 300s (10 mins)
            "news_intel": 600,  # 10 mins for aggregated news
            "messari_news": 900,  # 15 mins for Messari institutional feed
            "social_feed": 300,  # Increased from 120s (5 mins)
            "sentiment": 600,  # Increased from 300s
            "whale_data": 300,  # Increased from 120s
            "smart_money": 300,  # Increased from 120s
            "gmgn_smart_money": 300,  # Increased from 120s
            "launches": 120,  # Increased from 60s
            "bubble_map": 600,  # Increased from 300s
            "rugmaps_analysis": 1200,  # Increased from 600s (20 mins)
            "contract_scan": 3600,  # Increased from 1800s (1 hour)
            "threat_check": 600,  # Increased from 300s
            "prediction_markets": 120,  # Increased from 60s
            "prediction_signals": 300,  # Increased from 120s
            "defi_protocols": 600,  # Increased from 300s
            "rag_search": 600,  # Increased from 300s
            "tvl": 300,  # Increased from 120s
            "wallet_profile": 600,  # Increased from 300s
            "portfolio": 120,  # Increased from 60s
            # ── NEW FREE SOURCES ADDED ──
            "defillama_tvl": 3600,  # 1 hour (completely free, no API key)
            "defillama_chains": 3600,  # 1 hour (completely free, no API key)
            "blockchair_address": 600,  # 10 mins (free tier, no API key)
            "blockchair_stats": 1800,  # 30 mins (free tier, no API key)
            "birdeye_overview": 120,  # 2 mins (freemium, 50k/mo quota)
            "birdeye_price": 30,  # 30s (freemium, 50k/mo quota, fallback to DexScreener)
            "solana_tracker_price": 15,  # 15s (freemium, 5k/mo quota)
            "solana_tracker_token": 60,  # 1 min (freemium, 5k/mo quota)
            "solana_tracker_trending": 120,  # 2 mins (freemium, 5k/mo quota)
            "dev_activity": 3600,  # 1 hour (Santiment free tier, 100 calls/day)
            "url_security_scan": 86400,  # 24 hours (VirusTotal free tier, 500 req/day)
            "dune_early_buyers": 14400,  # 4 hours (Dune free tier: 10k CU/mo, aggressive caching)
            "default": 60,
        }

    def _extract_data_type(self, key: str) -> str:
        """Extract data_type from cache key format 'source:data_type:hash'."""
        parts = key.split(":")
        if len(parts) >= 2:
            return parts[1]
        return "default"

    async def get(self, key: str, data_type: str = "default") -> tuple[Any | None, bool]:
        """Get from cache with SWR. Returns (value, is_stale).
        If is_stale=True, caller should schedule background refresh.
        """
        # L1 (with SWR)
        val, is_stale = await self.l1.get(key)
        if val is not None:
            dtype = data_type or self._extract_data_type(key)
            if is_stale:
                self._type_stats[dtype]["stale_hits"] += 1
            else:
                self._type_stats[dtype]["hits"] += 1
            # SWR: schedule background refresh if stale and callback is set
            if is_stale and self.stale_refresh_callback:
                try:
                    asyncio.create_task(self.stale_refresh_callback(key))
                except Exception:
                    pass  # Best-effort refresh
            return val, is_stale
        # L2 (no SWR β€” Redis handles its own TTL)
        val = await self.l2.get(key)
        if val is not None:
            # Promote to L1 with shorter TTL (fresh only for now)
            await self.l1.set(key, val, 60)
            dtype = data_type or self._extract_data_type(key)
            self._type_stats[dtype]["hits"] += 1
            return val, False
        dtype = data_type or self._extract_data_type(key)
        self._type_stats[dtype]["misses"] += 1
        return None, False

    async def set(self, key: str, value: Any, ttl: int | None = None, data_type: str = "default"):
        """Set in cache. Writes to L1 + L2."""
        if ttl is None:
            ttl = self.ttl_config.get(data_type, 60)
        await self.l1.set(key, value, ttl)
        await self.l2.set(key, value, ttl)

    async def delete(self, key: str):
        await self.l1.delete(key)
        await self.l2.delete(key)

    async def clear(self):
        await self.l1.clear()
        await self.l2.clear()

    def make_key(self, source: str, data_type: str, **kwargs) -> str:
        """Generate a deterministic cache key."""
        args_str = json.dumps(kwargs, sort_keys=True, default=str)
        args_hash = hashlib.sha256(args_str.encode()).hexdigest()[:16]
        return f"{source}:{data_type}:{args_hash}"

    def type_stats(self) -> dict[str, dict]:
        """Per-data-type hit/miss/stale stats with TTL tuning suggestions."""
        result = {}
        for dtype, counts in self._type_stats.items():
            total = counts["hits"] + counts["stale_hits"] + counts["misses"]
            hit_rate = round((counts["hits"] + counts["stale_hits"]) / total * 100, 1) if total > 0 else 0
            entry = {
                "hits": counts["hits"],
                "stale_hits": counts["stale_hits"],
                "misses": counts["misses"],
                "hit_rate": hit_rate,
                "current_ttl": self.ttl_config.get(dtype, 60),
            }
            # Suggest TTL increase if hit_rate < 30%
            if total > 10 and hit_rate < 30:
                entry["suggestion"] = "increase_ttl"
            # Suggest TTL decrease if hit_rate > 95% and TTL > 120
            elif total > 10 and hit_rate > 95 and self.ttl_config.get(dtype, 60) > 120:
                entry["suggestion"] = "decrease_ttl"
            result[dtype] = entry
        return result

    async def health(self) -> dict:
        l1 = self.l1.stats()
        l2 = self.l2.stats()
        total_hits = l1["hits"] + l1["stale_hits"] + l2["hits"]
        total_misses = l1["misses"] + l2["misses"]
        total = total_hits + total_misses
        return {
            "status": "ok",
            "l1_memory": l1,
            "l2_redis": l2,
            "l3_r2": {"enabled": self._l3_enabled, "note": "Batched via RAG permanence cron"},
            "combined_hit_rate": round(total_hits / total * 100, 1) if total > 0 else 0,
            "total_hits": total_hits,
            "total_misses": total_misses,
            "per_type_stats": self.type_stats(),
            "ttl_config": self.ttl_config,
        }


# ── Singleton ─────────────────────────────────────────────────────────────────

_cache: CacheLayer | None = None


def get_cache() -> CacheLayer:
    global _cache
    if _cache is None:
        _cache = CacheLayer()
    return _cache