davegri
davegri

Reputation: 2286

How to get remaining timeout in Django cache

I'm throttling the post requests on a view that lets users add articles. I'm currently using a decorator like so:

def throttle_post(func, duration=60):
    def inner(request, *args, **kwargs):
        if request.method == 'POST':
            remote_addr = request.META.get('HTTP_X_FORWARDED_FOR') or \
                          request.META.get('REMOTE_ADDR')
            key = '%s.%s' % (remote_addr, request.get_full_path())
            if cache.get(key):
                messages.add_message(
                request, messages.ERROR, 'you cant add articles so fast, try waiting another {} seconds.format(remaining))
                return redirect(request.META.get('HTTP_REFERER'))
            else:
                cache.set(key, 1, duration)
        return func(request, *args, **kwargs)
return inner

I want to display in the message how many seconds are remaining for the timeout.

thanks

Upvotes: 2

Views: 1678

Answers (1)

Havel
Havel

Reputation: 31

no it is not possible to do, if you want it you have to cache the value to another memory like redis or etc

Upvotes: 1

Related Questions