Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(cache): explore using fastapi-cache to cache responses #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fastapi-cache2
shapely
xarray
xpublish>=0.3.0,<0.4.0
22 changes: 20 additions & 2 deletions xpublish_edr/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

import importlib
import logging
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import List, Optional

import xarray as xr
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi import APIRouter, Depends, FastAPI, HTTPException, Request
from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend
from fastapi_cache.decorator import cache
from xpublish import Dependencies, Plugin, hookimpl

from .formats.to_covjson import to_cf_covjson
Expand Down Expand Up @@ -65,8 +70,21 @@ def get_position_formats():
@hookimpl
def dataset_router(self, deps: Dependencies):
"""Register dataset level router for EDR endpoints"""
router = APIRouter(prefix=self.app_router_prefix, tags=self.dataset_router_tags)

@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
"""Lifespan for the application level router"""
# TODO: support other backends
FastAPICache.init(InMemoryBackend())
yield

router = APIRouter(
prefix=self.app_router_prefix,
tags=self.dataset_router_tags,
lifespan=lifespan,
)

@cache()
@router.get("/position", summary="Position query")
def get_position(
request: Request,
Expand Down
Loading