diff --git a/docs/topics/downloader-middleware.rst b/docs/topics/downloader-middleware.rst index 383f331b5..d09c205d1 100644 --- a/docs/topics/downloader-middleware.rst +++ b/docs/topics/downloader-middleware.rst @@ -415,7 +415,7 @@ HttpCacheMiddleware * :ref:`httpcache-policy-dummy` You can change the HTTP cache policy with the :setting:`HTTPCACHE_POLICY` - setting. Or you can also implement your own policy. + setting. Or you can also :ref:`implement your own policy `. .. reqmeta:: dont_cache @@ -487,6 +487,40 @@ RFC2616 policy * ... probably others .. +.. _httpcache-policy-custom: + +Writing your own policy +~~~~~~~~~~~~~~~~~~~~~~~ + +You can implement a cache policy by creating a class that +defines the methods described below. + +.. module:: scrapy.extensions.httpcache + +.. class:: CachePolicy + + .. method:: should_cache_request(request) + + Return True if the :class:`~scrapy.http.Request` is cacheable otherwise False + + .. method:: should_cache_response(response, request) + + Return True if :class:`~scrapy.http.Response` should be cached otherwise False + + .. method:: is_cached_response_fresh(cachedresponse, request) + + Return True if cached :class:`~scrapy.http.Response` has expired otherwise False + + .. method:: is_cached_response_valid(cachedresponse, response, request) + + Return True if cached :class:`~scrapy.http.Response` is valid otherwise False + + +In order to use this policy, set: + +* :setting:`HTTPCACHE_POLICY` to the Python import path of your custom policy class. + + .. _httpcache-storage-fs: Filesystem storage backend (default)