From 4b344f8af63f9194a4652520f1555f1247fa9b1f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 28 Feb 2012 10:19:52 -0500 Subject: [PATCH] Only save the resolver cache when the on-disk version is out of date. --- apachelog/resolve.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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: -- 2.26.2