Merge pull request #563 from ilvalle/fix-cache
fix cache clear with regex and tests
This commit is contained in:
@@ -128,10 +128,10 @@ class CacheAbstract(object):
|
||||
Auxiliary function called by `clear` to search and clear cache entries
|
||||
"""
|
||||
r = re.compile(regex)
|
||||
for key in storage:
|
||||
for key in storage.keys():
|
||||
if r.match(str(key)):
|
||||
del storage[key]
|
||||
break
|
||||
return
|
||||
|
||||
|
||||
class CacheInRam(CacheAbstract):
|
||||
@@ -321,6 +321,8 @@ class CacheOnDisk(CacheAbstract):
|
||||
for filename in filenames:
|
||||
yield filename
|
||||
|
||||
def keys(self):
|
||||
return [filename for dirpath, dirnames, filenames in os.walk(self.folder) for filename in filenames]
|
||||
|
||||
def get(self, key, default=None):
|
||||
try:
|
||||
|
||||
@@ -37,7 +37,6 @@ class TestCache(unittest.TestCase):
|
||||
def testCacheInRam(self):
|
||||
|
||||
# defaults to mode='http'
|
||||
|
||||
cache = CacheInRam()
|
||||
self.assertEqual(cache('a', lambda: 1, 0), 1)
|
||||
self.assertEqual(cache('a', lambda: 2, 100), 1)
|
||||
@@ -66,7 +65,6 @@ class TestCache(unittest.TestCase):
|
||||
def testCacheOnDisk(self):
|
||||
|
||||
# defaults to mode='http'
|
||||
|
||||
s = Storage({'application': 'admin',
|
||||
'folder': 'applications/admin'})
|
||||
cache = CacheOnDisk(s)
|
||||
@@ -102,8 +100,14 @@ class TestCache(unittest.TestCase):
|
||||
self.assertEqual(prefix('a', lambda: 2, 100), 1)
|
||||
self.assertEqual(cache.ram('prefixa', lambda: 2, 100), 1)
|
||||
|
||||
|
||||
|
||||
def testRegex(self):
|
||||
cache = CacheInRam()
|
||||
self.assertEqual(cache('a1', lambda: 1, 0), 1)
|
||||
self.assertEqual(cache('a2', lambda: 2, 100), 2)
|
||||
cache.clear(regex=r'a*')
|
||||
self.assertEqual(cache('a1', lambda: 2, 0), 2)
|
||||
self.assertEqual(cache('a2', lambda: 3, 100), 3)
|
||||
return
|
||||
|
||||
if __name__ == '__main__':
|
||||
setUpModule() # pre-python-2.7
|
||||
|
||||
Reference in New Issue
Block a user