Source

Dragoman / dragoman / lib / httpcache.py

Full commit
'''An httplib2 compatible threadsafe bsddb cache.'''
import httplib2
import bsddb

class BsddbCache(object):
	def __init__(self, cache, safe=httplib2.safename):
		self.db = bsddb.btopen(cache)
		self.safe = safe

	def get(self, key):
		cached_key = self.safe(key)
		if self.db.has_key(cached_key):
			return self.db[cached_key]
		return None

	def set(self, key, value):
		cached_key = self.safe(key)
		self.db[cached_key] = value
		self.db.sync()

	def delete(self, key):
		cached_key = self.safe(key)
		if self.db.has_key(cached_key):
			self.db.__delitem__(cached_key)
			self.db.sync()