Per-request cache in Django?

前端 未结 7 1329
余生分开走
余生分开走 2020-12-08 11:39

I would like to implement a decorator that provides per-request caching to any method, not just views. Here is an example use case.

I have a custom ta

7条回答
  •  一生所求
    2020-12-08 12:10

    You can always do the caching manually.

        ...
        if "get_favorites" in request.POST:
            favorites = request.POST["get_favorites"]
        else:
            from django.core.cache import cache
    
            favorites = cache.get(request.user.username)
            if not favorites:
                favorites = get_favorites(request.user)
                cache.set(request.user.username, favorites, seconds)
        ...
    

提交回复
热议问题