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

Use redis to store the role cache #298

Merged
merged 1 commit into from
Jul 19, 2024
Merged
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
2 changes: 1 addition & 1 deletion backend/authentication/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ async def authenticate(
if await user.fetch_admin_status(request.state.db):
scopes.append("admin")

scopes.extend(await user.get_user_roles(request.state.db))
scopes.extend(await user.get_user_roles())

return authentication.AuthCredentials(scopes), user
4 changes: 2 additions & 2 deletions backend/authentication/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def user_id(self) -> str:
def decoded_token(self) -> dict[str, any]:
return jwt.decode(self.token, SECRET_KEY, algorithms=["HS256"])

async def get_user_roles(self, database: Database) -> list[str]:
async def get_user_roles(self) -> list[str]:
"""Get a list of the user's discord roles."""
if not self.member:
return []

server_roles = await discord.get_roles(database)
server_roles = await discord.get_roles()
roles = [role.name for role in server_roles if role.id in self.member.roles]

if "admin" in roles:
Expand Down
43 changes: 11 additions & 32 deletions backend/discord.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""Various utilities for working with the Discord API."""

import datetime
import json

import httpx
import starlette.requests
from pymongo.database import Database
from starlette import exceptions

from backend import constants, models
Expand Down Expand Up @@ -66,7 +64,6 @@ async def _get_role_info() -> list[models.DiscordRole]:


async def get_roles(
database: Database,
*,
force_refresh: bool = False,
) -> list[models.DiscordRole]:
Expand All @@ -75,35 +72,17 @@ async def get_roles(

If `force_refresh` is True, the cache is skipped and the roles are updated.
"""
collection = database.get_collection("roles")

if force_refresh:
# Drop all values in the collection
await collection.delete_many({})

# `create_index` creates the index if it does not exist, or passes
# This handles TTL on role objects
await collection.create_index(
"inserted_at",
expireAfterSeconds=60 * 60 * 24, # 1 day
name="inserted_at",
)

roles = [models.DiscordRole(**json.loads(role["data"])) async for role in collection.find()]

if len(roles) == 0:
# Fetch roles from the API and insert into the database
roles = await _get_role_info()
await collection.insert_many(
{
"name": role.name,
"id": role.id,
"data": role.json(),
"inserted_at": datetime.datetime.now(tz=datetime.UTC),
}
for role in roles
)

role_cache_key = "forms-backend:role_cache"
if not force_refresh:
roles = await constants.REDIS_CLIENT.hgetall(role_cache_key)
if roles:
return [
models.DiscordRole(**json.loads(role_data)) for role_id, role_data in roles.items()
]

roles = await _get_role_info()
await constants.REDIS_CLIENT.hmset(role_cache_key, {role.id: role.json() for role in roles})
await constants.REDIS_CLIENT.expire(role_cache_key, 60 * 60 * 24) # 1 day
return roles


Expand Down
4 changes: 2 additions & 2 deletions backend/routes/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class RolesResponse(pydantic.BaseModel):
resp=Response(HTTP_200=RolesResponse),
tags=["roles"],
)
async def patch(self, request: Request) -> JSONResponse:
async def patch(self, request: Request) -> JSONResponse: # noqa: ARG002 Request is required by @requires
"""Refresh the roles database."""
roles = await discord.get_roles(request.state.db, force_refresh=True)
roles = await discord.get_roles(force_refresh=True)

return JSONResponse(
{"roles": [role.dict() for role in roles]},
Expand Down
Loading