Skip to content

Commit

Permalink
add source cache headers
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukaMa committed Jul 13, 2024
1 parent fbbc04e commit c2dd107
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 3 additions & 2 deletions webapi/block_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from starlette.requests import Request

from db import Database
from webapi.utils import SJSONResponse
from webapi.utils import SJSONResponse, cache_seconds


async def get_summary(db: Database):
Expand All @@ -22,12 +22,13 @@ async def get_summary(db: Database):
}
return summary


@cache_seconds(5)
async def recent_blocks_route(request: Request):
db: Database = request.app.state.db
recent_blocks = await db.get_recent_blocks_fast(10)
return SJSONResponse(recent_blocks)

@cache_seconds(5)
async def index_update_route(request: Request):
db: Database = request.app.state.db
last_block = request.query_params.get("last_block")
Expand Down
16 changes: 14 additions & 2 deletions webapi/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import asyncio
import functools
import os
import time
from typing import Any
from typing import Any, Callable, Coroutine

import aiohttp
import simplejson
from starlette.requests import Request
from starlette.responses import Response

from db import Database
Expand Down Expand Up @@ -94,4 +96,14 @@ async def function_definition(db: Database, program_id: str, function_name: str)
data = await db.get_function_definition(program_id, function_name)
if data is None:
return f"Unknown function {program_id}/{function_name}"
return data
return data

def cache_seconds(seconds: int):
def decorator(func: Callable[[Request], Coroutine[Any, Any, Response]]):
@functools.wraps(func)
async def wrapper(request: Request):
response = await func(request)
response.headers["Cache-Control"] = f"max-age={seconds}"
return response
return wrapper
return decorator
4 changes: 3 additions & 1 deletion webapi/webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from util.set_proc_title import set_proc_title
from .block_routes import get_summary, recent_blocks_route, index_update_route
from .error_routes import bad_request, not_found, internal_error
from .utils import out_of_sync_check, SJSONResponse
from .utils import cache_seconds, out_of_sync_check, SJSONResponse

load_dotenv()

Expand All @@ -39,11 +39,13 @@ def run(self, *args: Any, **kwargs: Any):
async def index_route(request: Request):
return SJSONResponse({"hello": "world"})

@cache_seconds(10)
async def sync_info_route(request: Request):
db: Database = request.app.state.db
sync_info = await out_of_sync_check(request.app.state.session, db)
return SJSONResponse(sync_info)

@cache_seconds(5)
async def summary_route(request: Request):
db: Database = request.app.state.db
return SJSONResponse(await get_summary(db))
Expand Down

0 comments on commit c2dd107

Please sign in to comment.