Changeset 821
- Timestamp:
- 10/10/05 08:56:39 (3 years ago)
- Files:
-
- django/trunk/django/core/cache.py (modified) (19 diffs)
- django/trunk/django/middleware/cache.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/cache.py
r701 r821 16 16 on localhost port 11211. 17 17 18 db://tablename/ A database backend in a table named 18 db://tablename/ A database backend in a table named 19 19 "tablename". This table should be created 20 20 with "django-admin createcachetable". … … 27 27 testing. Note that this cache backend is 28 28 NOT threadsafe! 29 29 30 30 locmem:/// A more sophisticaed local memory cache; 31 31 this is multi-process- and thread-safe. … … 73 73 74 74 class _Cache: 75 76 75 def __init__(self, params): 77 76 timeout = params.get('timeout', 300) … … 133 132 else: 134 133 class _MemcachedCache(_Cache): 135 """Memcached cache backend.""" 136 134 "Memcached cache backend." 137 135 def __init__(self, server, params): 138 136 _Cache.__init__(self, params) … … 162 160 163 161 class _SimpleCache(_Cache): 164 """Simple single-process in-memory cache""" 165 162 "Simple single-process in-memory cache." 166 163 def __init__(self, host, params): 167 164 _Cache.__init__(self, params) … … 231 228 except ImportError: 232 229 import pickle 230 import copy 233 231 from django.utils.synch import RWLock 234 232 235 233 class _LocMemCache(_SimpleCache): 236 """Thread-safe in-memory cache""" 237 234 "Thread-safe in-memory cache." 238 235 def __init__(self, host, params): 239 236 _SimpleCache.__init__(self, host, params) … … 251 248 should_delete = True 252 249 else: 253 return self._cache[key]250 return copy.deepcopy(self._cache[key]) 254 251 finally: 255 252 self._lock.reader_leaves() … … 262 259 finally: 263 260 self._lock.writer_leaves() 264 261 265 262 def set(self, key, value, timeout=None): 266 263 self._lock.writer_enters() … … 269 266 finally: 270 267 self._lock.writer_leaves() 271 268 272 269 def delete(self, key): 273 270 self._lock.writer_enters() … … 285 282 286 283 class _FileCache(_SimpleCache): 287 """File-based cache""" 288 284 "File-based cache." 289 285 def __init__(self, dir, params): 290 286 self._dir = dir … … 294 290 del self._cache 295 291 del self._expire_info 296 292 297 293 def get(self, key, default=None): 298 294 fname = self._key_to_file(key) … … 309 305 pass 310 306 return default 311 307 312 308 def set(self, key, value, timeout=None): 313 309 fname = self._key_to_file(key) … … 328 324 except (IOError, OSError): 329 325 raise 330 326 331 327 def delete(self, key): 332 328 try: … … 334 330 except (IOError, OSError): 335 331 pass 336 332 337 333 def has_key(self, key): 338 334 return os.path.exists(self._key_to_file(key)) 339 335 340 336 def _cull(self, filelist): 341 337 if self.cull_frequency == 0: … … 349 345 pass 350 346 351 def _createdir(self): 347 def _createdir(self): 352 348 try: 353 349 os.makedirs(self._dir) … … 367 363 368 364 class _DBCache(_Cache): 369 """SQL cache backend""" 370 365 "SQL cache backend." 371 366 def __init__(self, table, params): 372 367 _Cache.__init__(self, params) 373 368 self._table = table 374 max_entries = params.get('max_entries', 300) 375 try: 376 self._max_entries = int(max_entries) 377 except (ValueError, TypeError): 378 self._max_entries = 300 379 cull_frequency = params.get('cull_frequency', 3) 380 try: 381 self._cull_frequency = int(cull_frequency) 382 except (ValueError, TypeError): 383 self._cull_frequency = 3 384 369 max_entries = params.get('max_entries', 300) 370 try: 371 self._max_entries = int(max_entries) 372 except (ValueError, TypeError): 373 self._max_entries = 300 374 cull_frequency = params.get('cull_frequency', 3) 375 try: 376 self._cull_frequency = int(cull_frequency) 377 except (ValueError, TypeError): 378 self._cull_frequency = 3 379 385 380 def get(self, key, default=None): 386 381 cursor = db.cursor() … … 395 390 return default 396 391 return pickle.loads(base64.decodestring(row[1])) 397 392 398 393 def set(self, key, value, timeout=None): 399 394 if timeout is None: … … 418 413 else: 419 414 db.commit() 420 415 421 416 def delete(self, key): 422 417 cursor = db.cursor() 423 418 cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) 424 419 db.commit() 425 420 426 421 def has_key(self, key): 427 422 cursor = db.cursor() 428 423 cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key]) 429 424 return cursor.fetchone() is not None 430 425 431 426 def _cull(self, cursor, now): 432 427 if self._cull_frequency == 0: … … 439 434 cursor.execute("SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency]) 440 435 cursor.execute("DELETE FROM %s WHERE cache_key < %%s" % self._table, [cursor.fetchone()[0]]) 441 436 442 437 ########################################## 443 438 # Read settings and load a cache backend # django/trunk/django/middleware/cache.py
r810 r821 1 import copy2 1 from django.conf import settings 3 2 from django.core.cache import cache … … 50 49 51 50 request._cache_update_cache = False 52 return copy.copy(response)51 return response 53 52 54 53 def process_response(self, request, response):
