Skip to content

Commit

Permalink
Added a new test and fixed up @classmethod decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
thadeusb committed Apr 21, 2014
1 parent 35b5652 commit e688d0e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
5 changes: 3 additions & 2 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Changelog
---------

Version 0.xx 2013-xx-xx
Version 0.13 2014-04-21
```````````````````````

- Port to Python >= 3.3 (requiring Python 2.6/2.7 for 2.x).
Use same method as in Flask, reusing code from flask._compat module.
- Fixed bug with using per-memoize timeouts greater than the default timeout
- Added better support for per-instance memoization.
- Various bug fixes

Version 0.12 2013-04-29
```````````````````````
Expand Down
14 changes: 12 additions & 2 deletions flask_cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def function_namespace(f, args=None):
m_args = inspect.getargspec(f)[0]
instance_token = None

if getattr(f, '__self__', None):
instance_self = getattr(f, '__self__', None)

if instance_self \
and not inspect.isclass(instance_self):
instance_token = repr(f.__self__)
elif m_args \
and m_args[0] == 'self' \
Expand All @@ -57,7 +60,14 @@ def function_namespace(f, args=None):
if hasattr(f, '__qualname__'):
name = f.__qualname__
else:
klass = getattr(f, 'im_class', getattr(f, '__self__', None))
klass = getattr(f, '__self__', None)

if klass \
and not inspect.isclass(klass):
klass = klass.__class__

if not klass:
klass = getattr(f, 'im_class', None)

if not klass:
if m_args and args:
Expand Down
2 changes: 1 addition & 1 deletion flask_cache/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def gaememcached(app, config, args, kwargs):
return GAEMemcachedCache(*args, **kwargs)

def filesystem(app, config, args, kwargs):
args.append(config['CACHE_DIR'])
args.insert(0, config['CACHE_DIR'])
kwargs.update(dict(threshold=config['CACHE_THRESHOLD']))
return FileSystemCache(*args, **kwargs)

Expand Down
73 changes: 48 additions & 25 deletions test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,42 +350,65 @@ def add(self, b):
assert adder1.add(3) != adder2.add(3)

def test_10d_classfunc_memoize_delete(self):
class Adder(object):
def __init__(self, initial):
self.initial = initial
with self.app.test_request_context():
class Adder(object):
def __init__(self, initial):
self.initial = initial

@self.cache.memoize()
def add(self, b):
return self.initial + b + random.random()
@self.cache.memoize()
def add(self, b):
return self.initial + b + random.random()

adder1 = Adder(1)
adder2 = Adder(2)
adder1 = Adder(1)
adder2 = Adder(2)

a1 = adder1.add(3)
a2 = adder2.add(3)
a1 = adder1.add(3)
a2 = adder2.add(3)

assert a1 != a2
assert adder1.add(3) == a1
assert adder2.add(3) == a2
assert a1 != a2
assert adder1.add(3) == a1
assert adder2.add(3) == a2

self.cache.delete_memoized(adder1.add)
self.cache.delete_memoized(adder1.add)

a3 = adder1.add(3)
a4 = adder2.add(3)
a3 = adder1.add(3)
a4 = adder2.add(3)

self.assertNotEqual(a1, a3)
assert a1 != a3
self.assertEqual(a2, a4)
self.assertNotEqual(a1, a3)
assert a1 != a3
self.assertEqual(a2, a4)

self.cache.delete_memoized(Adder.add)
self.cache.delete_memoized(Adder.add)

a5 = adder1.add(3)
a6 = adder2.add(3)

self.assertNotEqual(a5, a6)
self.assertNotEqual(a3, a5)
self.assertNotEqual(a4, a6)

def test_10e_delete_memoize_classmethod(self):
with self.app.test_request_context():
class Mock(object):
@classmethod
@self.cache.memoize(5)
def big_foo(cls, a, b):
return a+b+random.randrange(0, 100000)

result = Mock.big_foo(5, 2)
result2 = Mock.big_foo(5, 3)

time.sleep(1)

a5 = adder1.add(3)
a6 = adder2.add(3)
assert Mock.big_foo(5, 2) == result
assert Mock.big_foo(5, 2) == result
assert Mock.big_foo(5, 3) != result
assert Mock.big_foo(5, 3) == result2

self.assertNotEqual(a5, a6)
self.assertNotEqual(a3, a5)
self.assertNotEqual(a4, a6)
self.cache.delete_memoized(Mock.big_foo)

assert Mock.big_foo(5, 2) != result
assert Mock.big_foo(5, 3) != result2

def test_11_cache_key_property(self):
@self.app.route('/')
Expand Down

0 comments on commit e688d0e

Please sign in to comment.