Spaces:
Sleeping
Sleeping
Redis + Websocket
Browse files
app.py
CHANGED
|
@@ -156,11 +156,11 @@ async def vote_by_voter(vote_request: VoteRequest):
|
|
| 156 |
raise HTTPException(status_code=500, detail=str(e))
|
| 157 |
|
| 158 |
@app.get("/get-voter")
|
| 159 |
-
async def get_voter(
|
| 160 |
try:
|
| 161 |
# Check cache
|
| 162 |
try:
|
| 163 |
-
cached_voter = redis_client.get(f'voter:{
|
| 164 |
if cached_voter:
|
| 165 |
return json.loads(cached_voter)
|
| 166 |
except RedisError as redis_err:
|
|
@@ -168,7 +168,7 @@ async def get_voter(voter_id: str):
|
|
| 168 |
# Continue without cache if Redis fails
|
| 169 |
pass
|
| 170 |
|
| 171 |
-
voter = voter_collection.find_one({'_id': ObjectId(
|
| 172 |
if not voter:
|
| 173 |
raise HTTPException(status_code=404, detail="Voter not found")
|
| 174 |
|
|
@@ -182,7 +182,7 @@ async def get_voter(voter_id: str):
|
|
| 182 |
|
| 183 |
# Try to save to cache
|
| 184 |
try:
|
| 185 |
-
redis_client.setex(f'voter:{
|
| 186 |
except RedisError as redis_err:
|
| 187 |
print(f"Redis caching error: {redis_err}")
|
| 188 |
# Continue even if caching fails
|
|
@@ -190,6 +190,7 @@ async def get_voter(voter_id: str):
|
|
| 190 |
|
| 191 |
return voter_data
|
| 192 |
except Exception as e:
|
|
|
|
| 193 |
raise HTTPException(status_code=500, detail=str(e))
|
| 194 |
|
| 195 |
@socket_manager.on('connect')
|
|
|
|
| 156 |
raise HTTPException(status_code=500, detail=str(e))
|
| 157 |
|
| 158 |
@app.get("/get-voter")
|
| 159 |
+
async def get_voter(id: str):
|
| 160 |
try:
|
| 161 |
# Check cache
|
| 162 |
try:
|
| 163 |
+
cached_voter = redis_client.get(f'voter:{id}')
|
| 164 |
if cached_voter:
|
| 165 |
return json.loads(cached_voter)
|
| 166 |
except RedisError as redis_err:
|
|
|
|
| 168 |
# Continue without cache if Redis fails
|
| 169 |
pass
|
| 170 |
|
| 171 |
+
voter = voter_collection.find_one({'_id': ObjectId(id)})
|
| 172 |
if not voter:
|
| 173 |
raise HTTPException(status_code=404, detail="Voter not found")
|
| 174 |
|
|
|
|
| 182 |
|
| 183 |
# Try to save to cache
|
| 184 |
try:
|
| 185 |
+
redis_client.setex(f'voter:{id}', CACHE_EXPIRE_TIME, json.dumps(voter_data))
|
| 186 |
except RedisError as redis_err:
|
| 187 |
print(f"Redis caching error: {redis_err}")
|
| 188 |
# Continue even if caching fails
|
|
|
|
| 190 |
|
| 191 |
return voter_data
|
| 192 |
except Exception as e:
|
| 193 |
+
print(f"Error in get_voter: {str(e)}")
|
| 194 |
raise HTTPException(status_code=500, detail=str(e))
|
| 195 |
|
| 196 |
@socket_manager.on('connect')
|