Skip to content

Instantly share code, notes, and snippets.

@giornaledisistema
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giornaledisistema/4399d1ee93963958a38f to your computer and use it in GitHub Desktop.
Save giornaledisistema/4399d1ee93963958a38f to your computer and use it in GitHub Desktop.
xmlrpc cache server
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