Skip to content

Commit

Permalink
feat: add cache API
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Feb 4, 2024
1 parent 8df2926 commit b420992
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 3 deletions.
3 changes: 2 additions & 1 deletion backend/api/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from fastapi import APIRouter

from backend.api.endpoints import analyze, lookup, submit
from backend.api.endpoints import analyze, cache, lookup, submit

api_router = APIRouter()
api_router.include_router(analyze.router, prefix="/analyze", tags=["analyze"])
api_router.include_router(submit.router, prefix="/submit", tags=["submit"])
api_router.include_router(lookup.router, prefix="/lookup", tags=["lookup"])
api_router.include_router(cache.router, prefix="/cache", tags=["cache"])
2 changes: 1 addition & 1 deletion backend/api/endpoints/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def cache_response(
expire: int = settings.REDIS_EXPIRE,
field: str = settings.REDIS_FIELD,
):
redis.hset(name=response.id, key=field, value=response.model_dump_json())
redis.hset(name=field, key=response.id, value=response.model_dump_json())

if expire > 0:
redis.expire(name=response.id, time=expire)
Expand Down
21 changes: 21 additions & 0 deletions backend/api/endpoints/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fastapi import APIRouter, HTTPException, status

from backend import deps, settings

router = APIRouter()


@router.get(
"/",
response_description="Return cache keys",
summary="Get analysis cache keys",
description="Try to get analysis cache keys",
)
async def cache_keys(optional_redis: deps.OptionalRedis) -> list[str]:
if optional_redis is None:
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail="Redis cache is not enabled",
)

return optional_redis.hkeys(settings.REDIS_FIELD) # type: ignore
2 changes: 1 addition & 1 deletion backend/api/endpoints/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def lookup(id: str, *, optional_redis: deps.OptionalRedis) -> schemas.Resp
detail="Redis cache is not enabled",
)

got: bytes | None = optional_redis.hget(name=id, key=settings.REDIS_FIELD) # type: ignore
got: bytes | None = optional_redis.hget(key=id, name=settings.REDIS_FIELD) # type: ignore
if got is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ export const API = {
async lookup(id: string): Promise<Response> {
const res = await client.get<Response>(`/api/lookup/${id}`)
return res.data
},
async getCacheKeys(): Promise<string[]> {
const res = await client.get<string[]>(`/api/cache/`)
return res.data
}
}
46 changes: 46 additions & 0 deletions frontend/src/components/Cache.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<template>
<div class="box">
<h2 class="is-size-4 has-text-weight-bold middle">Cache</h2>
<Loading v-if="getCacheKeysTask.isRunning"></Loading>
<ErrorMessage :error="getCacheKeysTask.last?.error" v-if="getCacheKeysTask.isError" />
<div class="block" v-if="getCacheKeysTask.last?.value && !getCacheKeysTask.last.isRunning">
<div class="buttons">
<router-link
class="button is-link is-light"
:to="{ name: 'Lookup', params: { id: key } }"
v-for="key in getCacheKeysTask.last.value"
:key="key"
>{{ key }}</router-link
>
</div>
</div>
</div>
</template>

<script lang="ts">
import { defineComponent, onMounted } from 'vue'
import { useAsyncTask } from 'vue-concurrency'
import { API } from '@/api'
import ErrorMessage from '@/components/ErrorMessage.vue'
import Loading from '@/components/Loading.vue'
export default defineComponent({
name: 'HomeView',
components: {
ErrorMessage,
Loading
},
setup() {
const getCacheKeysTask = useAsyncTask<string[], []>(async () => {
return await API.getCacheKeys()
})
onMounted(async () => {
await getCacheKeysTask.perform()
})
return { getCacheKeysTask }
}
})
</script>
1 change: 1 addition & 0 deletions frontend/src/components/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<div class="navbar-start"></div>
<div class="navbar-end">
<router-link class="navbar-item" :to="{ name: 'Home' }">Home</router-link>
<router-link class="navbar-item" :to="{ name: 'Cache' }">Cache</router-link>
<a class="navbar-item"><a href="/docs" target="_blank" class="navbar-item">API</a></a>
<a class="navbar-item"
><a href="https://github.com/ninoseki/eml_analyzer" target="_blank" class="navbar-item"
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router'

import Cache from '@/views/Cache.vue'
import Home from '@/views/Home.vue'
import Lookup from '@/views/Lookup.vue'

Expand All @@ -14,6 +15,11 @@ const routes: Array<RouteRecordRaw> = [
name: 'Lookup',
component: Lookup,
props: true
},
{
path: '/cache',
name: 'Cache',
component: Cache
}
]

Expand Down
15 changes: 15 additions & 0 deletions frontend/src/views/Cache.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<Cache />
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import Cache from '@/components/Cache.vue'
export default defineComponent({
name: 'CacheView',
components: { Cache },
setup() {}
})
</script>

0 comments on commit b420992

Please sign in to comment.