| 1 |
import time |
| 2 |
from datetime import datetime |
| 3 |
|
| 4 |
import shutil |
| 5 |
|
| 6 |
from beaker.cache import CacheManager, cache_regions |
| 7 |
from beaker.util import parse_cache_config_options |
| 8 |
|
| 9 |
defaults = {'cache.data_dir':'./cache', 'cache.type':'dbm', 'cache.expire': 2} |
| 10 |
|
| 11 |
def teardown_module(): |
| 12 |
import shutil |
| 13 |
shutil.rmtree('./cache', True) |
| 14 |
|
| 15 |
def make_cache_obj(**kwargs): |
| 16 |
opts = defaults.copy() |
| 17 |
opts.update(kwargs) |
| 18 |
cache = CacheManager(**parse_cache_config_options(opts)) |
| 19 |
return cache |
| 20 |
|
| 21 |
def make_region_cached_func(): |
| 22 |
global _cache_obj |
| 23 |
opts = {} |
| 24 |
opts['cache.regions'] = 'short_term, long_term' |
| 25 |
opts['cache.short_term.expire'] = '2' |
| 26 |
cache = make_cache_obj(**opts) |
| 27 |
|
| 28 |
@cache.region('short_term', 'region_loader') |
| 29 |
def load(person): |
| 30 |
now = datetime.now() |
| 31 |
return "Hi there %s, its currently %s" % (person, now) |
| 32 |
_cache_obj = cache |
| 33 |
return load |
| 34 |
|
| 35 |
def make_cached_func(): |
| 36 |
global _cache_obj |
| 37 |
cache = make_cache_obj() |
| 38 |
|
| 39 |
@cache.cache('loader') |
| 40 |
def load(person): |
| 41 |
now = datetime.now() |
| 42 |
return "Hi there %s, its currently %s" % (person, now) |
| 43 |
_cache_obj = cache |
| 44 |
return load |
| 45 |
|
| 46 |
def test_parse_doesnt_allow_none(): |
| 47 |
opts = {} |
| 48 |
opts['cache.regions'] = 'short_term, long_term' |
| 49 |
for region, params in parse_cache_config_options(opts)['cache_regions'].items(): |
| 50 |
for k, v in params.items(): |
| 51 |
assert v != 'None', k |
| 52 |
|
| 53 |
def test_parse_doesnt_allow_empty_region_name(): |
| 54 |
opts = {} |
| 55 |
opts['cache.regions'] = '' |
| 56 |
regions = parse_cache_config_options(opts)['cache_regions'] |
| 57 |
assert len(regions) == 0 |
| 58 |
|
| 59 |
def test_decorators(): |
| 60 |
for func in (make_region_cached_func, make_cached_func): |
| 61 |
check_decorator(func()) |
| 62 |
|
| 63 |
def check_decorator(func): |
| 64 |
result = func('Fred') |
| 65 |
assert 'Fred' in result |
| 66 |
|
| 67 |
result2 = func('Fred') |
| 68 |
assert result == result2 |
| 69 |
|
| 70 |
result3 = func('George') |
| 71 |
assert 'George' in result3 |
| 72 |
result4 = func('George') |
| 73 |
assert result3 == result4 |
| 74 |
|
| 75 |
time.sleep(2) |
| 76 |
result2 = func('Fred') |
| 77 |
assert result != result2 |
| 78 |
|
| 79 |
def test_check_invalidate_region(): |
| 80 |
func = make_region_cached_func() |
| 81 |
result = func('Fred') |
| 82 |
assert 'Fred' in result |
| 83 |
|
| 84 |
result2 = func('Fred') |
| 85 |
assert result == result2 |
| 86 |
_cache_obj.region_invalidate(func, None, 'region_loader', 'Fred') |
| 87 |
|
| 88 |
result3 = func('Fred') |
| 89 |
assert result3 != result2 |
| 90 |
|
| 91 |
result2 = func('Fred') |
| 92 |
assert result3 == result2 |
| 93 |
|
| 94 |
|
| 95 |
_cache_obj.region_invalidate(func, None, 'region_loader', 'Fredd') |
| 96 |
assert result3 == result2 |
| 97 |
|
| 98 |
def test_check_invalidate(): |
| 99 |
func = make_cached_func() |
| 100 |
result = func('Fred') |
| 101 |
assert 'Fred' in result |
| 102 |
|
| 103 |
result2 = func('Fred') |
| 104 |
assert result == result2 |
| 105 |
_cache_obj.invalidate(func, 'loader', 'Fred') |
| 106 |
|
| 107 |
result3 = func('Fred') |
| 108 |
assert result3 != result2 |
| 109 |
|
| 110 |
result2 = func('Fred') |
| 111 |
assert result3 == result2 |
| 112 |
|
| 113 |
|
| 114 |
_cache_obj.invalidate(func, 'loader', 'Fredd') |
| 115 |
assert result3 == result2 |
| 116 |
|
| 117 |
def test_long_name(): |
| 118 |
func = make_cached_func() |
| 119 |
name = 'Fred' * 250 |
| 120 |
result = func(name) |
| 121 |
assert name in result |
| 122 |
|
| 123 |
result2 = func(name) |
| 124 |
assert result == result2 |
| 125 |
|
| 126 |
_cache_obj.invalidate(func, 'loader', name, key_length=8000) |
| 127 |
|
| 128 |
result3 = func(name) |
| 129 |
assert result3 == result2 |
| 130 |
|
| 131 |
|
| 132 |
_cache_obj.invalidate(func, 'loader', name) |
| 133 |
result4 = func(name) |
| 134 |
assert result3 != result4 |
| 135 |
|
| 136 |
|
| 137 |
def test_cache_region_has_default_key_length(): |
| 138 |
try: |
| 139 |
cache = CacheManager(cache_regions={ |
| 140 |
'short_term_without_key_length':{ |
| 141 |
'expire': 60, |
| 142 |
'type': 'memory' |
| 143 |
} |
| 144 |
}) |
| 145 |
|
| 146 |
|
| 147 |
assert 'short_term_without_key_length' in cache_regions |
| 148 |
|
| 149 |
@cache.region('short_term_without_key_length') |
| 150 |
def load_without_key_length(person): |
| 151 |
now = datetime.now() |
| 152 |
return "Hi there %s, its currently %s" % (person, now) |
| 153 |
|
| 154 |
|
| 155 |
msg = load_without_key_length('fred') |
| 156 |
msg2 = load_without_key_length('fred') |
| 157 |
assert msg == msg2, (msg, msg2) |
| 158 |
|
| 159 |
|
| 160 |
msg3 = load_without_key_length('george') |
| 161 |
assert msg3.split(',')[-1] != msg2.split(',')[-1] |
| 162 |
|
| 163 |
finally: |
| 164 |
|
| 165 |
cache_regions.pop('short_term_without_key_length', None) |
| 166 |
|
| 167 |
|
| 168 |
def test_cache_region_expire_is_always_int(): |
| 169 |
try: |
| 170 |
cache = CacheManager(cache_regions={ |
| 171 |
'short_term_with_string_expire': { |
| 172 |
'expire': '60', |
| 173 |
'type': 'memory' |
| 174 |
} |
| 175 |
}) |
| 176 |
|
| 177 |
|
| 178 |
assert 'short_term_with_string_expire' in cache_regions |
| 179 |
|
| 180 |
@cache.region('short_term_with_string_expire') |
| 181 |
def load_with_str_expire(person): |
| 182 |
now = datetime.now() |
| 183 |
return "Hi there %s, its currently %s" % (person, now) |
| 184 |
|
| 185 |
|
| 186 |
msg = load_with_str_expire('fred') |
| 187 |
msg2 = load_with_str_expire('fred') |
| 188 |
assert msg == msg2, (msg, msg2) |
| 189 |
|
| 190 |
finally: |
| 191 |
|
| 192 |
cache_regions.pop('short_term_with_string_expire', None) |
| 193 |
|
| 194 |
|
| 195 |
def test_directory_goes_away(): |
| 196 |
cache = CacheManager(cache_regions={ |
| 197 |
'short_term_without_key_length':{ |
| 198 |
'expire': 60, |
| 199 |
'type': 'dbm', |
| 200 |
'data_dir': '/tmp/beaker-tests/cache/data', |
| 201 |
'lock_dir': '/tmp/beaker-tests/cache/lock' |
| 202 |
} |
| 203 |
}) |
| 204 |
|
| 205 |
|
| 206 |
@cache.region('short_term_without_key_length') |
| 207 |
def load_with_str_expire(person): |
| 208 |
now = datetime.now() |
| 209 |
return "Hi there %s, its currently %s" % (person, now) |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
msg = load_with_str_expire('fred') |
| 214 |
msg2 = load_with_str_expire('fred') |
| 215 |
|
| 216 |
shutil.rmtree('/tmp/beaker-tests') |
| 217 |
|
| 218 |
msg3 = load_with_str_expire('fred') |
| 219 |
assert msg == msg2, (msg, msg2) |
| 220 |
assert msg2 != msg3, (msg2, msg3) |