2004-09-20 19:34:09 +00:00

38 lines
1015 B
Python

import threading
import time
from Logging import *
class Cache(object):
def __init__(self, expiration):
self.lock = threading.Lock()
self.expiration = expiration
self.cache = {}
def put(self, key, value):
self.lock.acquire()
self.cache[key] = (time.time(), value)
debug("cache.put(%s, %s)" % (str(key), str(value)))
self.lock.release()
def get(self, key):
try:
self.lock.acquire()
debug("cache.get(%s)" % str(key))
try:
timestamp, value = self.cache[key]
debug("cache.get found: %s" % value)
if (timestamp + self.expiration) < time.time():
debug("cache.get: expired")
del self.cache[key]
raise KeyError
return value
except KeyError:
debug("cache.get: found nothing")
return None
finally:
self.lock.release()