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

feat: cache burst from admin #128

Merged
merged 1 commit into from
Jan 28, 2025
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
11 changes: 10 additions & 1 deletion backend/caviardeul/admin/article.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from datetime import timedelta

from django.contrib import admin
from asgiref.sync import async_to_sync
from django.contrib import admin, messages
from django.core.exceptions import ValidationError
from import_export.admin import ImportMixin
from import_export.fields import Field
from import_export.resources import ModelResource
from import_export.widgets import BooleanWidget

from caviardeul.models import CustomArticle, DailyArticle
from caviardeul.services.articles import burst_cache_for_article


@admin.register(CustomArticle)
Expand Down Expand Up @@ -88,3 +90,10 @@ class DailyArticleAdmin(ImportMixin, admin.ModelAdmin):
ordering = ("-id",)

resource_class = DailyArticleResource
actions = ["burst_cache"]

@admin.action(description="Burst article cache")
def burst_cache(self, request, queryset) -> None:
page_ids = list(queryset.values_list("page_id", flat=True))
async_to_sync(burst_cache_for_article)(page_ids)
self.message_user(request, "Cache burst was successful", messages.SUCCESS)
5 changes: 5 additions & 0 deletions backend/caviardeul/services/articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ async def _get_article_content_from_cache(page_id: str) -> str | None:
return await cache.aget(f"wikipedia::{page_id}")


async def burst_cache_for_article(page_ids: list[str]) -> None:
keys = [f"wikipedia::{page_id}" for page_id in page_ids]
await cache.adelete_many(keys)


async def _set_article_to_cache(page_id: str, content: str) -> None:
now = timezone.now()
tomorrow = (now + timedelta(days=1)).replace(
Expand Down