File size: 8,506 Bytes
ac517af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
"""
Cache Management Module

Provides intelligent caching for expensive computations:
- Function result caching
- DataFrame caching with hash-based keys
- Automatic cache invalidation
- Memory-aware cache limits
"""

import hashlib
import pickle
import functools
import logging
from typing import Callable, Any, Dict, Optional, Union
from pathlib import Path
import pandas as pd
import numpy as np
from datetime import datetime, timedelta

logger = logging.getLogger(__name__)

class CacheManager:
    """Intelligent cache manager with memory limits"""
    
    def __init__(self, max_memory_mb: int = 500, cache_dir: Optional[Path] = None):
        self.max_memory_mb = max_memory_mb
        self.cache_dir = cache_dir or Path("/app/data/cache")
        self.cache_dir.mkdir(parents=True, exist_ok=True)
        
        self.memory_cache = {}
        self.cache_metadata = {}
        self.current_memory_mb = 0
        
    def _calculate_size_mb(self, obj: Any) -> float:
        """Calculate object size in MB"""
        try:
            if isinstance(obj, pd.DataFrame):
                return obj.memory_usage(deep=True).sum() / 1024 / 1024
            elif isinstance(obj, np.ndarray):
                return obj.nbytes / 1024 / 1024
            else:
                return len(pickle.dumps(obj)) / 1024 / 1024
        except:
            return 0.1  # Default fallback
    
    def _generate_key(self, func_name: str, args: tuple, kwargs: dict) -> str:
        """Generate cache key from function name and arguments"""
        # Create a stable hash from arguments
        key_data = {
            'func': func_name,
            'args': str(args),
            'kwargs': str(sorted(kwargs.items()))
        }
        
        key_string = str(key_data)
        return hashlib.md5(key_string.encode()).hexdigest()
    
    def _evict_oldest(self):
        """Evict oldest cache entries to free memory"""
        if not self.cache_metadata:
            return
            
        # Sort by last access time
        sorted_keys = sorted(
            self.cache_metadata.keys(),
            key=lambda k: self.cache_metadata[k]['last_access']
        )
        
        # Remove oldest entries until under memory limit
        for key in sorted_keys:
            if self.current_memory_mb <= self.max_memory_mb * 0.8:
                break
                
            if key in self.memory_cache:
                size_mb = self.cache_metadata[key]['size_mb']
                del self.memory_cache[key]
                del self.cache_metadata[key]
                self.current_memory_mb -= size_mb
                logger.info(f"Cache evicted: {key[:8]}... ({size_mb:.2f}MB freed)")
    
    def get(self, key: str) -> Optional[Any]:
        """Get cached value"""
        if key in self.memory_cache:
            self.cache_metadata[key]['last_access'] = datetime.now()
            self.cache_metadata[key]['hits'] += 1
            return self.memory_cache[key]
        
        # Try file cache
        file_path = self.cache_dir / f"{key}.pkl"
        if file_path.exists():
            try:
                with open(file_path, 'rb') as f:
                    value = pickle.load(f)
                    
                # Load back to memory cache if space available
                size_mb = self._calculate_size_mb(value)
                if self.current_memory_mb + size_mb <= self.max_memory_mb:
                    self.memory_cache[key] = value
                    self.cache_metadata[key] = {
                        'size_mb': size_mb,
                        'last_access': datetime.now(),
                        'hits': 1,
                        'created': datetime.now()
                    }
                    self.current_memory_mb += size_mb
                    
                return value
            except Exception as e:
                logger.warning(f"Failed to load cache file {key}: {e}")
                file_path.unlink(missing_ok=True)
        
        return None
    
    def set(self, key: str, value: Any, persist: bool = True):
        """Set cached value"""
        size_mb = self._calculate_size_mb(value)
        
        # Memory cache
        if size_mb <= self.max_memory_mb:
            # Ensure we have space
            if self.current_memory_mb + size_mb > self.max_memory_mb:
                self._evict_oldest()
            
            self.memory_cache[key] = value
            self.cache_metadata[key] = {
                'size_mb': size_mb,
                'last_access': datetime.now(),
                'hits': 0,
                'created': datetime.now()
            }
            self.current_memory_mb += size_mb
        
        # File cache for persistence
        if persist:
            try:
                file_path = self.cache_dir / f"{key}.pkl"
                with open(file_path, 'wb') as f:
                    pickle.dump(value, f)
            except Exception as e:
                logger.warning(f"Failed to persist cache {key}: {e}")
    
    def clear(self, pattern: Optional[str] = None):
        """Clear cache entries"""
        if pattern is None:
            # Clear all
            self.memory_cache.clear()
            self.cache_metadata.clear()
            self.current_memory_mb = 0
            
            # Clear file cache
            for file_path in self.cache_dir.glob("*.pkl"):
                file_path.unlink()
        else:
            # Clear matching pattern
            keys_to_remove = [k for k in self.memory_cache.keys() if pattern in k]
            for key in keys_to_remove:
                if key in self.memory_cache:
                    self.current_memory_mb -= self.cache_metadata[key]['size_mb']
                    del self.memory_cache[key]
                    del self.cache_metadata[key]
                
                # Remove file
                file_path = self.cache_dir / f"{key}.pkl"
                file_path.unlink(missing_ok=True)
    
    def get_stats(self) -> Dict[str, Any]:
        """Get cache statistics"""
        total_hits = sum(meta['hits'] for meta in self.cache_metadata.values())
        
        return {
            'memory_usage_mb': round(self.current_memory_mb, 2),
            'memory_limit_mb': self.max_memory_mb,
            'memory_usage_percent': round(self.current_memory_mb / self.max_memory_mb * 100, 1),
            'entries_count': len(self.memory_cache),
            'total_hits': total_hits,
            'cache_files': len(list(self.cache_dir.glob("*.pkl")))
        }

# Global cache manager
cache_manager = CacheManager()

def cached_function(expire_hours: int = 24, persist: bool = True, key_prefix: str = ""):
    """Decorator for caching function results"""
    def decorator(func: Callable) -> Callable:
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            # Generate cache key
            func_name = f"{key_prefix}{func.__name__}" if key_prefix else func.__name__
            cache_key = cache_manager._generate_key(func_name, args, kwargs)
            
            # Try to get from cache
            cached_result = cache_manager.get(cache_key)
            if cached_result is not None:
                logger.debug(f"Cache hit: {func.__name__}")
                return cached_result
            
            # Execute function
            logger.debug(f"Cache miss: {func.__name__}")
            result = func(*args, **kwargs)
            
            # Cache result
            cache_manager.set(cache_key, result, persist=persist)
            
            return result
        return wrapper
    return decorator

def cache_dataframe_operation(func: Callable) -> Callable:
    """Specialized caching for DataFrame operations"""
    @functools.wraps(func)
    def wrapper(df: pd.DataFrame, *args, **kwargs):
        # Create hash from DataFrame content and arguments
        df_hash = hashlib.md5(pd.util.hash_pandas_object(df).values).hexdigest()[:8]
        args_hash = hashlib.md5(str((args, kwargs)).encode()).hexdigest()[:8]
        cache_key = f"df_{func.__name__}_{df_hash}_{args_hash}"
        
        # Try cache
        cached_result = cache_manager.get(cache_key)
        if cached_result is not None:
            logger.debug(f"DataFrame cache hit: {func.__name__}")
            return cached_result
        
        # Execute and cache
        result = func(df, *args, **kwargs)
        cache_manager.set(cache_key, result, persist=True)
        
        return result
    return wrapper