File size: 3,325 Bytes
d1c3b42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Simple in-memory cache for Gemini API responses.
Reduces API calls and costs by caching compositions based on image hash.
"""
import hashlib
import time
from typing import Optional, Dict, Any
from PIL import Image
import io


class CompositionCache:
    """In-memory cache for AI-generated compositions."""
    
    def __init__(self, max_size: int = 100, ttl_seconds: int = 3600):
        """
        Initialize the cache.
        
        Args:
            max_size: Maximum number of cached items
            ttl_seconds: Time-to-live for cache entries (default: 1 hour)
        """
        self.cache: Dict[str, Dict[str, Any]] = {}
        self.max_size = max_size
        self.ttl_seconds = ttl_seconds
    
    def _get_image_hash(self, image: Image.Image) -> str:
        """Generate a hash from an image for cache key."""
        # Convert image to bytes
        img_byte_arr = io.BytesIO()
        image.save(img_byte_arr, format='PNG')
        img_bytes = img_byte_arr.getvalue()
        
        # Generate SHA256 hash
        return hashlib.sha256(img_bytes).hexdigest()
    
    def get(self, image: Image.Image, audio_path: Optional[str] = None) -> Optional[Dict[str, Any]]:
        """
        Retrieve a cached composition.
        
        Args:
            image: PIL Image object
            audio_path: Optional path to audio file
            
        Returns:
            Cached composition data or None if not found/expired
        """
        cache_key = self._get_image_hash(image)
        if audio_path:
            cache_key += f"_audio_{hashlib.md5(audio_path.encode()).hexdigest()}"
        
        if cache_key in self.cache:
            entry = self.cache[cache_key]
            
            # Check if expired
            if time.time() - entry['timestamp'] > self.ttl_seconds:
                del self.cache[cache_key]
                return None
            
            return entry['data']
        
        return None
    
    def set(self, image: Image.Image, composition: Dict[str, Any], audio_path: Optional[str] = None):
        """
        Store a composition in the cache.
        
        Args:
            image: PIL Image object
            composition: Composition data to cache
            audio_path: Optional path to audio file
        """
        cache_key = self._get_image_hash(image)
        if audio_path:
            cache_key += f"_audio_{hashlib.md5(audio_path.encode()).hexdigest()}"
        
        # Implement LRU eviction if cache is full
        if len(self.cache) >= self.max_size:
            # Remove oldest entry
            oldest_key = min(self.cache.keys(), key=lambda k: self.cache[k]['timestamp'])
            del self.cache[oldest_key]
        
        self.cache[cache_key] = {
            'data': composition,
            'timestamp': time.time()
        }
    
    def clear(self):
        """Clear all cached entries."""
        self.cache.clear()
    
    def get_stats(self) -> Dict[str, Any]:
        """Get cache statistics."""
        return {
            'size': len(self.cache),
            'max_size': self.max_size,
            'ttl_seconds': self.ttl_seconds,
            'oldest_entry_age': min(
                [time.time() - entry['timestamp'] for entry in self.cache.values()],
                default=0
            )
        }