如何使用 django-cacheops 使视图缓存失效

How to invalidate a view cache using django-cacheops

我有一个视图并缓存了它 在 views.py 中使用 django-cacheops (https://github.com/Suor/django-cacheops):

@cached_view(timeout=60*15)
@csrf_exempt
def order(request, usr):
    ...

urls.py中订单视图的正则表达式:

url(r'^order/(?P<usr>\D+)$', views.order, name='ord')
# Example Url: http://127.0.0.1:8000/order/demo (demo is the user name)

我想使下面视图中的缓存视图order无效:

@login_required 
def available(request, pk, avail):
    pk = int(pk)
    avail = strtobool(avail)
    if avail:
        Product.objects.filter(id = pk).update(available = True)
    else:
        Product.objects.filter(id = pk).update(available = False)
    return HttpResponseRedirect(reverse_lazy('yc')) 

根据文档,我们可以通过以下方式实现:

@login_required 
def available(request, pk, avail):
    pk = int(pk)
    avail = strtobool(avail)
    if avail:
        Product.objects.filter(id = pk).update(available = True)
        order.invalidate("http://127.0.0.1:8000/order/demo", "demo")
        #it's a dummy url I've handled it dynamically in my code
    else:
        Product.objects.filter(id = pk).update(available = False)
        order.invalidate("http://127.0.0.1:8000/order/demo", "demo")
        #it's a dummy url I've handled it dynamically in my code
    return HttpResponseRedirect(reverse_lazy('yc')) 

但它不起作用。 这是我使用 redis-cli monitor:

的日志
1647434341.849096 [1 [::1]:59650] "GET" "c:af687d461ec8bb3c48f6392010e54778"
1647434341.866966 [1 [::1]:59650] "SETEX" "c:af687d461ec8bb3c48f6392010e54778" "900" "\x80\x04\x95\xfa\b\x00\x00\x00\x00\x00\x00\x8c\x14django.http.response\x94\x8c\x0cHttpResponse\x94\x93\x94)\x81\x94}\x94(\x8c\b_headers\x94}\x94\x8c\x0ccontent-type\x94\x8c\x0cContent-Type\x94\x8c\x18text/html; charset=utf-8\x94\x86\x94s\x8c\x11_closable_objects\x94]\x94\x8c\x0e_handler_class\x94N\x8c\acookies\x94\x8c\x0chttp.cookies\x94\x8c\x0cSimpleCookie\x94\x93\x94)\x81\x94\x8c\x06closed\x94\x89\x8c\x0e_reason_phrase\x94N\x8c\b_charset\x94N\x8c\n_container\x94]\x94B\xed\a\x00\x00<!DOCTYPE html>\n\n<html>\n  <head>\n    <meta charset=\"utf-8\">\n    <title>Buy Products</title>\n    <link href=\"https://fonts.googleapis.com/css?family=Peralta\" rel=\"stylesheet\">\n    <link rel=\"stylesheet\" href=\"/static/css/bootstrap.min.css\">\n    <link rel=\"stylesheet\" href=\"/static/css/app.css\">\n  </head>\n  <body>\n      <div class=\"wrapper\">\n        <div class=\"container\">\n          <ol class=\"breadcrumb my-4\">\n            <li class=\"breadcrumb-item active\" style=\"color: #000;\">Buy Products</li>\n          </ol>\n          <form method=\"post\">\n            <!-- <input type=\"hidden\" name=\"csrfmiddlewaretoken\" value=\"SnsBnyPIwIDejqctR7TMNkITcSafgwiydwsyIiAKQkiSvr3nFA0cm1Tf3Mk6JTPj\"> -->\n            <p><label for=\"id_name\">Name:</label> <select name=\"name\" id=\"id_name\">\n  <option value=\"Redmi note 5\">Product Name: Redmi note 5 \n MRP: 100000 \n  Discounted Price: 45678 \n Description: It&#39;s good phone too</option>\n\n  <option value=\"xiomi 2\">Product Name: xiomi 2 \n MRP: 10000 \n  Discounted Price: 200 \n Description: xyz</option>\n\n  <option value=\"mouse\">Product Name: mouse \n MRP: 1400 \n  Discounted Price: 200 \n Description: xyzat</option>\n\n</select></p>\n<p><label for=\"id_user_name\">User name:</label> <textarea name=\"user_name\" cols=\"40\" rows=\"1\" maxlength=\"30\" required id=\"id_user_name\">\n</textarea></p>\n<p><label for=\"id_adress\">Adress:</label> <textarea name=\"adress\" cols=\"40\" rows=\"2\" maxlength=\"4000\" required id=\"id_adress\">\n</textarea></p>\n<p><label for=\"id_mobile\">Mobile:</label> <textarea name=\"mobile\" cols=\"40\" rows=\"1\" maxlength=\"10\" required id=\"id_mobile\">\n</textarea></p>\n<p><label for=\"id_qty\">Qty:</label> <input type=\"number\" name=\"qty\" required id=\"id_qty\"></p>\n            <button type=\"submit\" class=\"btn btn-success\">Buy</button>\n          </form>\n        </div>\n        <div class=\"push\"></div>\n      </div>\n    <script src=\"/static/js/jquery-3.2.1.min.js\"></script>\n    <script src=\"/static/js/popper.min.js\"></script>\n    <script src=\"/static/js/bootstrap.min.js\"></script>\n  </body>\n</html>\n\x94aub."
1647434354.133804 [1 [::1]:59650] "DEL" "c:94c7a9e7f6c7a45ee645caa02f53d000"

它似乎正在删除一些其他缓存。

我也在django-cache的repo中提出了这个问题,你可以查看它以获取更多信息:https://github.com/Suor/django-cacheops/issues/425

由于您在正则表达式中使用了命名组 usr,Django 将其作为关键字参数传递:

url(r'^order/(?P<usr>\D+)$', views.order, name='ord')

但是您正在尝试使用位置参数使缓存无效:

order.invalidate("http://127.0.0.1:8000/order/demo", "demo")

相反,使用相应的关键字参数使其无效:

order.invalidate("http://127.0.0.1:8000/order/demo", usr="demo")