From: W. Trevor King <wking@drexel.edu>
Date: Thu, 23 Feb 2012 02:57:23 +0000 (-0500)
Subject: Add persistent caching to Resolver.
X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3f715a892e61017656e18bf1d38595b3cfcc8203;p=apachelog.git

Add persistent caching to Resolver.
---

diff --git a/apachelog/resolve.py b/apachelog/resolve.py
index d825c17..88843b7 100644
--- a/apachelog/resolve.py
+++ b/apachelog/resolve.py
@@ -1,3 +1,5 @@
+import os.path as _os_path
+import pickle as _pickle
 import re as _re
 import socket as _socket
 
@@ -46,8 +48,31 @@ class Resolver (object):
         ]:
         REGEXPS[bot] = [_re.compile('.*{}.*'.format(bot))]
 
+    _cache_file = _os_path.expanduser(
+        _os_path.join('~', '.apachelog-resolver.cache'))
+    _cache_loaded = False
+
     def __init__(self, smart=False):
         self._smart = smart
+        self.load_cache()
+
+    @classmethod
+    def load_cache(self):
+        if not self._cache_loaded:
+            self._cache_loaded = True
+            try:
+                with open(self._cache_file, 'rb') as f:
+                    self.IP = _pickle.load(f)
+            except IOError:
+                pass
+            if self.IP is None:
+                self.IP = {}
+
+    @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)
 
     def resolve(self, ip):
         if ip not in self.IP: