These are chat archives for django/django

Jul 2017
Anish Shrestha
Jul 20 2017 04:40
@flyboy1565 Hey are you using django-channels?
Anish Shrestha
Jul 20 2017 04:59

In the online server I am having an issue

Error during WebSocket handshake: Unexpected response code: 500

Anish Shrestha
Jul 20 2017 06:04
So, restarted the server... And it works now. Zzz :laughing:


Have you tried turning it on and off again?

makes sense.

Jul 20 2017 11:51
@annshress 😀
Anish Shrestha
Jul 20 2017 11:52
Yea :smile:
Job Matheka
Jul 20 2017 18:09

hey friends,
i am writing a Django caching middleware, that takes a list of URLs from the settings file and caches them.
e.g. in

CACHE_URLS = [('^index$', 60*60), ]

this middleware checks the request URL and caches the page for the duration specified in settings. If the page was found in the cache, return the cached result and don’t process the page.

from django.http import HttpResponse

from pawame.settings import CACHE_URLS
from django.core.cache import cache

class CacheUrls(object):
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        response = self.get_response(request)
        request_url = request.path.strip('/')
        for to_cache_url in CACHE_URLS:
            if request_url in to_cache_url[0]:
                if cache.get(request_url):
                    # use the response body
                    return HttpResponse(cache.get(request_url))
                    # cache here
                    body = response
                    url = request_url
                    time_to_cache = to_cache_url[1]
                    cache.set(url, body, time_to_cache)
        return response

This is working, but is there a better way of achieveing what i described above?
PS I am using Redis as my cache backend.