From c13e23641bbc66c5d1a760310c6eb97e35c12c11 Mon Sep 17 00:00:00 2001 From: nramirezuy Date: Mon, 28 Jul 2014 17:58:56 -0300 Subject: [PATCH] httpcache dont_cache meta #19 #689 --- docs/topics/downloader-middleware.rst | 3 +++ docs/topics/request-response.rst | 1 + scrapy/contrib/downloadermiddleware/httpcache.py | 6 ++++++ tests/test_downloadermiddleware_httpcache.py | 12 ++++++++++++ 4 files changed, 22 insertions(+) diff --git a/docs/topics/downloader-middleware.rst b/docs/topics/downloader-middleware.rst index a3647fd9b..9ef09785f 100644 --- a/docs/topics/downloader-middleware.rst +++ b/docs/topics/downloader-middleware.rst @@ -328,6 +328,9 @@ HttpCacheMiddleware You can change the HTTP cache policy with the :setting:`HTTPCACHE_POLICY` setting. Or you can also implement your own policy. + .. reqmeta:: dont_cache + + You can also avoid caching a response on every policy using :reqmeta:`dont_cache` meta key equals `True`. .. _httpcache-policy-dummy: diff --git a/docs/topics/request-response.rst b/docs/topics/request-response.rst index f2c37c2e6..b6b165d50 100644 --- a/docs/topics/request-response.rst +++ b/docs/topics/request-response.rst @@ -227,6 +227,7 @@ Those are: * :reqmeta:`handle_httpstatus_all` * ``dont_merge_cookies`` (see ``cookies`` parameter of :class:`Request` constructor) * :reqmeta:`cookiejar` + :reqmeta:`dont_cache` * :reqmeta:`redirect_urls` * :reqmeta:`bindaddress` * :reqmeta:`dont_obey_robotstxt` diff --git a/scrapy/contrib/downloadermiddleware/httpcache.py b/scrapy/contrib/downloadermiddleware/httpcache.py index 90aa6cab7..7b4b53f7c 100644 --- a/scrapy/contrib/downloadermiddleware/httpcache.py +++ b/scrapy/contrib/downloadermiddleware/httpcache.py @@ -28,6 +28,9 @@ class HttpCacheMiddleware(object): self.storage.close_spider(spider) def process_request(self, request, spider): + if request.meta.get('dont_cache', False): + return + # Skip uncacheable requests if not self.policy.should_cache_request(request): request.meta['_dont_cache'] = True # flag as uncacheable @@ -53,6 +56,9 @@ class HttpCacheMiddleware(object): request.meta['cached_response'] = cachedresponse def process_response(self, request, response, spider): + if request.meta.get('dont_cache', False): + return response + # Skip cached responses and uncacheable requests if 'cached' in response.flags or '_dont_cache' in request.meta: request.meta.pop('_dont_cache', None) diff --git a/tests/test_downloadermiddleware_httpcache.py b/tests/test_downloadermiddleware_httpcache.py index 1e22ae661..ea811cb3b 100644 --- a/tests/test_downloadermiddleware_httpcache.py +++ b/tests/test_downloadermiddleware_httpcache.py @@ -89,6 +89,18 @@ class _BaseTest(unittest.TestCase): assert any(h in request2.headers for h in ('If-None-Match', 'If-Modified-Since')) self.assertEqual(request1.body, request2.body) + def test_dont_cache(self): + with self._middleware() as mw: + self.request.meta['dont_cache'] = True + mw.process_response(self.request, self.response, self.spider) + self.assertEqual(mw.storage.retrieve_response(self.spider, self.request), None) + + with self._middleware() as mw: + self.request.meta['dont_cache'] = False + mw.process_response(self.request, self.response, self.spider) + if mw.policy.should_cache_response(self.response, self.request): + self.assertIsInstance(mw.storage.retrieve_response(self.spider, self.request), self.response.__class__) + class DefaultStorageTest(_BaseTest):