| 106 | | if response: |
| 107 | | # The page data exist in the cache |
| 108 | | assert isinstance(response, HttpResponse) |
| 109 | | if request.debug: |
| 110 | | #print "Use cached page version. (key: '%s')" % self.cache_key |
| 111 | | response["from_cache"] = "yes" |
| 112 | | self.insert_page_stats(request, response) |
| 113 | | #elif settings.DEBUG: |
| 114 | | #print "Page not in cache found. (key: '%s')" % self.cache_key |
| | 114 | if response == None: |
| | 115 | # The page data doesn't exist in the cache |
| | 116 | return |
| | 117 | |
| | 118 | # The page data exist in the cache |
| | 119 | assert isinstance(response, HttpResponse) |
| | 120 | request._from_cache = True |
| | 121 | |
| | 122 | if request.debug: |
| | 123 | # Add the cache debug information. |
| | 124 | response = self._insert_cache_info(request, response, True) |
| 136 | | def cache_response(self, request, response): |
| 137 | | """ |
| 138 | | Cache the given response. |
| 139 | | """ |
| 140 | | # Add cache info headers to the response object |
| 141 | | self.patch_response_headers(request, response) |
| 142 | | |
| 143 | | # Save the page into the cache |
| 144 | | cache.set(self.cache_key, response, CACHE_TIMEOUT) |
| 145 | | |
| 146 | | |
| 147 | | def patch_response_headers(self, request, response): |
| 148 | | """ |
| 149 | | Adds some useful response headers for the browser cache to the given |
| 150 | | HttpResponse object. |
| 151 | | Based on django.utils.cache.patch_response_headers() but here we use |
| 152 | | the original page last update time. |
| 153 | | """ |
| 154 | | # The the original page last update time |
| 155 | | context = request.CONTEXT |
| 156 | | current_page_obj = context["PAGE"] |
| 157 | | lastupdatetime = current_page_obj.lastupdatetime |
| 158 | | |
| 159 | | response['ETag'] = md5.new(self.cache_key).hexdigest() |
| 160 | | response['Last-Modified'] = lastupdatetime.strftime(DATE_FORMAT) |
| 161 | | now = datetime.datetime.utcnow() |
| 162 | | expires = now + datetime.timedelta(0, CACHE_TIMEOUT) |
| 163 | | response['Expires'] = expires.strftime(DATE_FORMAT) |
| 164 | | |
| 165 | | |
| 166 | | def insert_page_stats(self, request, response): |
| 167 | | """ |
| 168 | | calculate the statistic and replace it into the html page. |
| 169 | | """ |
| 170 | | # Put only the statistic into HTML pages |
| 171 | | if not "html" in response._headers["content-type"][1]: |
| 172 | | # No HTML Page -> do nothing |
| 173 | | return response |
| 174 | | |
| 175 | | # compute the db time for the queries just run |
| 176 | | # FIXME: In my shared webhosting environment the queries is always = 0 |
| 177 | | queries = len(connection.queries) - self.old_queries |
| 178 | | |
| 179 | | total_time = human_duration(time.time() - self.start_time) |
| 180 | | overall_time = human_duration(time.time() - start_overall) |
| 181 | | |
| 182 | | # replace the comment if found |
| 183 | | stat_info = FMT % { |
| 184 | | 'total_time' : total_time, |
| 185 | | 'overall_time' : overall_time, |
| 186 | | 'queries' : queries, |
| 187 | | } |
| | 150 | def _insert_cache_info(self, request, response, is_from_cache): |
| | 151 | """ |
| | 152 | Add the cache debug information. |
| | 153 | |
| | 154 | if request.debug == True, we added the information if the response |
| | 155 | was from the cache or not. We "added" the information after the |
| | 156 | pagestats >TAG< ;) |
| | 157 | """ |
| | 158 | if request._from_cache == True: |
| | 159 | response_msg = CACHED_INFO_YES |
| | 160 | response["from_cache"] = "yes" |
| | 161 | elif request._from_cache == False: |
| | 162 | response_msg = CACHED_INFO_NO |
| | 163 | response["from_cache"] = "no" |
| | 164 | else: |
| | 165 | raise AssertionError("wrong request._from_cache info.") |
| | 185 | |
| | 186 | |
| | 187 | def _cache_response(self, request, response): |
| | 188 | """ |
| | 189 | Cache the given response. |
| | 190 | """ |
| | 191 | # Add cache info headers to the response object |
| | 192 | self._patch_response_headers(request, response) |
| | 193 | |
| | 194 | # Save the page into the cache |
| | 195 | cache.set(self.cache_key, response, CACHE_TIMEOUT) |
| | 196 | |
| | 197 | |
| | 198 | def _patch_response_headers(self, request, response): |
| | 199 | """ |
| | 200 | Adds some useful response headers for the browser cache to the given |
| | 201 | HttpResponse object. |
| | 202 | Based on django.utils.cache.patch_response_headers() but here we use |
| | 203 | the original page last update time. |
| | 204 | """ |
| | 205 | # The the original page last update time |
| | 206 | context = request.CONTEXT |
| | 207 | current_page_obj = context["PAGE"] |
| | 208 | lastupdatetime = current_page_obj.lastupdatetime |
| | 209 | |
| | 210 | response['ETag'] = md5.new(self.cache_key).hexdigest() |
| | 211 | response['Last-Modified'] = lastupdatetime.strftime(DATE_FORMAT) |
| | 212 | now = datetime.datetime.utcnow() |
| | 213 | expires = now + datetime.timedelta(0, CACHE_TIMEOUT) |
| | 214 | response['Expires'] = expires.strftime(DATE_FORMAT) |