summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOwen W. Taylor <otaylor@fishsoup.net>2009-08-31 01:05:07 -0400
committerOwen W. Taylor <otaylor@fishsoup.net>2009-08-31 01:05:07 -0400
commit9b8a0acd33c457111e2b07076f6b30600ad6b5a1 (patch)
treed6fc9573b56fcabe53ebde1c5789709f8ff9be6c
parent9804dce599270a979fb159ce327bd46fa1a1ea1c (diff)
Add utility for constant-response caching
We don't want to constantly refetch things like legal field values from the server. Add a simple cache based on RawConfigParser and pickled values stored in ~/.git-bz-cache.
-rwxr-xr-xgit-bz41
1 files changed, 40 insertions, 1 deletions
diff --git a/git-bz b/git-bz
index 4aa3ab6..bb83522 100755
--- a/git-bz
+++ b/git-bz
@@ -224,7 +224,8 @@ default-priority = ---
################################################################################
import base64
-from ConfigParser import RawConfigParser
+import cPickle as pickle
+from ConfigParser import RawConfigParser, NoOptionError
import httplib
from optparse import OptionParser
import os
@@ -709,6 +710,44 @@ def encode_multipart_formdata(fields, files=None):
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
+# Cache of constant-responses per bugzilla server
+# ===============================================
+
+CACHE_EXPIRY_TIME = 3600 * 24 # one day
+
+class Cache(object):
+ def __init__(self):
+ self.cfp = None
+
+ def __ensure(self, host):
+ if self.cfp == None:
+ self.cfp = RawConfigParser()
+ self.cfp.read(os.path.expanduser("~/.git-bz-cache"))
+
+ if self.cfp.has_section(host):
+ if time.time() > self.cfp.getfloat(host, "expires"):
+ self.cfp.remove_section(host)
+
+ if not self.cfp.has_section(host):
+ self.cfp.add_section(host)
+ self.cfp.set(host, "expires", time.time() + CACHE_EXPIRY_TIME)
+
+ def get(self, host, key):
+ self.__ensure(host)
+ try:
+ return pickle.loads(self.cfp.get(host, key))
+ except NoOptionError:
+ raise IndexError()
+
+ def set(self, host, key, value):
+ self.__ensure(host)
+ self.cfp.set(host, key, pickle.dumps(value))
+ f = open(os.path.expanduser("~/.git-bz-cache"), "w")
+ self.cfp.write(f)
+ f.close()
+
+cache = Cache()
+
# General Utility Functions
# =========================