Skip to content

Commit

Permalink
feat(server): differenciate cache from hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
alambare committed Jun 9, 2024
1 parent 35a2c79 commit 9f10748
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions eodag/rest/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import Any, Callable, Coroutine, Dict, TypeVar
from typing import Any, Callable, Coroutine, Dict, TypeVar, cast

import orjson
from cachetools import LRUCache
from fastapi import FastAPI, Request

from eodag.rest.config import Settings
from eodag.utils import urlsplit

logger = logging.getLogger("eodag.rest.utils")

Expand All @@ -42,10 +43,14 @@ async def cached(
"""Either get the result from local cache or run the function and cache the result."""
settings = Settings.from_environment()

host = urlsplit(cast(str, request.state.url_root)).netloc

host_cache_key = f"{cache_key}:{host}"

try:
c: Dict[str, Any] = request.app.state.cache

if cached := c.get(cache_key):
if cached := c.get(host_cache_key):
logger.debug("Cache result hit")
return orjson.loads(cached) # type: ignore
except Exception as e:
Expand All @@ -56,7 +61,7 @@ async def cached(
result = await fn()

try:
c[cache_key] = orjson.dumps(result) # type: ignore
c[host_cache_key] = orjson.dumps(result) # type: ignore
except Exception as e:
logger.error(f"Error in cache: {e}")
if settings.debug:
Expand Down

0 comments on commit 9f10748

Please sign in to comment.