Django

Code

Show
Ignore:
Timestamp:
12/04/07 12:03:56 (9 months ago)
Author:
jacob
Message:

Fixed #6099: the filebased cache backend now uses md5 hashes of keys instead of sanitized filenames. For good measure, keys are partitioned into subdirectories using the first few bits of the hash. Thanks, sherbang.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/tests/regressiontests/cache/tests.py

    r6822 r6887  
    44# Uses whatever cache backend is set in the test settings file. 
    55 
    6 import time, unittest 
    7  
     6import time 
     7import unittest 
    88from django.core.cache import cache 
    99from django.utils.cache import patch_vary_headers 
     
    2828        cache.add("addkey1", "newvalue") 
    2929        self.assertEqual(cache.get("addkey1"), "value") 
    30  
     30         
    3131    def test_non_existent(self): 
    3232        # get with non-existent keys 
     
    7777 
    7878    def test_expiration(self): 
    79         # expiration 
    80         cache.set('expire', 'very quickly', 1) 
    81         time.sleep(2) 
    82         self.assertEqual(cache.get("expire"), None) 
     79        cache.set('expire1', 'very quickly', 1) 
     80        cache.set('expire2', 'very quickly', 1) 
     81        cache.set('expire3', 'very quickly', 1) 
     82 
     83        time.sleep(2)         
     84        self.assertEqual(cache.get("expire1"), None) 
     85         
     86        cache.add("expire2", "newvalue") 
     87        self.assertEqual(cache.get("expire2"), "newvalue") 
     88        self.assertEqual(cache.has_key("expire3"), False) 
    8389 
    8490    def test_unicode(self): 
     
    9399            self.assertEqual(cache.get(key), value) 
    94100 
     101import os 
     102import md5 
     103import shutil 
     104import tempfile 
     105from django.core.cache.backends.filebased import CacheClass as FileCache 
     106 
     107class FileBasedCacheTests(unittest.TestCase): 
     108    """ 
     109    Specific test cases for the file-based cache. 
     110    """ 
     111    def setUp(self): 
     112        self.dirname = tempfile.mktemp() 
     113        os.mkdir(self.dirname) 
     114        self.cache = FileCache(self.dirname, {}) 
     115         
     116    def tearDown(self): 
     117        shutil.rmtree(self.dirname) 
     118         
     119    def test_hashing(self): 
     120        """Test that keys are hashed into subdirectories correctly""" 
     121        self.cache.set("foo", "bar") 
     122        keyhash = md5.new("foo").hexdigest() 
     123        keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:]) 
     124        self.assert_(os.path.exists(keypath)) 
     125         
     126    def test_subdirectory_removal(self): 
     127        """ 
     128        Make sure that the created subdirectories are correctly removed when empty. 
     129        """ 
     130        self.cache.set("foo", "bar") 
     131        keyhash = md5.new("foo").hexdigest() 
     132        keypath = os.path.join(self.dirname, keyhash[:2], keyhash[2:4], keyhash[4:]) 
     133        self.assert_(os.path.exists(keypath)) 
     134 
     135        self.cache.delete("foo") 
     136        self.assert_(not os.path.exists(keypath)) 
     137        self.assert_(not os.path.exists(os.path.dirname(keypath))) 
     138        self.assert_(not os.path.exists(os.path.dirname(os.path.dirname(keypath)))) 
    95139 
    96140class CacheUtils(unittest.TestCase):