From: W. Trevor King Date: Tue, 28 Feb 2012 15:19:52 +0000 (-0500) Subject: Only save the resolver cache when the on-disk version is out of date. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=4b344f8af63f9194a4652520f1555f1247fa9b1f;p=apachelog.git Only save the resolver cache when the on-disk version is out of date. --- diff --git a/apachelog/resolve.py b/apachelog/resolve.py index 31451fa..3a5a7c6 100644 --- a/apachelog/resolve.py +++ b/apachelog/resolve.py @@ -51,6 +51,7 @@ class Resolver (object): _cache_file = _os_path.expanduser( _os_path.join('~', '.apachelog-resolver.cache')) _cache_loaded = False + _cache_dirty = None def __init__(self, smart=False): self._smart = smart @@ -63,6 +64,7 @@ class Resolver (object): try: with open(self._cache_file, 'rb') as f: self.IP = _pickle.load(f) + self._cache_dirty = False except IOError: pass if self.IP is None: @@ -71,11 +73,13 @@ class Resolver (object): @classmethod def save_cache(self): self.load_cache() # avoid clobbering unloaded content - with open(self._cache_file, 'wb') as f: - _pickle.dump(self.IP, f) + if self._cache_dirty: + with open(self._cache_file, 'wb') as f: + _pickle.dump(self.IP, f) def resolve(self, ip): if ip not in self.IP: + self._cache_dirty = True try: self.IP[ip] = _socket.gethostbyaddr(ip) except _socket.herror as e: