File size: 2,205 Bytes
86368de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import json
import os
import time
from typing import Optional, Dict, Any


class AnswersCache:
    def __init__(self, filename: str = "cache.json", max_size: int = 1000):
        self.filename = filename
        self.max_size = max_size
        self.cache: Dict[str, Dict[str, Any]] = {}
        self._load_cache()

    def _load_cache(self):
        """Load cache from file if it exists"""
        if os.path.exists(self.filename):
            try:
                with open(self.filename, 'r') as f:
                    self.cache = json.load(f)
            except (json.JSONDecodeError, FileNotFoundError):
                self.cache = {}

    def _save_cache(self):
        """Save cache to file"""
        with open(self.filename, 'w') as f:
            json.dump(self.cache, f, indent=2)

    def set(self, key: str, value: str, ttl: Optional[int] = None):
        """Set a key-value pair with optional TTL (time to live in seconds)"""
        if len(self.cache) >= self.max_size:
            self._evict_oldest()

        cache_entry = {
            'value': value,
            'created_at': time.time(),
            'ttl': ttl
        }
        self.cache[key] = cache_entry
        self._save_cache()

    def get(self, key: str) -> Optional[str]:
        """Get value for key, returns None if not found or expired"""
        if key not in self.cache:
            return None

        entry = self.cache[key]

        # Check if entry has expired
        if entry['ttl'] is not None:
            if time.time() - entry['created_at'] > entry['ttl']:
                self.delete(key)
                return None

        return entry['value']

    def delete(self, key: str):
        """Delete a key from cache"""
        if key in self.cache:
            del self.cache[key]
            self._save_cache()

    def clear(self):
        """Clear all cache entries"""
        self.cache = {}
        self._save_cache()

    def _evict_oldest(self):
        """Remove the oldest entry when cache is full"""
        if not self.cache:
            return

        oldest_key = min(self.cache.keys(),
                         key=lambda k: self.cache[k]['created_at'])
        del self.cache[oldest_key]