-
-
Save giornaledisistema/4399d1ee93963958a38f to your computer and use it in GitHub Desktop.
xmlrpc cache server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from SimpleXMLRPCServer import SimpleXMLRPCServer | |
import time | |
class CacheServer(object): | |
""" | |
A very semplified cache server like memcache. :-) | |
""" | |
def __init__(self): | |
""" | |
Initialize a empty cache server istance. | |
""" | |
self.cached_values = {} | |
def set(self, key, value, timeout=None): | |
""" | |
Add a key:value in cache. | |
""" | |
self.cached_values[key] = {"value": value, | |
"creation_time": time.time(), | |
"timeout": timeout, | |
} | |
def get(self, key): | |
""" | |
Get the relative value from cache if key exists or key exits for less | |
time than timeout, otherwise None. | |
""" | |
if key in self.cached_values: | |
data = self.cached_values[key] | |
if time.time() - data["creation_time"] < data["timeout"]: | |
return data["value"] | |
else: | |
return None | |
else: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment