Skip to content

Commit

Permalink
Use redis to store the role cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLovering committed Jul 17, 2024
1 parent 5023467 commit 46af39a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 37 deletions.
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

0 comments on commit 46af39a

Please sign in to comment.