如何在Django缓存中获得剩余的超时时间

问题描述 投票:1回答:1

我在允许用户添加文章的视图上限制发布请求。我目前正在使用这样的装饰器:

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

我想在消息中显示超时剩余的秒数。

谢谢

django memcached django-cache
1个回答
0
投票

没有,这是不可能的,如果需要,您必须将值缓存到另一个内存,例如redis或其他,

© www.soinside.com 2019 - 2024. All rights reserved.