Spaces:
Paused
Paused
Upload 94 files
Browse files- app/__pycache__/main.cpython-312.pyc +0 -0
- app/core/__pycache__/config.cpython-312.pyc +0 -0
- app/core/config.py +16 -6
- app/main.py +5 -4
- app/realtime/__pycache__/subscriber.cpython-312.pyc +0 -0
- app/realtime/subscriber.py +2 -0
- app/utils/__pycache__/cache.cpython-312.pyc +0 -0
- app/utils/cache.py +79 -63
app/__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/main.cpython-312.pyc and b/app/__pycache__/main.cpython-312.pyc differ
|
|
|
app/core/__pycache__/config.cpython-312.pyc
CHANGED
|
Binary files a/app/core/__pycache__/config.cpython-312.pyc and b/app/core/__pycache__/config.cpython-312.pyc differ
|
|
|
app/core/config.py
CHANGED
|
@@ -8,11 +8,13 @@ class Settings(BaseSettings):
|
|
| 8 |
VERSION: str = "1.0.0"
|
| 9 |
|
| 10 |
# Service Authentication
|
| 11 |
-
SERVICE_TOKEN: str
|
| 12 |
-
POS_API_URL: str
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# PostgreSQL Database settings
|
| 15 |
-
DATABASE_URL:
|
| 16 |
|
| 17 |
# JWT Settings
|
| 18 |
SECRET_KEY: str = "your-secret-key-here"
|
|
@@ -20,9 +22,16 @@ class Settings(BaseSettings):
|
|
| 20 |
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
|
| 21 |
|
| 22 |
# Redis settings
|
| 23 |
-
REDIS_HOST: str
|
| 24 |
-
REDIS_PORT: int
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Email settings
|
| 28 |
MAIL_USERNAME: str = "yungdml31@gmail.com"
|
|
@@ -36,6 +45,7 @@ class Settings(BaseSettings):
|
|
| 36 |
|
| 37 |
class Config:
|
| 38 |
case_sensitive = True
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
settings = Settings()
|
|
|
|
| 8 |
VERSION: str = "1.0.0"
|
| 9 |
|
| 10 |
# Service Authentication
|
| 11 |
+
SERVICE_TOKEN: str
|
| 12 |
+
POS_API_URL: str
|
| 13 |
+
POS_API_KEY: str
|
| 14 |
+
POS_API_VERSION: str
|
| 15 |
|
| 16 |
# PostgreSQL Database settings
|
| 17 |
+
DATABASE_URL: str
|
| 18 |
|
| 19 |
# JWT Settings
|
| 20 |
SECRET_KEY: str = "your-secret-key-here"
|
|
|
|
| 22 |
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
|
| 23 |
|
| 24 |
# Redis settings
|
| 25 |
+
REDIS_HOST: str
|
| 26 |
+
REDIS_PORT: int
|
| 27 |
+
REDIS_PASSWORD: str
|
| 28 |
+
REDIS_USERNAME: str = "default"
|
| 29 |
+
|
| 30 |
+
@property
|
| 31 |
+
def REDIS_URL(self) -> str:
|
| 32 |
+
if self.REDIS_PASSWORD:
|
| 33 |
+
return f"redis://{self.REDIS_USERNAME}:{self.REDIS_PASSWORD}@{self.REDIS_HOST}:{self.REDIS_PORT}"
|
| 34 |
+
return f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}"
|
| 35 |
|
| 36 |
# Email settings
|
| 37 |
MAIL_USERNAME: str = "yungdml31@gmail.com"
|
|
|
|
| 45 |
|
| 46 |
class Config:
|
| 47 |
case_sensitive = True
|
| 48 |
+
env_file = ".env"
|
| 49 |
|
| 50 |
|
| 51 |
settings = Settings()
|
app/main.py
CHANGED
|
@@ -13,7 +13,7 @@ import socketio
|
|
| 13 |
import time
|
| 14 |
import logging
|
| 15 |
import asyncio
|
| 16 |
-
from typing import List, Dict
|
| 17 |
|
| 18 |
# Configure logging
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -39,15 +39,16 @@ app.add_middleware(
|
|
| 39 |
|
| 40 |
# Socket.IO event handlers
|
| 41 |
@sio.event
|
| 42 |
-
async def connect(sid, environ):
|
| 43 |
logger.info(f"Client connected: {sid}")
|
|
|
|
| 44 |
|
| 45 |
@sio.event
|
| 46 |
-
async def disconnect(sid):
|
| 47 |
logger.info(f"Client disconnected: {sid}")
|
| 48 |
|
| 49 |
@sio.event
|
| 50 |
-
async def message(sid, data):
|
| 51 |
logger.info(f"Message from {sid}: {data}")
|
| 52 |
await sio.emit('message', {'response': 'Message received'}, room=sid)
|
| 53 |
|
|
|
|
| 13 |
import time
|
| 14 |
import logging
|
| 15 |
import asyncio
|
| 16 |
+
from typing import List, Dict, Optional
|
| 17 |
|
| 18 |
# Configure logging
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 39 |
|
| 40 |
# Socket.IO event handlers
|
| 41 |
@sio.event
|
| 42 |
+
async def connect(sid: str, environ: dict, auth: Optional[dict] = None):
|
| 43 |
logger.info(f"Client connected: {sid}")
|
| 44 |
+
return True
|
| 45 |
|
| 46 |
@sio.event
|
| 47 |
+
async def disconnect(sid: str):
|
| 48 |
logger.info(f"Client disconnected: {sid}")
|
| 49 |
|
| 50 |
@sio.event
|
| 51 |
+
async def message(sid: str, data: dict):
|
| 52 |
logger.info(f"Message from {sid}: {data}")
|
| 53 |
await sio.emit('message', {'response': 'Message received'}, room=sid)
|
| 54 |
|
app/realtime/__pycache__/subscriber.cpython-312.pyc
CHANGED
|
Binary files a/app/realtime/__pycache__/subscriber.cpython-312.pyc and b/app/realtime/__pycache__/subscriber.cpython-312.pyc differ
|
|
|
app/realtime/subscriber.py
CHANGED
|
@@ -12,6 +12,8 @@ async def subscribe_order_events(manager: ConnectionManager) -> None:
|
|
| 12 |
r = redis.Redis(
|
| 13 |
host=settings.REDIS_HOST,
|
| 14 |
port=settings.REDIS_PORT,
|
|
|
|
|
|
|
| 15 |
decode_responses=True
|
| 16 |
)
|
| 17 |
|
|
|
|
| 12 |
r = redis.Redis(
|
| 13 |
host=settings.REDIS_HOST,
|
| 14 |
port=settings.REDIS_PORT,
|
| 15 |
+
password=settings.REDIS_PASSWORD,
|
| 16 |
+
username=settings.REDIS_USERNAME,
|
| 17 |
decode_responses=True
|
| 18 |
)
|
| 19 |
|
app/utils/__pycache__/cache.cpython-312.pyc
CHANGED
|
Binary files a/app/utils/__pycache__/cache.cpython-312.pyc and b/app/utils/__pycache__/cache.cpython-312.pyc differ
|
|
|
app/utils/cache.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
-
import
|
| 2 |
import json
|
| 3 |
import inspect
|
| 4 |
-
import
|
| 5 |
from ..core.config import settings
|
| 6 |
-
from typing import Any, Optional, Callable, TypeVar
|
| 7 |
from ..utils.logger import logger
|
| 8 |
|
| 9 |
T = TypeVar('T')
|
|
@@ -19,6 +18,8 @@ class RedisCache:
|
|
| 19 |
cls._instance.redis = redis.Redis(
|
| 20 |
host=settings.REDIS_HOST,
|
| 21 |
port=settings.REDIS_PORT,
|
|
|
|
|
|
|
| 22 |
decode_responses=True
|
| 23 |
)
|
| 24 |
return cls._instance
|
|
@@ -37,10 +38,11 @@ class RedisCache:
|
|
| 37 |
async def set_cache(self, key: str, value: Any, expire: int = 3600):
|
| 38 |
"""Set a cache entry with optional expiration time (default 1 hour)"""
|
| 39 |
try:
|
| 40 |
-
if self.is_connected:
|
| 41 |
-
await self.redis.set(key, json.dumps(value), ex=expire)
|
| 42 |
-
else:
|
| 43 |
self.fallback_cache[key] = value
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
logger.error(f"Cache set error: {str(e)}")
|
| 46 |
self.fallback_cache[key] = value
|
|
@@ -48,21 +50,24 @@ class RedisCache:
|
|
| 48 |
async def get_cache(self, key: str) -> Optional[Any]:
|
| 49 |
"""Get a cached value by key"""
|
| 50 |
try:
|
| 51 |
-
if self.is_connected:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
logger.error(f"Cache get error: {str(e)}")
|
| 57 |
return self.fallback_cache.get(key)
|
|
|
|
| 58 |
|
| 59 |
async def delete_cache(self, key: str) -> bool:
|
| 60 |
"""Delete a cache entry by key"""
|
| 61 |
try:
|
| 62 |
-
if self.is_connected:
|
| 63 |
-
return bool(
|
| 64 |
-
|
| 65 |
-
return
|
| 66 |
except Exception as e:
|
| 67 |
logger.error(f"Cache delete error: {str(e)}")
|
| 68 |
return False
|
|
@@ -70,67 +75,78 @@ class RedisCache:
|
|
| 70 |
async def clear_cache_pattern(self, pattern: str) -> bool:
|
| 71 |
"""Clear all cache entries matching a pattern"""
|
| 72 |
try:
|
| 73 |
-
if self.is_connected:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
-
logger.error(f"Cache clear
|
| 80 |
return False
|
| 81 |
|
| 82 |
async def check_connection(self) -> bool:
|
| 83 |
"""Check if Redis connection is alive"""
|
| 84 |
try:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
return False
|
| 89 |
except Exception:
|
|
|
|
| 90 |
return False
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
def cached(ttl_seconds: int):
|
| 93 |
"""Decorator to cache function results"""
|
| 94 |
def decorator(func: Callable[..., T]) -> Callable[..., T]:
|
| 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 |
-
except Exception as e:
|
| 131 |
-
logger.error(f"Cache set error in sync wrapper: {str(e)}")
|
| 132 |
-
return result
|
| 133 |
-
return sync_wrapper
|
| 134 |
return decorator
|
| 135 |
|
| 136 |
cache = RedisCache()
|
|
|
|
| 1 |
+
from typing import Optional, Any, Dict, Callable, TypeVar
|
| 2 |
import json
|
| 3 |
import inspect
|
| 4 |
+
import redis.asyncio as redis
|
| 5 |
from ..core.config import settings
|
|
|
|
| 6 |
from ..utils.logger import logger
|
| 7 |
|
| 8 |
T = TypeVar('T')
|
|
|
|
| 18 |
cls._instance.redis = redis.Redis(
|
| 19 |
host=settings.REDIS_HOST,
|
| 20 |
port=settings.REDIS_PORT,
|
| 21 |
+
password=settings.REDIS_PASSWORD,
|
| 22 |
+
username=settings.REDIS_USERNAME,
|
| 23 |
decode_responses=True
|
| 24 |
)
|
| 25 |
return cls._instance
|
|
|
|
| 38 |
async def set_cache(self, key: str, value: Any, expire: int = 3600):
|
| 39 |
"""Set a cache entry with optional expiration time (default 1 hour)"""
|
| 40 |
try:
|
| 41 |
+
if not self.is_connected:
|
|
|
|
|
|
|
| 42 |
self.fallback_cache[key] = value
|
| 43 |
+
return
|
| 44 |
+
|
| 45 |
+
await self.redis.set(key, json.dumps(value), ex=expire)
|
| 46 |
except Exception as e:
|
| 47 |
logger.error(f"Cache set error: {str(e)}")
|
| 48 |
self.fallback_cache[key] = value
|
|
|
|
| 50 |
async def get_cache(self, key: str) -> Optional[Any]:
|
| 51 |
"""Get a cached value by key"""
|
| 52 |
try:
|
| 53 |
+
if not self.is_connected:
|
| 54 |
+
return self.fallback_cache.get(key)
|
| 55 |
+
|
| 56 |
+
value = await self.redis.get(key)
|
| 57 |
+
if value:
|
| 58 |
+
return json.loads(value)
|
| 59 |
except Exception as e:
|
| 60 |
logger.error(f"Cache get error: {str(e)}")
|
| 61 |
return self.fallback_cache.get(key)
|
| 62 |
+
return None
|
| 63 |
|
| 64 |
async def delete_cache(self, key: str) -> bool:
|
| 65 |
"""Delete a cache entry by key"""
|
| 66 |
try:
|
| 67 |
+
if not self.is_connected:
|
| 68 |
+
return bool(self.fallback_cache.pop(key, None))
|
| 69 |
+
|
| 70 |
+
return bool(await self.redis.delete(key))
|
| 71 |
except Exception as e:
|
| 72 |
logger.error(f"Cache delete error: {str(e)}")
|
| 73 |
return False
|
|
|
|
| 75 |
async def clear_cache_pattern(self, pattern: str) -> bool:
|
| 76 |
"""Clear all cache entries matching a pattern"""
|
| 77 |
try:
|
| 78 |
+
if not self.is_connected:
|
| 79 |
+
# Basic pattern matching for fallback cache
|
| 80 |
+
removed = 0
|
| 81 |
+
for key in list(self.fallback_cache.keys()):
|
| 82 |
+
if pattern in key:
|
| 83 |
+
del self.fallback_cache[key]
|
| 84 |
+
removed += 1
|
| 85 |
+
return removed > 0
|
| 86 |
+
|
| 87 |
+
# Get all keys matching pattern
|
| 88 |
+
keys = [key async for key in self.redis.scan_iter(pattern)]
|
| 89 |
+
if keys:
|
| 90 |
+
await self.redis.delete(*keys)
|
| 91 |
+
return bool(keys)
|
| 92 |
except Exception as e:
|
| 93 |
+
logger.error(f"Cache pattern clear error: {str(e)}")
|
| 94 |
return False
|
| 95 |
|
| 96 |
async def check_connection(self) -> bool:
|
| 97 |
"""Check if Redis connection is alive"""
|
| 98 |
try:
|
| 99 |
+
await self.redis.ping()
|
| 100 |
+
self.is_connected = True
|
| 101 |
+
return True
|
|
|
|
| 102 |
except Exception:
|
| 103 |
+
self.is_connected = False
|
| 104 |
return False
|
| 105 |
|
| 106 |
+
async def cleanup_expired(self):
|
| 107 |
+
"""Clean up expired cache entries"""
|
| 108 |
+
# Redis automatically handles expiration, only need to clean fallback
|
| 109 |
+
if not self.is_connected:
|
| 110 |
+
self.fallback_cache = {}
|
| 111 |
+
|
| 112 |
def cached(ttl_seconds: int):
|
| 113 |
"""Decorator to cache function results"""
|
| 114 |
def decorator(func: Callable[..., T]) -> Callable[..., T]:
|
| 115 |
+
async def async_wrapper(*args, **kwargs) -> T:
|
| 116 |
+
# Create cache key from function name and arguments
|
| 117 |
+
key = f"{func.__name__}:{str(args)}:{str(kwargs)}"
|
| 118 |
+
|
| 119 |
+
# Try to get from cache first
|
| 120 |
+
cached_value = await cache.get_cache(key)
|
| 121 |
+
if cached_value is not None:
|
| 122 |
+
return cached_value
|
| 123 |
+
|
| 124 |
+
# If not in cache, execute function
|
| 125 |
+
result = await func(*args, **kwargs)
|
| 126 |
+
|
| 127 |
+
# Cache the result
|
| 128 |
+
await cache.set_cache(key, result, expire=ttl_seconds)
|
| 129 |
+
|
| 130 |
+
return result
|
| 131 |
+
|
| 132 |
+
def sync_wrapper(*args, **kwargs) -> T:
|
| 133 |
+
# Create cache key from function name and arguments
|
| 134 |
+
key = f"{func.__name__}:{str(args)}:{str(kwargs)}"
|
| 135 |
+
|
| 136 |
+
# For sync functions, we can't use async cache directly
|
| 137 |
+
# So we use the fallback cache
|
| 138 |
+
if key in cache.fallback_cache:
|
| 139 |
+
return cache.fallback_cache[key]
|
| 140 |
+
|
| 141 |
+
# If not in cache, execute function
|
| 142 |
+
result = func(*args, **kwargs)
|
| 143 |
+
|
| 144 |
+
# Cache the result in fallback
|
| 145 |
+
cache.fallback_cache[key] = result
|
| 146 |
+
|
| 147 |
+
return result
|
| 148 |
+
|
| 149 |
+
return async_wrapper if inspect.iscoroutinefunction(func) else sync_wrapper
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
return decorator
|
| 151 |
|
| 152 |
cache = RedisCache()
|