Spaces:
Runtime error
Runtime error
ddad
Browse files- README.md +3 -2
- modules/server_cache.py +36 -3
README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
-
title: rdune71/
|
| 4 |
sdk: gradio
|
| 5 |
-
sdk_version:
|
|
|
|
| 6 |
---
|
| 7 |
# README.md
|
| 8 |
# AI Research Assistant
|
|
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
+
title: rdune71/myspace133
|
| 4 |
sdk: gradio
|
| 5 |
+
sdk_version: 4.38.1
|
| 6 |
+
|
| 7 |
---
|
| 8 |
# README.md
|
| 9 |
# AI Research Assistant
|
modules/server_cache.py
CHANGED
|
@@ -1,23 +1,56 @@
|
|
|
|
|
| 1 |
# modules/server_cache.py
|
| 2 |
import time
|
| 3 |
import redis
|
| 4 |
import json
|
| 5 |
import logging
|
|
|
|
| 6 |
|
| 7 |
class RedisServerStatusCache:
|
| 8 |
-
def __init__(self, host=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Test connection
|
| 12 |
self.redis.ping()
|
| 13 |
self.use_redis = True
|
| 14 |
-
logging.info("Connected to Redis cache")
|
| 15 |
except (redis.ConnectionError, redis.TimeoutError) as e:
|
| 16 |
# Fallback to in-memory cache if Redis is not available
|
| 17 |
self.redis = None
|
| 18 |
self.fallback_cache = {}
|
| 19 |
self.use_redis = False
|
| 20 |
logging.warning(f"Redis not available, using in-memory cache: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
self.default_ttl = default_ttl
|
| 23 |
|
|
|
|
| 1 |
+
===== ./modules/server_cache.py =====
|
| 2 |
# modules/server_cache.py
|
| 3 |
import time
|
| 4 |
import redis
|
| 5 |
import json
|
| 6 |
import logging
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
class RedisServerStatusCache:
|
| 10 |
+
def __init__(self, host=None, port=None, password=None, db=0, default_ttl=300):
|
| 11 |
+
# Use environment variables or passed parameters
|
| 12 |
+
redis_host = host or os.getenv('REDIS_HOST', 'localhost')
|
| 13 |
+
redis_port = port or int(os.getenv('REDIS_PORT', 6379))
|
| 14 |
+
redis_password = password or os.getenv('REDIS_PASSWORD', None)
|
| 15 |
+
|
| 16 |
try:
|
| 17 |
+
# Connect to Redis with authentication if password provided
|
| 18 |
+
if redis_password and redis_password != "":
|
| 19 |
+
self.redis = redis.StrictRedis(
|
| 20 |
+
host=redis_host,
|
| 21 |
+
port=redis_port,
|
| 22 |
+
db=db,
|
| 23 |
+
password=redis_password,
|
| 24 |
+
decode_responses=True,
|
| 25 |
+
socket_connect_timeout=5,
|
| 26 |
+
socket_timeout=5
|
| 27 |
+
)
|
| 28 |
+
else:
|
| 29 |
+
self.redis = redis.StrictRedis(
|
| 30 |
+
host=redis_host,
|
| 31 |
+
port=redis_port,
|
| 32 |
+
db=db,
|
| 33 |
+
decode_responses=True,
|
| 34 |
+
socket_connect_timeout=5,
|
| 35 |
+
socket_timeout=5
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
# Test connection
|
| 39 |
self.redis.ping()
|
| 40 |
self.use_redis = True
|
| 41 |
+
logging.info(f"Connected to Redis cache at {redis_host}:{redis_port}")
|
| 42 |
except (redis.ConnectionError, redis.TimeoutError) as e:
|
| 43 |
# Fallback to in-memory cache if Redis is not available
|
| 44 |
self.redis = None
|
| 45 |
self.fallback_cache = {}
|
| 46 |
self.use_redis = False
|
| 47 |
logging.warning(f"Redis not available, using in-memory cache: {e}")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
# Fallback for any other Redis errors
|
| 50 |
+
self.redis = None
|
| 51 |
+
self.fallback_cache = {}
|
| 52 |
+
self.use_redis = False
|
| 53 |
+
logging.warning(f"Redis connection failed, using in-memory cache: {e}")
|
| 54 |
|
| 55 |
self.default_ttl = default_ttl
|
| 56 |
|