Created
November 11, 2010 20:31
-
-
Save veriojon/673129 to your computer and use it in GitHub Desktop.
basic class with some tests
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
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