0

I'm creating a django app for comics and having a code in my views.py of an app like this:

@comic_inc
@cache_page(settings.CACHE_S) //CACHE_S = 60
@vary_on_cookie
def comic(request, com):
    try:
        cobj = Comic.gen_comic(com, request.user.id)
        if request.GET.get('sort') == '2':
            chaps = Chapter.objects.filter(published=True, comic = cobj).order_by('num','snum','volume')
        else:
            chaps = Chapter.objects.filter(published=True, comic = cobj).order_by('-num','-snum','-volume')
        return render(request, 'base/comic.html', {'comic': cobj, 'chapters':chaps})
    except Exception as e:
        return render(request, 'base/comic.html', {'error': 'Comic not found'})

the @comic_inc is a decorator I created when I was trying to implement this solution: Counting "page views" or "hits" when using cache

the decorator code is as below:

def comic_inc(view_func):
    def _wrapped(*args,**kwargs):
        Comic.objects.filter(slug=kwargs.get('com')).update(pageviews=F('pageviews')+1)
        return view_func(*args,**kwargs)
    return _wrapped

I didn't particularly specify any CACHE in django settings since there is already a default cache django.core.cache.backends.locmem.LocMemCache that works perfectly fine for development (shows up properly in debug toolbar). Additionally I did try memcached (downloaded and ran memcached -m 512 -vvv in CMD and it worked perfectly).

Either way the result was the same, the pageviews is updated only when cached page times out and only increased by 1. I don't mind that the view value is not changed for every refresh on the cached template page but I atleast want the value to be increased on the backend. I did verify if the value of pageviews is changing by refereshing about 20 times on the template and then checking the admin panel and also waiting for timeout and refreshing again, but the value didn't increase more than 1.

Please help me.

yashas123
  • 270
  • 5
  • 23

1 Answers1

1

Remove any caching-related middleware from your settings.py if you want to use the cache_page decorator like that. The middleware would be responsible for easy site-wide caching while the decorator is useful for more configurable page/view-specific caching.

Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
  • Also, I have something like a bookmark function that allows users to bookmark and follow it for updates notification kind of thing, but the cache doesn't expire and recalculate it when they bookmark it until it times out, so even after they click the bookmark button the page refreshes and yet again shows the add to bookmark button (I have set it so that if it's bookmarked the button should not show and it's tested properly), should I use partial template caching here or something else? could you just comment it or edit your answer or something? – yashas123 Feb 19 '19 at 15:48
  • Guess there are multiple ways to solve this... Cahe only parts of the template/response, disable caching for logged in users, look into the "vary on..." decorators (eg. you could use vary on cookie and set a cookie), set a value in the session and depend caching on that... – Bernhard Vallant Feb 19 '19 at 15:53
  • Can I use both per view and partial template cache on the view that is cached? – yashas123 Feb 19 '19 at 17:44