| 1 |
import time |
| 2 |
from datetime import datetime |
| 3 |
|
| 4 |
from beaker.cache import CacheManager, cache_region, region_invalidate |
| 5 |
from beaker import util |
| 6 |
from unittest import SkipTest |
| 7 |
|
| 8 |
defaults = {'cache.data_dir':'./cache', 'cache.type':'dbm', 'cache.expire': 2} |
| 9 |
|
| 10 |
def teardown_module(): |
| 11 |
import shutil |
| 12 |
shutil.rmtree('./cache', True) |
| 13 |
|
| 14 |
@cache_region('short_term') |
| 15 |
def fred(x): |
| 16 |
return time.time() |
| 17 |
|
| 18 |
@cache_region('short_term') |
| 19 |
def george(x): |
| 20 |
return time.time() |
| 21 |
|
| 22 |
@cache_region('short_term') |
| 23 |
def albert(x): |
| 24 |
"""A doc string""" |
| 25 |
return time.time() |
| 26 |
|
| 27 |
@cache_region('short_term') |
| 28 |
def alfred(x, xx, y=None): |
| 29 |
return str(time.time()) + str(x) + str(xx) + str(y) |
| 30 |
|
| 31 |
class AlfredCacher(object): |
| 32 |
@cache_region('short_term') |
| 33 |
def alfred_self(self, xx, y=None): |
| 34 |
return str(time.time()) + str(self) + str(xx) + str(y) |
| 35 |
|
| 36 |
try: |
| 37 |
from .annotated_functions import AnnotatedAlfredCacher |
| 38 |
except (ImportError, SyntaxError): |
| 39 |
AnnotatedAlfredCacher = None |
| 40 |
|
| 41 |
|
| 42 |
def make_cache_obj(**kwargs): |
| 43 |
opts = defaults.copy() |
| 44 |
opts.update(kwargs) |
| 45 |
cache = CacheManager(**util.parse_cache_config_options(opts)) |
| 46 |
return cache |
| 47 |
|
| 48 |
def make_cached_func(**opts): |
| 49 |
cache = make_cache_obj(**opts) |
| 50 |
@cache.cache() |
| 51 |
def load(person): |
| 52 |
now = datetime.now() |
| 53 |
return "Hi there %s, its currently %s" % (person, now) |
| 54 |
return cache, load |
| 55 |
|
| 56 |
def make_region_cached_func(): |
| 57 |
opts = {} |
| 58 |
opts['cache.regions'] = 'short_term, long_term' |
| 59 |
opts['cache.short_term.expire'] = '2' |
| 60 |
cache = make_cache_obj(**opts) |
| 61 |
|
| 62 |
@cache_region('short_term', 'region_loader') |
| 63 |
def load(person): |
| 64 |
now = datetime.now() |
| 65 |
return "Hi there %s, its currently %s" % (person, now) |
| 66 |
return load |
| 67 |
|
| 68 |
def make_region_cached_func_2(): |
| 69 |
opts = {} |
| 70 |
opts['cache.regions'] = 'short_term, long_term' |
| 71 |
opts['cache.short_term.expire'] = '2' |
| 72 |
cache = make_cache_obj(**opts) |
| 73 |
|
| 74 |
@cache_region('short_term') |
| 75 |
def load_person(person): |
| 76 |
now = datetime.now() |
| 77 |
return "Hi there %s, its currently %s" % (person, now) |
| 78 |
return load_person |
| 79 |
|
| 80 |
def test_check_region_decorator(): |
| 81 |
func = make_region_cached_func() |
| 82 |
result = func('Fred') |
| 83 |
assert 'Fred' in result |
| 84 |
|
| 85 |
result2 = func('Fred') |
| 86 |
assert result == result2 |
| 87 |
|
| 88 |
result3 = func('George') |
| 89 |
assert 'George' in result3 |
| 90 |
result4 = func('George') |
| 91 |
assert result3 == result4 |
| 92 |
|
| 93 |
time.sleep(2) |
| 94 |
result2 = func('Fred') |
| 95 |
assert result != result2 |
| 96 |
|
| 97 |
def test_different_default_names(): |
| 98 |
result = fred(1) |
| 99 |
time.sleep(0.1) |
| 100 |
result2 = george(1) |
| 101 |
assert result != result2 |
| 102 |
|
| 103 |
def test_check_invalidate_region(): |
| 104 |
func = make_region_cached_func() |
| 105 |
result = func('Fred') |
| 106 |
assert 'Fred' in result |
| 107 |
|
| 108 |
result2 = func('Fred') |
| 109 |
assert result == result2 |
| 110 |
region_invalidate(func, None, 'region_loader', 'Fred') |
| 111 |
|
| 112 |
result3 = func('Fred') |
| 113 |
assert result3 != result2 |
| 114 |
|
| 115 |
result2 = func('Fred') |
| 116 |
assert result3 == result2 |
| 117 |
|
| 118 |
|
| 119 |
region_invalidate(func, None, 'region_loader', 'Fredd') |
| 120 |
assert result3 == result2 |
| 121 |
|
| 122 |
|
| 123 |
def test_check_invalidate_region_2(): |
| 124 |
func = make_region_cached_func_2() |
| 125 |
result = func('Fred') |
| 126 |
assert 'Fred' in result |
| 127 |
|
| 128 |
result2 = func('Fred') |
| 129 |
assert result == result2 |
| 130 |
region_invalidate(func, None, 'Fred') |
| 131 |
|
| 132 |
result3 = func('Fred') |
| 133 |
assert result3 != result2 |
| 134 |
|
| 135 |
result2 = func('Fred') |
| 136 |
assert result3 == result2 |
| 137 |
|
| 138 |
|
| 139 |
region_invalidate(func, None, 'Fredd') |
| 140 |
assert result3 == result2 |
| 141 |
|
| 142 |
def test_invalidate_cache(): |
| 143 |
cache, func = make_cached_func() |
| 144 |
val = func('foo') |
| 145 |
time.sleep(0.1) |
| 146 |
val2 = func('foo') |
| 147 |
assert val == val2 |
| 148 |
|
| 149 |
cache.invalidate(func, 'foo') |
| 150 |
val3 = func('foo') |
| 151 |
assert val3 != val |
| 152 |
|
| 153 |
def test_class_key_cache(): |
| 154 |
cache = make_cache_obj() |
| 155 |
|
| 156 |
class Foo(object): |
| 157 |
@cache.cache('method') |
| 158 |
def go(self, x, y): |
| 159 |
return "hi foo" |
| 160 |
|
| 161 |
@cache.cache('standalone') |
| 162 |
def go(x, y): |
| 163 |
return "hi standalone" |
| 164 |
|
| 165 |
x = Foo().go(1, 2) |
| 166 |
y = go(1, 2) |
| 167 |
|
| 168 |
ns = go._arg_namespace |
| 169 |
assert cache.get_cache(ns).get('method 1 2') == x |
| 170 |
assert cache.get_cache(ns).get('standalone 1 2') == y |
| 171 |
|
| 172 |
def test_func_namespace(): |
| 173 |
def go(x, y): |
| 174 |
return "hi standalone" |
| 175 |
|
| 176 |
assert 'test_cache_decorator' in util.func_namespace(go) |
| 177 |
assert util.func_namespace(go).endswith('go') |
| 178 |
|
| 179 |
def test_class_key_region(): |
| 180 |
opts = {} |
| 181 |
opts['cache.regions'] = 'short_term' |
| 182 |
opts['cache.short_term.expire'] = '2' |
| 183 |
cache = make_cache_obj(**opts) |
| 184 |
|
| 185 |
class Foo(object): |
| 186 |
@cache_region('short_term', 'method') |
| 187 |
def go(self, x, y): |
| 188 |
return "hi foo" |
| 189 |
|
| 190 |
@cache_region('short_term', 'standalone') |
| 191 |
def go(x, y): |
| 192 |
return "hi standalone" |
| 193 |
|
| 194 |
x = Foo().go(1, 2) |
| 195 |
y = go(1, 2) |
| 196 |
ns = go._arg_namespace |
| 197 |
assert cache.get_cache_region(ns, 'short_term').get('method 1 2') == x |
| 198 |
assert cache.get_cache_region(ns, 'short_term').get('standalone 1 2') == y |
| 199 |
|
| 200 |
def test_classmethod_key_region(): |
| 201 |
opts = {} |
| 202 |
opts['cache.regions'] = 'short_term' |
| 203 |
opts['cache.short_term.expire'] = '2' |
| 204 |
cache = make_cache_obj(**opts) |
| 205 |
|
| 206 |
class Foo(object): |
| 207 |
@classmethod |
| 208 |
@cache_region('short_term', 'method') |
| 209 |
def go(cls, x, y): |
| 210 |
return "hi" |
| 211 |
|
| 212 |
x = Foo.go(1, 2) |
| 213 |
ns = Foo.go._arg_namespace |
| 214 |
assert cache.get_cache_region(ns, 'short_term').get('method 1 2') == x |
| 215 |
|
| 216 |
def test_class_key_region_invalidate(): |
| 217 |
opts = {} |
| 218 |
opts['cache.regions'] = 'short_term' |
| 219 |
opts['cache.short_term.expire'] = '2' |
| 220 |
cache = make_cache_obj(**opts) |
| 221 |
|
| 222 |
class Foo(object): |
| 223 |
@cache_region('short_term', 'method') |
| 224 |
def go(self, x, y): |
| 225 |
now = datetime.now() |
| 226 |
return "hi %s" % now |
| 227 |
|
| 228 |
def invalidate(self, x, y): |
| 229 |
region_invalidate(self.go, None, "method", x, y) |
| 230 |
|
| 231 |
x = Foo().go(1, 2) |
| 232 |
time.sleep(0.1) |
| 233 |
y = Foo().go(1, 2) |
| 234 |
Foo().invalidate(1, 2) |
| 235 |
z = Foo().go(1, 2) |
| 236 |
|
| 237 |
assert x == y |
| 238 |
assert x != z |
| 239 |
|
| 240 |
def test_check_region_decorator_keeps_docstring_and_name(): |
| 241 |
result = albert(1) |
| 242 |
time.sleep(0.1) |
| 243 |
result2 = albert(1) |
| 244 |
assert result == result2 |
| 245 |
|
| 246 |
assert albert.__doc__ == "A doc string" |
| 247 |
assert albert.__name__ == "albert" |
| 248 |
|
| 249 |
|
| 250 |
def test_check_region_decorator_with_kwargs(): |
| 251 |
result = alfred(1, xx=5, y=3) |
| 252 |
time.sleep(0.1) |
| 253 |
|
| 254 |
result2 = alfred(1, y=3, xx=5) |
| 255 |
assert result == result2 |
| 256 |
|
| 257 |
result3 = alfred(1, 5, y=5) |
| 258 |
assert result != result3 |
| 259 |
|
| 260 |
result4 = alfred(1, 5, 3) |
| 261 |
assert result == result4 |
| 262 |
|
| 263 |
result5 = alfred(1, 5, y=3) |
| 264 |
assert result == result5 |
| 265 |
|
| 266 |
|
| 267 |
def test_check_region_decorator_with_kwargs_and_self(): |
| 268 |
a1 = AlfredCacher() |
| 269 |
a2 = AlfredCacher() |
| 270 |
|
| 271 |
result = a1.alfred_self(xx=5, y='blah') |
| 272 |
time.sleep(0.1) |
| 273 |
|
| 274 |
result2 = a2.alfred_self(y='blah', xx=5) |
| 275 |
assert result == result2 |
| 276 |
|
| 277 |
result3 = a2.alfred_self(5, y=5) |
| 278 |
assert result != result3 |
| 279 |
|
| 280 |
result4 = a2.alfred_self(5, 'blah') |
| 281 |
assert result == result4 |
| 282 |
|
| 283 |
result5 = a2.alfred_self(5, y='blah') |
| 284 |
assert result == result5 |
| 285 |
|
| 286 |
result6 = a2.alfred_self(6, 'blah') |
| 287 |
assert result != result6 |
| 288 |
|
| 289 |
|
| 290 |
def test_check_region_decorator_with_kwargs_self_and_annotations(): |
| 291 |
if AnnotatedAlfredCacher is None: |
| 292 |
raise SkipTest('Python version not supporting annotations') |
| 293 |
|
| 294 |
a1 = AnnotatedAlfredCacher() |
| 295 |
a2 = AnnotatedAlfredCacher() |
| 296 |
|
| 297 |
result = a1.alfred_self(xx=5, y='blah') |
| 298 |
time.sleep(0.1) |
| 299 |
|
| 300 |
result2 = a2.alfred_self(y='blah', xx=5) |
| 301 |
assert result == result2 |
| 302 |
|
| 303 |
result3 = a2.alfred_self(5, y=5) |
| 304 |
assert result != result3 |
| 305 |
|
| 306 |
result4 = a2.alfred_self(5, 'blah') |
| 307 |
assert result == result4 |
| 308 |
|
| 309 |
result5 = a2.alfred_self(5, y='blah') |
| 310 |
assert result == result5 |
| 311 |
|
| 312 |
result6 = a2.alfred_self(6, 'blah') |
| 313 |
assert result != result6 |