feat: implement caching for marketplace stocks using Redis
This commit is contained in:
2
main.py
2
main.py
@@ -125,8 +125,8 @@ def get_status(task_id):
|
|||||||
|
|
||||||
@app.get('/marketplace/{marketplace_id}/stocks')
|
@app.get('/marketplace/{marketplace_id}/stocks')
|
||||||
async def get_marketplace_stocks(
|
async def get_marketplace_stocks(
|
||||||
marketplace_id: int,
|
|
||||||
session: SessionDependency,
|
session: SessionDependency,
|
||||||
|
marketplace_id: int,
|
||||||
only_available: bool = False
|
only_available: bool = False
|
||||||
):
|
):
|
||||||
updater = StocksUpdater(session)
|
updater = StocksUpdater(session)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
import redis.asyncio
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from typing import List, Union
|
from typing import List, Union
|
||||||
|
|
||||||
@@ -8,6 +10,7 @@ from sqlalchemy import select, or_
|
|||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.orm import joinedload, selectinload
|
from sqlalchemy.orm import joinedload, selectinload
|
||||||
|
|
||||||
|
from backend.config import REDIS_URL
|
||||||
from backend.session import session_factory
|
from backend.session import session_factory
|
||||||
from database import Marketplace, MarketplaceProduct, Warehouse, Company
|
from database import Marketplace, MarketplaceProduct, Warehouse, Company
|
||||||
from database.sipro.enums.general import BaseMarketplace
|
from database.sipro.enums.general import BaseMarketplace
|
||||||
@@ -74,9 +77,16 @@ class StocksUpdater:
|
|||||||
f"{marketplace.name} successfully fully updated in {round(time.time() - start, 2)} seconds.")
|
f"{marketplace.name} successfully fully updated in {round(time.time() - start, 2)} seconds.")
|
||||||
|
|
||||||
async def get_all_stocks_for_marketplace(self, marketplace_id: int, only_available: bool) -> List[StockData]:
|
async def get_all_stocks_for_marketplace(self, marketplace_id: int, only_available: bool) -> List[StockData]:
|
||||||
|
cache_key = f"marketplace_stocks_{marketplace_id}_{only_available}"
|
||||||
|
client = redis.asyncio.from_url(REDIS_URL)
|
||||||
|
if cached := await client.get(cache_key):
|
||||||
|
return json.loads(cached)
|
||||||
|
|
||||||
marketplace = await self.get_marketplace(marketplace_id)
|
marketplace = await self.get_marketplace(marketplace_id)
|
||||||
updater = UpdaterFactory.get_updater(self.session, marketplace)
|
updater = UpdaterFactory.get_updater(self.session, marketplace)
|
||||||
return await updater.get_all_stocks(only_available)
|
response = await updater.get_all_stocks(only_available)
|
||||||
|
await client.set(cache_key, json.dumps(response), ex=600)
|
||||||
|
return response
|
||||||
|
|
||||||
async def full_update_all_marketplaces(self, marketplace_ids: Union[List[int], None] = None):
|
async def full_update_all_marketplaces(self, marketplace_ids: Union[List[int], None] = None):
|
||||||
marketplaces = await self.get_marketplaces(marketplace_ids)
|
marketplaces = await self.get_marketplaces(marketplace_ids)
|
||||||
|
|||||||
Reference in New Issue
Block a user