Commits

Andriy Kornatskyy  committed f0b876a

Refactoring due to eliminated cache_factory concept.

  • Participants
  • Parent commits 6e4c9d8

Comments (0)

Files changed (7)

File demos/hello/helloworld.py

 
 
 cache = MemoryCache()
-cache_factory = lambda: cache
+
 public_cache_profile = CacheProfile(
     'public', duration=timedelta(minutes=15), enabled=True)
 no_cache_profile = CacheProfile(
 ]
 
 options = {
-    'http_cache_factory': cache_factory
+    'http_cache': cache
 }
 
 main = WSGIApplication(

File demos/template/setup.py

 
 install_requires = [
     'wheezy.core>=0.1.82',
-    'wheezy.caching>=0.1.54',
+    'wheezy.caching>=0.1.61',
     'wheezy.html>=0.1.109',
-    'wheezy.http>=0.1.236',
+    'wheezy.http>=0.1.254',
     'wheezy.routing>=0.1.124',
     'wheezy.security>=0.1.36',
     'wheezy.validation>=0.1.74',

File demos/template/src/config.py

 
 if mode == 'mock':
     cache = MemoryCache()
-    cache_factory = lambda: cache
 else:
     raise NotImplementedError(mode)
 
 
 # HTTPCacheMiddleware
 options.update({
-    'http_cache_factory': cache_factory
+    'http_cache': cache
 })
 
 # Cache Profiles
         module_directory=config.get('mako', 'module-directory'),
         filesystem_checks=config.getboolean('mako', 'filesystem-checks'),
         directories=directories,
-        cache_factory=cache_factory,
+        cache=cache,
         preprocessor=[
             inline_preprocessor(directories, config.getboolean(
                 'mako', 'inline-preprocessor-fallback')),

File demos/template/src/membership/repository/caching.py

+
 """
 """
 
-from wheezy.caching import CacheDependency
-
-from config import cache_factory
+from config import cache
 from membership.repository import keys
 from membership.repository.contract import IMembershipRepository
 
 
     def has_account(self, username):
         key = keys.has_account(username)
-        #with cache_factory() as c:
-        c = cache_factory()
-        try:
-            c.__enter__()
-            result = c.get(key)
-        finally:
-            c.__exit__(None, None, None)
+        result = cache.get(key)
         if result is None:
             result = self.inner.has_account(username)
             if result is not None:
-                #with cache_factory() as c:
-                c = cache_factory()
-                try:
-                    c.__enter__()
-                    c.set(key, result, time=600, namespace='membership')
-                finally:
-                    c.__exit__(None, None, None)
+                cache.set(key, result, time=600, namespace='membership')
         return result
 
     def user_roles(self, username):
 
     def create_account(self, registration):
         key = keys.has_account(registration.credential.username)
-        #with cache_factory() as c:
-        c = cache_factory()
-        try:
-            c.__enter__()
-            c.delete(key, namespace='membership')
-        finally:
-            c.__exit__(None, None, None)
+        cache.delete(key, namespace='membership')
         return self.inner.create_account(registration)
 
 install_requires = [
     'wheezy.core>=0.1.70',
-    'wheezy.caching>=0.1.54',
+    'wheezy.caching>=0.1.61',
     'wheezy.html>=0.1.109',
-    'wheezy.http>=0.1.236',
+    'wheezy.http>=0.1.254',
     'wheezy.routing>=0.1.124',
     'wheezy.security>=0.1.36',
     'wheezy.validation>=0.1.74',

File src/wheezy/web/templates.py

     def __init__(
             self, directories=None,
             module_directory='/tmp/mako_modules',
-            cache_factory=None,
+            cache=None,
             **kwargs):
         from mako.lookup import TemplateLookup
-        if cache_factory is None:
+        if cache is None:
             self.template_lookup = TemplateLookup(
                 directories=directories or ['content/templates'],
                 module_directory=module_directory,
                 directories=directories or ['content/templates'],
                 module_directory=module_directory,
                 cache_impl='wheezy',
-                cache_args={'cache_factory': cache_factory},
+                cache_args={'cache': cache},
                 **kwargs)
 
     def __call__(self, template_name, kwargs):
 
 
 class MakoCacheImpl(object):
-    __slots__ = ('cache_factory', 'prefix')
+    __slots__ = ('cache', 'prefix')
 
     pass_context = False
 
     def __init__(self, cache):
-        self.cache_factory = cache.template.cache_args['cache_factory']
+        self.cache = cache.template.cache_args['cache']
         self.prefix = cache.id
 
     def get_or_create(self, key, creation_function, **kwargs):
         namespace = kwargs.get('namespace', None)
-        context = self.cache_factory()
-        cache = context.__enter__()
-        try:
-            value = cache.get(self.prefix + key, namespace)
-            if value is None:
-                value = creation_function()
-                cache.add(
-                    self.prefix + key,
-                    value,
-                    int(kwargs.get('time', 0)),
-                    namespace)
-            return value
-        finally:
-            context.__exit__(None, None, None)
+        value = self.cache.get(self.prefix + key, namespace)
+        if value is None:
+            value = creation_function()
+            self.cache.add(self.prefix + key, value,
+                           int(kwargs.get('time', 0)), namespace)
+        return value
 
     def set(self, key, value, **kw):
         raise NotImplementedError()

File src/wheezy/web/tests/test_templates.py

             module_directory='/tmp/mako_modules',
         )
 
-    def test_init_with_cache_factory(self):
-        """ Check __init__ with specified cache_factory
+    def test_init_with_cache(self):
+        """ Check __init__ with specified cache
         """
         from wheezy.web.templates import MakoTemplate
         assert MakoTemplate(
-            cache_factory='mock_cache_factory'
+            cache='mock_cache'
         )
         self.mock_template_lookup_class.assert_called_once_with(
             directories=['content/templates'],
             module_directory='/tmp/mako_modules',
             cache_impl='wheezy',
-            cache_args={'cache_factory': 'mock_cache_factory'}
+            cache_args={'cache': 'mock_cache'}
         )
         self.mock_register_plugin.assert_called_once_with(
             'wheezy', 'wheezy.web.templates', 'MakoCacheImpl')
 
     def setUp(self):
         from wheezy.web.templates import MakoCacheImpl
-        self.mock_cache_factory = Mock()
+        self.mock_cache = Mock()
         mock_mako_cache = Mock()
         mock_mako_cache.template.cache_args = {
-            'cache_factory': self.mock_cache_factory
+            'cache': self.mock_cache
         }
         mock_mako_cache.id = 'prefix-'
         self.cache = MakoCacheImpl(mock_mako_cache)
         """ __init__.
         """
         assert 'prefix-' == self.cache.prefix
-        assert self.mock_cache_factory == self.cache.cache_factory
+        assert self.mock_cache == self.cache.cache
 
     def test_not_implemented(self):
         """ set, get and invalidate raise error.
     def test_get_or_create_missing_in_cache(self):
         """ Requested item is missing in cache.
         """
-        mock_cache = Mock()
-        mock_cache.get.return_value = None
-        mock_context = Mock()
-        mock_context.__enter__ = Mock(return_value=mock_cache)
-        mock_context.__exit__ = Mock()
-        self.mock_cache_factory.return_value = mock_context
+        self.mock_cache.get.return_value = None
         mock_creation_function = Mock(return_value='html')
         assert 'html' == self.cache.get_or_create(
             'key',
             mock_creation_function,
             namespace='namespace',
             time='100')
-        mock_cache.get.assert_called_once_with(
+        self.mock_cache.get.assert_called_once_with(
             'prefix-key', 'namespace')
-        mock_cache.add.assert_called_once_with(
+        self.mock_cache.add.assert_called_once_with(
             'prefix-key', 'html', 100, 'namespace')
 
     def test_get_or_create_found_in_cache(self):
         """ Requested item found in cache.
         """
-        mock_cache = Mock()
-        mock_cache.get.return_value = 'html'
-        mock_context = Mock()
-        mock_context.__enter__ = Mock(return_value=mock_cache)
-        mock_context.__exit__ = Mock()
-        self.mock_cache_factory.return_value = mock_context
+        self.mock_cache.get.return_value = 'html'
         mock_creation_function = Mock()
         assert 'html' == self.cache.get_or_create(
             'key',
             mock_creation_function,
             namespace='namespace',
             time='100')
-        mock_cache.get.assert_called_once_with(
+        self.mock_cache.get.assert_called_once_with(
             'prefix-key', 'namespace')
         assert not mock_creation_function.called