| 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.""" | |
| 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 |