Spaces:
Sleeping
Sleeping
Download database.py from ghstedpixel/app.py: direct link, hf CLI and curl.
- Browser
- Download file 4.46 kB
-
https://huggingface.co/spaces/ghstedpixel/app.py/resolve/4fa2fd90c00a78d4e4e68ccd489221ffc5d7362b/database.py
- Command line
-
hf download hf://spaces/ghstedpixel/app.py@4fa2fd90c00a78d4e4e68ccd489221ffc5d7362b/database.py
-
curl -L -o database.py https://huggingface.co/spaces/ghstedpixel/app.py/resolve/4fa2fd90c00a78d4e4e68ccd489221ffc5d7362b/database.py
4.46 kB
| import json | |
| import time | |
| import re | |
| from config import REDIS_URL | |
| REDIS_AVAILABLE = False | |
| try: | |
| import redis | |
| REDIS_AVAILABLE = True | |
| except ImportError: | |
| print("⚠️ WARNING: 'redis' library not installed. Caching will fall back to local in-memory dictionaries.") | |
| # --- SMART STORAGE MEMORY NODE LAYERS --- | |
| redis_client = None | |
| IN_MEMORY_CACHE = {} | |
| IN_MEMORY_EXPIRY = {} | |
| if REDIS_AVAILABLE and REDIS_URL: | |
| try: | |
| redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True) | |
| redis_client.ping() | |
| print("📶 [Cache Setup] Connected securely to remote Redis server node.") | |
| except Exception as e: | |
| print(f"⚠️ [Cache Setup] Redis connection failed ({e}). Defaulting to internal memory arrays.") | |
| redis_client = None | |
| def get_cache_key(product_name: str, size: str = "", mode: str = "balanced") -> str: | |
| """Generates a unique deterministic cache key combining name, size, and search mode""" | |
| clean_name = re.sub(r'[^a-z0-9 ]', '', product_name.lower().strip()) | |
| clean_size = re.sub(r'[^a-z0-9 ]', '', size.lower().strip()) if size else "nosize" | |
| clean_name = clean_name.replace(" ", "_") | |
| clean_size = clean_size.replace(" ", "_") | |
| return f"tarz:cache:{clean_name}:{clean_size}:{mode}" | |
| def get_cached_deals(product_name: str, size: str = "", mode: str = "balanced") -> list: | |
| """Retrieves cached pricing data array if available and unexpired""" | |
| key = get_cache_key(product_name, size, mode) | |
| if redis_client: | |
| try: | |
| data = redis_client.get(key) | |
| if data: return json.loads(data) | |
| except Exception as e: | |
| print(f"⚠️ [Redis Cache Error] Read failed: {e}") | |
| else: | |
| if key in IN_MEMORY_CACHE: | |
| if time.time() < IN_MEMORY_EXPIRY.get(key, 0): | |
| return IN_MEMORY_CACHE[key] | |
| else: | |
| del IN_MEMORY_CACHE[key] | |
| del IN_MEMORY_EXPIRY[key] | |
| return None | |
| def set_cached_deals(product_name: str, size: str, mode: str, deals: list): | |
| """Caches product list matrix with strict 2-hour sliding validation lifetime""" | |
| if not deals: return | |
| key = get_cache_key(product_name, size, mode) | |
| if redis_client: | |
| try: | |
| redis_client.setex(key, 7200, json.dumps(deals)) | |
| except Exception as e: | |
| print(f"⚠️ [Redis Cache Error] Write failed: {e}") | |
| else: | |
| IN_MEMORY_CACHE[key] = deals | |
| IN_MEMORY_EXPIRY[key] = time.time() + 7200 | |
| def get_base_price(product_name: str, size: str = "", mode: str = "balanced") -> float: | |
| """Retrieves the previous cycle's lowest observed stable price benchmark""" | |
| key = get_cache_key(product_name, size, mode).replace("tarz:cache:", "tarz:baseprice:") | |
| if redis_client: | |
| try: | |
| val = redis_client.get(key) | |
| if val: return float(val) | |
| except: pass | |
| else: | |
| return IN_MEMORY_CACHE.get(key) | |
| return None | |
| def set_base_price(product_name: str, size: str, mode: str, price: float): | |
| """Saves a new lowest benchmark configuration when an active shift is confirmed""" | |
| key = get_cache_key(product_name, size, mode).replace("tarz:cache:", "tarz:baseprice:") | |
| if redis_client: | |
| try: redis_client.set(key, str(price)) | |
| except: pass | |
| else: | |
| IN_MEMORY_CACHE[key] = price | |
| def get_cached_coupons(product_name: str) -> list: | |
| """Retrieves isolated promotional matrices matching strict text layout structures""" | |
| clean_name = re.sub(r'[^a-z0-9 ]', '', product_name.lower().strip()).replace(" ", "_") | |
| key = f"tarz:coupons:{clean_name}" | |
| if redis_client: | |
| try: | |
| data = redis_client.get(key) | |
| if data: return json.loads(data) | |
| except: pass | |
| else: | |
| if key in IN_MEMORY_CACHE: | |
| if time.time() < IN_MEMORY_EXPIRY.get(key, 0): | |
| return IN_MEMORY_CACHE[key] | |
| return None | |
| def set_cached_coupons(product_name: str, coupons: list): | |
| """Caches discovered active promo strings (saves empty results for 15 mins to prevent loop spam)""" | |
| clean_name = re.sub(r'[^a-z0-9 ]', '', product_name.lower().strip()).replace(" ", "_") | |
| key = f"tarz:coupons:{clean_name}" | |
| duration = 7200 if coupons else 900 | |
| if redis_client: | |
| try: redis_client.setex(key, duration, json.dumps(coupons)) | |
| except: pass | |
| else: | |
| IN_MEMORY_CACHE[key] = coupons | |
| IN_MEMORY_EXPIRY[key] = time.time() + duration |