| 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) |
|---|
| | 101 | import os |
|---|
| | 102 | import md5 |
|---|
| | 103 | import shutil |
|---|
| | 104 | import tempfile |
|---|
| | 105 | from django.core.cache.backends.filebased import CacheClass as FileCache |
|---|
| | 106 | |
|---|
| | 107 | class 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)))) |
|---|