Skip to content

Instantly share code, notes, and snippets.

@veriojon
Created November 11, 2010 20:31
Show Gist options
  • Save veriojon/673129 to your computer and use it in GitHub Desktop.
Save veriojon/673129 to your computer and use it in GitHub Desktop.
basic class with some tests
class memCache(object):
def __init__(self):
self.cache_dict = dict()
def get(self,key):
try:
value = self.cache_dict[key]
return value
except:
raise UserWarning, "Key not found"
def set(self,key,value):
try:
self.get(key)
except:
if key == "":
key = str(uuid.uuid4())
finally:
self.cache_dict[key] = value
return key
def delete(self,key):
try:
self.get(key)
self.cache_dict.pop(key)
return 1
except:
return 0
return
test = memCache()
key = test.set('','bar')
print test.cache_dict
print test.get(key)
# try to delete a key that does not exist
status = test.delete('foo')
print status
print test.cache_dict
# try to delete a key that does exist
status = test.delete(key)
print status
print test.cache_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment