initial upload
This commit is contained in:
37
smmapd_prototype/Cache.py
Normal file
37
smmapd_prototype/Cache.py
Normal file
@ -0,0 +1,37 @@
|
||||
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()
|
||||
|
Reference in New Issue
Block a user