Django view caching: how to set expire at time?

47 views Asked by At

I would like to cache some view until end of month.

e.g.

@cache_page_expire_at_end_of_month
def some_view(request):
   ...

I found this old question Django per-view caching: set expiry time rather than cache timeout? But I can't get it to work.

1

There are 1 answers

0
Cristus Cleetus On BEST ANSWER

To cache a Django view until the end of the month, you need to create a custom decorator that calculates the remaining time until the end of the current month and then uses Django's caching mechanism with that specific timeout. Django does not have a built-in decorator for setting cache expiration to the end of the month, so you'll have to implement this functionality yourself.

Here’s a step-by-step guide on how to do it:

  1. Calculate Time Until End of Month
  2. Create Custom Decorator
  3. Apply Custom Decorator to Views

from django.utils.decorators import method_decorator

from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from datetime import datetime, timedelta

import calendar

def cache_page_expire_at_end_of_month(timeout_default=86400):
    def decorator(view_func):
        def _wrapped_view_func(*args, **kwargs):
            # Calculate the current time and find the last day of the month
            now = datetime.now()
            _, last_day = calendar.monthrange(now.year, now.month)
            end_of_month = datetime(now.year, now.month, last_day, 23, 59, 59)

            # Calculate remaining seconds until the end of the month
            delta = end_of_month - now
            timeout = max(delta.total_seconds(), timeout_default)

            # Apply the cache_page decorator with the calculated timeout
            return cache_page(timeout)(view_func)(*args, **kwargs)
        return _wrapped_view_func
    return decorator

# Usage example
@cache_page_expire_at_end_of_month()
def some_view(request):
    # Your view logic here
    ...

To apply this decorator to a class-based view, you'll need to use the method_decorator like this:

from django.utils.decorators import method_decorator

@method_decorator(cache_page_expire_at_end_of_month(), name='dispatch')
class SomeClassBasedView(View):
    def get(self, request, *args, **kwargs):
        # Your view logic here
        ...