You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
1.1 KiB

4 years ago
  1. from twisted.internet import defer
  2. from twisted.internet.base import ThreadedResolver
  3. from scrapy.utils.datatypes import LocalCache
  4. # TODO: cache misses
  5. dnscache = LocalCache(10000)
  6. class CachingThreadedResolver(ThreadedResolver):
  7. def __init__(self, reactor, cache_size, timeout):
  8. super(CachingThreadedResolver, self).__init__(reactor)
  9. dnscache.limit = cache_size
  10. self.timeout = timeout
  11. def getHostByName(self, name, timeout=None):
  12. if name in dnscache:
  13. return defer.succeed(dnscache[name])
  14. # in Twisted<=16.6, getHostByName() is always called with
  15. # a default timeout of 60s (actually passed as (1, 3, 11, 45) tuple),
  16. # so the input argument above is simply overridden
  17. # to enforce Scrapy's DNS_TIMEOUT setting's value
  18. timeout = (self.timeout,)
  19. d = super(CachingThreadedResolver, self).getHostByName(name, timeout)
  20. if dnscache.limit:
  21. d.addCallback(self._cache_result, name)
  22. return d
  23. def _cache_result(self, result, name):
  24. dnscache[name] = result
  25. return result