Commits

Andriy Kornatskyy committed 9a4d7ac

Refactored HTTPCacheMiddleware by eliminating cache_factory concept.

Comments (0)

Files changed (2)

src/wheezy/http/middleware.py

     """ HTTP cache middleware.
     """
 
-    def __init__(self, cache_factory, middleware_vary):
+    def __init__(self, cache, middleware_vary):
         """
-            ``cache_factory`` - cache factory to be used.
+            ``cache`` - cache to be used.
             ``middleware_vary`` - a way to determine cache profile
             key for the request.
         """
-        assert cache_factory
+        assert cache
         assert middleware_vary
-        self.cache_factory = cache_factory
+        self.cache = cache
         self.middleware_vary = middleware_vary
         self.profiles = defaultdict(lambda: None)
 
         cache_profile = self.profiles[middleware_key]
         if cache_profile:
             request_key = cache_profile.request_vary.key(request)
-            context = self.cache_factory()
-            cache = context.__enter__()
-            try:
-                response = cache.get(request_key, cache_profile.namespace)
-            finally:
-                context.__exit__(None, None, None)
+            response = self.cache.get(request_key, cache_profile.namespace)
             if response:  # cache hit
                 environ = request.environ
                 if response.etag:
                         request)
                 dependency = response.dependency
                 response = CacheableResponse(response)
-                context = self.cache_factory()
-                cache = context.__enter__()
-                try:
-                    if dependency:
-                        cache.set_multi({
-                            request_key: response,
-                            dependency.next_key(
-                                cache,
-                                response_cache_profile.namespace
-                            ): request_key},
-                            response_cache_profile.duration,
-                            '',
-                            response_cache_profile.namespace)
-                    else:
-                        cache.set(
-                            request_key,
-                            response,
-                            response_cache_profile.duration,
-                            response_cache_profile.namespace)
-                finally:
-                    context.__exit__(None, None, None)
+                if dependency:
+                    self.cache.set_multi({
+                        request_key: response,
+                        dependency.next_key(
+                            self.cache,
+                            response_cache_profile.namespace
+                        ): request_key},
+                        response_cache_profile.duration,
+                        '',
+                        response_cache_profile.namespace)
+                else:
+                    self.cache.set(
+                        request_key,
+                        response,
+                        response_cache_profile.duration,
+                        response_cache_profile.namespace)
         return response
 
 
 def http_cache_middleware_factory(options):
     """ HTTP cache middleware factory.
 
-        Requires ``http_cache_factory`` in options.
+        Requires ``http_cache`` in options.
 
         Supports ``http_cache_middleware_vary`` - a way to determine
         cache key for the request.
     """
-    cache_factory = options['http_cache_factory']
+    cache = options['http_cache']
     middleware_vary = options.get('http_cache_middleware_vary', None)
     if middleware_vary is None:
         middleware_vary = RequestVary()
         options['http_cache_middleware_vary'] = middleware_vary
     return HTTPCacheMiddleware(
-        cache_factory=cache_factory,
+        cache=cache,
         middleware_vary=middleware_vary)
 
 

src/wheezy/http/tests/test_middleware.py

         """
         from wheezy.http.middleware import http_cache_middleware_factory
         options = {
-            'http_cache_factory': 'cache_factory',
+            'http_cache': 'cache',
             'http_cache_middleware_vary': 'middleware_vary'
         }
 
         middleware = http_cache_middleware_factory(options)
-        assert 'cache_factory' == middleware.cache_factory
+        assert 'cache' == middleware.cache
         assert 'middleware_vary' == middleware.middleware_vary
 
         del options['http_cache_middleware_vary']
         middleware = http_cache_middleware_factory(options)
         assert middleware.middleware_vary
 
-        del options['http_cache_factory']
+        del options['http_cache']
         self.assertRaises(KeyError,
                           lambda: http_cache_middleware_factory(options))
 
         from wheezy.http.middleware import http_cache_middleware_factory
         from wheezy.http.response import HTTPResponse
         self.mock_cache = Mock()
-        mock_context = Mock()
-        mock_context.__enter__ = Mock(return_value=self.mock_cache)
-        mock_context.__exit__ = Mock()
-        mock_cache_factory = Mock(return_value=mock_context)
         options = {
-            'http_cache_factory': mock_cache_factory
+            'http_cache': self.mock_cache
         }
         self.middleware = http_cache_middleware_factory(options)
         self.mock_request = Mock()