File size: 975 Bytes
0914e96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from cachetools import TTLCache
import functools

# Cache up to 100 items for 1 hour (3600 seconds)
memory_cache = TTLCache(maxsize=100, ttl=3600)

def cached_response(func):
    """Decorator to cache function outputs."""
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # Key unique hona chahiye, e.g., query string
        try:
            # Sirf pehle argument (query) ko key banate hain simple rakhne ke liye
            cache_key = str(args[0]) if args else "default"
            
            if cache_key in memory_cache:
                # print(f"⚡ Using Cache for: {cache_key[:30]}...")
                return memory_cache[cache_key]
        except:
            pass # Agar cache key banane me error aaye to ignore karo

        result = func(*args, **kwargs)
        
        # Cache save
        if args:
             cache_key = str(args[0])
             memory_cache[cache_key] = result
             
        return result
    return wrapper