prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  DNSCACHE_ENABLED=False not working
  Originally reported by @softwarevamp on [StackOverflow](https://stackoverflow.com/questions/44877296/scrapy-with-dnscache-enabled-false-not-working):
  
  > When i run scrapy shell with `DNSCACHE_ENABLED=False` got
  ```
  KeyError: 'dictionary is empty'
  twisted.internet.error.DNSLookupError: DNS lookup failed: no results for hostname lookup: www.mydomain.com.
  ```
  
  ```
      2017-07-03 03:09:12 [twisted] CRITICAL: while looking up www.mydomain.com with <scrapy.resolver.CachingThreadedResolver object at 0x3fd0050>
      Traceback (most recent call last):
        File "/usr/lib64/python2.7/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
          current.result = callback(current.result, *args, **kw)
        File "/usr/lib64/python2.7/site-packages/scrapy/resolver.py", line 29, in _cache_result
          dnscache[name] = result
        File "/usr/lib64/python2.7/site-packages/scrapy/utils/datatypes.py", line 305, in __setitem__
          self.popitem(last=False)
        File "/usr/lib64/python2.7/collections.py", line 159, in popitem
          raise KeyError('dictionary is empty')
      KeyError: 'dictionary is empty'
      2017-07-03 03:09:12 [scrapy.downloadermiddlewares.retry] DEBUG: Gave up retrying <GET //www.mydomain.com/> (failed 3 times): DNS lookup failed: no results for hostname lookup: www.mydomain.com.
      Traceback (most recent call last):
        File "/usr/bin/scrapy", line 11, in <module>
          sys.exit(execute())
        File "/usr/lib64/python2.7/site-packages/scrapy/cmdline.py", line 149, in execute
          _run_print_help(parser, _run_command, cmd, args, opts)
        File "/usr/lib64/python2.7/site-packages/scrapy/cmdline.py", line 89, in _run_print_help
          func(*a, **kw)
        File "/usr/lib64/python2.7/site-packages/scrapy/cmdline.py", line 156, in _run_command
          cmd.run(args, opts)
        File "/usr/lib64/python2.7/site-packages/scrapy/commands/shell.py", line 73, in run
          shell.start(url=url, redirect=not opts.no_redirect)
        File "/usr/lib64/python2.7/site-packages/scrapy/shell.py", line 48, in start
          self.fetch(url, spider, redirect=redirect)
        File "/usr/lib64/python2.7/site-packages/scrapy/shell.py", line 115, in fetch
          reactor, self._schedule, request, spider)
        File "/usr/lib64/python2.7/site-packages/twisted/internet/threads.py", line 122, in blockingCallFromThread
          result.raiseException()
        File "<string>", line 2, in raiseException
      twisted.internet.error.DNSLookupError: DNS lookup failed: no results for hostname lookup: www.mydomain.com.
  ```
  
  > Any thoughts welcome
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `scrapy/resolver.py`
  Content:
  ```
  1 from twisted.internet import defer
  2 from twisted.internet.base import ThreadedResolver
  3 
  4 from scrapy.utils.datatypes import LocalCache
  5 
  6 # TODO: cache misses
  7 
  8 dnscache = LocalCache(10000)
  9 
  10 class CachingThreadedResolver(ThreadedResolver):
  11     def __init__(self, reactor, cache_size, timeout):
  12         super(CachingThreadedResolver, self).__init__(reactor)
  13         dnscache.limit = cache_size
  14         self.timeout = timeout
  15 
  16     def getHostByName(self, name, timeout=None):
  17         if name in dnscache:
  18             return defer.succeed(dnscache[name])
  19         # in Twisted<=16.6, getHostByName() is always called with
  20         # a default timeout of 60s (actually passed as (1, 3, 11, 45) tuple),
  21         # so the input argument above is simply overridden
  22         # to enforce Scrapy's DNS_TIMEOUT setting's value
  23         timeout = (self.timeout,)
  24         d = super(CachingThreadedResolver, self).getHostByName(name, timeout)
  25         d.addCallback(self._cache_result, name)
  26         return d
  27 
  28     def _cache_result(self, result, name):
  29         dnscache[name] = result
  30         return result
  31
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/scrapy/resolver.py b/scrapy/resolver.py
  --- a/scrapy/resolver.py
  +++ b/scrapy/resolver.py
  @@ -22,7 +22,8 @@
           # to enforce Scrapy's DNS_TIMEOUT setting's value
           timeout = (self.timeout,)
           d = super(CachingThreadedResolver, self).getHostByName(name, timeout)
  -        d.addCallback(self._cache_result, name)
  +        if dnscache.limit:
  +            d.addCallback(self._cache_result, name)
           return d
   
       def _cache_result(self, result, name):
  
verification_info: {"golden_diff": "diff --git a/scrapy/resolver.py b/scrapy/resolver.py\n--- a/scrapy/resolver.py\n+++ b/scrapy/resolver.py\n@@ -22,7 +22,8 @@\n         # to enforce Scrapy's DNS_TIMEOUT setting's value\n         timeout = (self.timeout,)\n         d = super(CachingThreadedResolver, self).getHostByName(name, timeout)\n-        d.addCallback(self._cache_result, name)\n+        if dnscache.limit:\n+            d.addCallback(self._cache_result, name)\n         return d\n \n     def _cache_result(self, result, name):\n", "issue": "DNSCACHE_ENABLED=False not working\nOriginally reported by @softwarevamp on [StackOverflow](https://stackoverflow.com/questions/44877296/scrapy-with-dnscache-enabled-false-not-working):\r\n\r\n> When i run scrapy shell with `DNSCACHE_ENABLED=False` got\r\n```\r\nKeyError: 'dictionary is empty'\r\ntwisted.internet.error.DNSLookupError: DNS lookup failed: no results for hostname lookup: www.mydomain.com.\r\n```\r\n\r\n```\r\n    2017-07-03 03:09:12 [twisted] CRITICAL: while looking up www.mydomain.com with <scrapy.resolver.CachingThreadedResolver object at 0x3fd0050>\r\n    Traceback (most recent call last):\r\n      File \"/usr/lib64/python2.7/site-packages/twisted/internet/defer.py\", line 653, in _runCallbacks\r\n        current.result = callback(current.result, *args, **kw)\r\n      File \"/usr/lib64/python2.7/site-packages/scrapy/resolver.py\", line 29, in _cache_result\r\n        dnscache[name] = result\r\n      File \"/usr/lib64/python2.7/site-packages/scrapy/utils/datatypes.py\", line 305, in __setitem__\r\n        self.popitem(last=False)\r\n      File \"/usr/lib64/python2.7/collections.py\", line 159, in popitem\r\n        raise KeyError('dictionary is empty')\r\n    KeyError: 'dictionary is empty'\r\n    2017-07-03 03:09:12 [scrapy.downloadermiddlewares.retry] DEBUG: Gave up retrying <GET //www.mydomain.com/> (failed 3 times): DNS lookup failed: no results for hostname lookup: www.mydomain.com.\r\n    Traceback (most recent call last):\r\n      File \"/usr/bin/scrapy\", line 11, in <module>\r\n        sys.exit(execute())\r\n      File \"/usr/lib64/python2.7/site-packages/scrapy/cmdline.py\", line 149, in execute\r\n        _run_print_help(parser, _run_command, cmd, args, opts)\r\n      File \"/usr/lib64/python2.7/site-packages/scrapy/cmdline.py\", line 89, in _run_print_help\r\n        func(*a, **kw)\r\n      File \"/usr/lib64/python2.7/site-packages/scrapy/cmdline.py\", line 156, in _run_command\r\n        cmd.run(args, opts)\r\n      File \"/usr/lib64/python2.7/site-packages/scrapy/commands/shell.py\", line 73, in run\r\n        shell.start(url=url, redirect=not opts.no_redirect)\r\n      File \"/usr/lib64/python2.7/site-packages/scrapy/shell.py\", line 48, in start\r\n        self.fetch(url, spider, redirect=redirect)\r\n      File \"/usr/lib64/python2.7/site-packages/scrapy/shell.py\", line 115, in fetch\r\n        reactor, self._schedule, request, spider)\r\n      File \"/usr/lib64/python2.7/site-packages/twisted/internet/threads.py\", line 122, in blockingCallFromThread\r\n        result.raiseException()\r\n      File \"<string>\", line 2, in raiseException\r\n    twisted.internet.error.DNSLookupError: DNS lookup failed: no results for hostname lookup: www.mydomain.com.\r\n```\r\n\r\n> Any thoughts welcome\n", "before_files": [{"content": "from twisted.internet import defer\nfrom twisted.internet.base import ThreadedResolver\n\nfrom scrapy.utils.datatypes import LocalCache\n\n# TODO: cache misses\n\ndnscache = LocalCache(10000)\n\nclass CachingThreadedResolver(ThreadedResolver):\n    def __init__(self, reactor, cache_size, timeout):\n        super(CachingThreadedResolver, self).__init__(reactor)\n        dnscache.limit = cache_size\n        self.timeout = timeout\n\n    def getHostByName(self, name, timeout=None):\n        if name in dnscache:\n            return defer.succeed(dnscache[name])\n        # in Twisted<=16.6, getHostByName() is always called with\n        # a default timeout of 60s (actually passed as (1, 3, 11, 45) tuple),\n        # so the input argument above is simply overridden\n        # to enforce Scrapy's DNS_TIMEOUT setting's value\n        timeout = (self.timeout,)\n        d = super(CachingThreadedResolver, self).getHostByName(name, timeout)\n        d.addCallback(self._cache_result, name)\n        return d\n\n    def _cache_result(self, result, name):\n        dnscache[name] = result\n        return result\n", "path": "scrapy/resolver.py"}], "after_files": [{"content": "from twisted.internet import defer\nfrom twisted.internet.base import ThreadedResolver\n\nfrom scrapy.utils.datatypes import LocalCache\n\n# TODO: cache misses\n\ndnscache = LocalCache(10000)\n\nclass CachingThreadedResolver(ThreadedResolver):\n    def __init__(self, reactor, cache_size, timeout):\n        super(CachingThreadedResolver, self).__init__(reactor)\n        dnscache.limit = cache_size\n        self.timeout = timeout\n\n    def getHostByName(self, name, timeout=None):\n        if name in dnscache:\n            return defer.succeed(dnscache[name])\n        # in Twisted<=16.6, getHostByName() is always called with\n        # a default timeout of 60s (actually passed as (1, 3, 11, 45) tuple),\n        # so the input argument above is simply overridden\n        # to enforce Scrapy's DNS_TIMEOUT setting's value\n        timeout = (self.timeout,)\n        d = super(CachingThreadedResolver, self).getHostByName(name, timeout)\n        if dnscache.limit:\n            d.addCallback(self._cache_result, name)\n        return d\n\n    def _cache_result(self, result, name):\n        dnscache[name] = result\n        return result\n", "path": "scrapy/resolver.py"}]}