Show
Ignore:
Timestamp:
03/27/08 16:59:44 (8 months ago)
Author:
JensDiemer
Message:

add a experimental new cache middleware
-merge pagestats into cache middleware
-add unitest for the cache
-move setup_debug() from the views into the common middleware
-update other tests

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/PyLucid/index.py

    r1507 r1510  
    1818""" 
    1919 
    20 import datetime, md5 
    21  
    2220from django.http import HttpResponse, HttpResponsePermanentRedirect, \ 
    2321                                                           HttpResponseRedirect 
     22from django.conf import settings 
    2423from django.template import RequestContext 
    25 from django.core.cache import cache 
     24from django.utils.safestring import mark_safe 
    2625from django.utils.translation import ugettext as _ 
    27 from django.utils.safestring import mark_safe 
    28 from django.conf import settings 
    29  
    30 from PyLucid import models 
    31  
     26 
     27from PyLucid.models import Page 
    3228from PyLucid.system import plugin_manager 
     29from PyLucid.system.URLs import URLs 
     30from PyLucid.system.page_msg import PageMessages 
    3331from PyLucid.system.response import SimpleStringIO 
    3432from PyLucid.system.exceptions import AccessDenied 
    35 from PyLucid.system.page_msg import PageMessages 
     33from PyLucid.system.context_processors import add_dynamic_context, add_css_tag 
    3634from PyLucid.system.detect_page import get_current_page_obj, \ 
    3735                                                            get_default_page_id 
    38 from PyLucid.system.URLs import URLs 
    39 from PyLucid.system.context_processors import add_dynamic_context, add_css_tag 
    40 from PyLucid.system.utils import setup_debug 
     36from PyLucid.tools.utils import escape, escape_django_tags 
    4137from PyLucid.tools.content_processors import apply_markup, \ 
    4238                                    render_string_template, redirect_warnings 
    43 from PyLucid.tools.utils import escape, escape_django_tags 
    4439from PyLucid.plugins_internal.page_style.page_style import replace_add_data 
    4540 
     
    135130 
    136131 
    137 def patch_response_headers(response, cache_timeout, ETag, last_modified): 
    138     """ 
    139     Adds some useful headers to the given HttpResponse object: 
    140         ETag, Last-Modified, Expires and Cache-Control 
    141  
    142     Original version: django.utils.cache.patch_response_headers() 
    143     """ 
    144     response['ETag'] = ETag 
    145     response['Last-Modified'] = last_modified.strftime( 
    146         '%a, %d %b %Y %H:%M:%S GMT' 
    147     ) 
    148     now = datetime.datetime.utcnow() 
    149     expires = now + datetime.timedelta(0, cache_timeout) 
    150     response['Expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S GMT') 
    151  
    152  
    153 def get_cached_data(url): 
    154     """ 
    155     -Build the cache_key from the given url. Use the last page shortcut. 
    156     -retuned the cache_key and the page data. 
    157     """ 
    158     if url == "": 
    159         # Request without a shortcut -> request the default page 
    160         shortcut = "/" 
    161     else: 
    162         # Note: We use append_slash, but the url pattern striped the last 
    163         #     slash out. 
    164         # e.g.: '/page1/page2/page3' -> ['/page1/page2', 'page3'] -> 'page3' 
    165         shortcut = url.rsplit("/", 1)[-1] 
    166  
    167     cache_key = settings.PAGE_CACHE_PREFIX + shortcut 
    168     #print "Used cache key:", cache_key 
    169  
    170     # Get the page data from the cache. 
    171     response = cache.get(cache_key) 
    172  
    173     return cache_key, response 
    174  
    175  
    176132def index(request, url): 
    177133    """ 
     
    181137    the page shortcut from the url. 
    182138    """ 
    183     # Cache only for anonymous users. Otherwise users how are log-in don't see 
    184     # the dynamic integrate admin menu. 
    185     use_cache = request.user.is_anonymous() 
    186  
    187     if use_cache: 
    188         # Try to get the cms page request from the cache 
    189         cache_key, response = get_cached_data(url) 
    190         if response: 
    191             # This page has been cached in the past, use the cache data: 
    192             return response 
    193  
    194     setup_debug(request) 
    195  
    196139    try: 
    197140        current_page_obj = get_current_page_obj(request, url) 
     
    211154 
    212155    context = _get_context(request, current_page_obj) 
     156 
    213157    # Get the response for the requested cms page: 
    214158    response = _render_cms_page(request, context) 
    215159 
    216     if use_cache: 
    217         # It's a anonymous user -> Cache the cms page. 
    218         cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS 
    219         # Add some headers for the browser cache 
    220         patch_response_headers( 
    221             response, cache_timeout, 
    222             ETag = md5.new(cache_key).hexdigest(), 
    223             last_modified = current_page_obj.lastupdatetime, 
    224         ) 
    225         # Save the page into the cache 
    226         cache.set(cache_key, response, cache_timeout) 
     160    if getattr(request, "_use_cache", None) == None: 
     161        # Set _use_cache information for the PyLucid cache middleware, but only 
     162        # if it was set to true or false in the past 
     163        request._use_cache = True 
    227164 
    228165    return response 
     166 
    229167 
    230168def _get_page(request, page_id): 
     
    233171    TODO: Check int(page_id)! 
    234172    """ 
    235     setup_debug(request) 
    236  
    237173    try: 
    238         current_page_obj = models.Page.objects.get(id=int(page_id)) 
    239     except models.Page.DoesNotExist: 
     174        current_page_obj = Page.objects.get(id=int(page_id)) 
     175    except Page.DoesNotExist: 
    240176        # The ID in the url is wrong -> goto the default page 
    241177        default_page_id = get_default_page_id() 
    242         current_page_obj = models.Page.objects.get(id=default_page_id) 
     178        current_page_obj = Page.objects.get(id=default_page_id) 
    243179 
    244180        user = request.user 
     
    256192 
    257193    return current_page_obj 
     194 
    258195 
    259196def handle_command(request, page_id, module_name, method_name, url_args): 
     
    329266    return HttpResponsePermanentRedirect(url) 
    330267 
     268 
    331269def permalink(request, page_id): 
    332270    """