Commits

Ben Bangert  committed 93b9dfc Merge

merge

  • Participants
  • Parent commits f000c58, 02b1dfc
  • Tags v1.1.2

Comments (0)

Files changed (2)

File beaker/container.py

         try:
             has_value = self.has_value()
             if has_value:
-                self.storedtime, self.expiretime, value = self.namespace[self.key]
+                value = self.__get_value()
                 if not self._is_expired():
                     return value
 
             self.namespace.acquire_read_lock()
             try:
                 if self.has_value():
-                    self.storedtime, self.expiretime, value = self.namespace[self.key]
+                    value = self.__get_value()
                     if not self._is_expired():
                         return value
             finally:
             creation_lock.release()
             debug("released create lock")
 
+    def __get_value(self):
+        value = self.namespace[self.key]
+        try:
+            self.storedtime, self.expiretime, value = value
+        except ValueError:
+            if not len(value) == 2:
+                raise
+            # Old format: upgrade
+            self.storedtime, value = value
+            self.expiretime = self.expire_argument = None
+            debug("get_value upgrading time %r expire time %r", self.storedtime, self.expire_argument)
+            self.namespace.release_read_lock()
+            self.set_value(value)
+            self.namespace.acquire_read_lock()
+        return value
+
     def set_value(self, value):
         self.namespace.acquire_write_lock()
         try:
 class DBMContainer(Container):
     namespace_class = DBMNamespaceManager
 
-DbmContainer = DBMContainer
+DbmContainer = DBMContainer

File tests/test_cache.py

 # coding: utf-8
+import os
+import shutil
+import tarfile
+import tempfile
 import time
 from beaker.middleware import CacheMiddleware
 from beaker.cache import Cache
 from webtest import TestApp
 
+# Tarball of the output of:
+# >>> from beaker.cache import Cache
+# >>> c = Cache('test', data_dir='db', type='dbm')
+# >>> c['foo'] = 'bar'
+# at [24f57102d310]
+old_cache_tar = """\
+eJzt3EtOwkAAgOEBjTHEBDfu2ekKZ6bTTnsBL+ABzPRB4osSRBMXHsNruXDl3nMYLaEbpYRAaIn6
+f8kwhFcn/APLSeNTUTdZsL4/m4Pg21wSqiCt9D1PC6mUZ7Xo+bWvrHB/N3HjXk+MrrLhQ/a48HXL
+nv+l0vg0yYcTdznMxhdpfFvHbpj1lyv0N8oq+jdhrr/b/A5Yo79R9G9ERX8XbXgLrNHfav7/G1Hd
+30XGhYPMT5JYRbELVGISGVov9SKVRaGNQj2I49TrF+8oxpJrTAMHxizob+b7ay+Y/v5lE1/AP+8v
+9o5ccdsWYvdViMPpIwdCtMRsiP3yTrucd8r5pJxbz8On9/KT2uVo3H5rG1cFAAAAAOD3aIuP7lv3
+pRjbXgkAAAAAAFjVyc1Idc6U1lYGgbSmL0Mjpe248+PYjY87I91x/UGeb3udAAAAAACgfh+fAAAA
+AADgr/t5/sPFTZ5cb/38D19Lzn9pRHX/zR4CtEZ/o+nfiEX9N3kI0Gr9vWl/W0z0BwAAAAAAAAAA
+AAAAAAAAqPAFyOvcKA==
+""".decode('base64').decode('zlib')
+
 def simple_app(environ, start_response):
     clear = False
     if environ.get('beaker.clear'):
     assert cache.get_value('x', expiretime=1, createfunc=lambda: '12', type='file', data_dir='./cache') == '10'
     
 
+def test_upgrade():
+    dir = tempfile.mkdtemp()
+    fd, name = tempfile.mkstemp(dir=dir)
+    fp = os.fdopen(fd, 'w')
+    fp.write(old_cache_tar)
+    fp.close()
+    tar = tarfile.open(name)
+    tar.extractall(dir)
+    tar.close()
+    try:
+        _test_upgrade(os.path.join(dir, 'db'))
+    finally:
+        shutil.rmtree(dir)
+
+def _test_upgrade(dir):
+    cache = Cache('test', data_dir=dir, type='dbm')
+    assert cache['foo'] == 'bar'
+    assert cache['foo'] == 'bar'