Skip to content

Commit

Permalink
redis datastore now supports salted keys
Browse files Browse the repository at this point in the history
  • Loading branch information
serg0987 committed Sep 19, 2011
1 parent f49aa72 commit be2dbf9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions datastore.py
Expand Up @@ -8,6 +8,7 @@
import re, logging, copy
import json
import os
import hashlib

non_record = re.compile(r'__\w+__')

Expand Down Expand Up @@ -49,7 +50,11 @@ def __get__(self, instance, owner):

def __set__(self, instance, value):
valuedict = instance.__instdict__
valuedict[self.key] = self.type(value)
try:
valuedict[self.key] = self.type(value)
except:
logging.info( 'could not save key %s with value %s as type %s'%(self.key,value,self.type))
raise


class DateValue(Value):
Expand All @@ -61,10 +66,11 @@ class Model(object):
# static model parameters
__structure__ = {}
__collection__ = {} # Collection name for Mongo DB

id = Value(int)

id = Value(str)
__salt__ = None
def __init__(self, valuedict=None, embedded=False, **kwargs):
#we might use salt to make our sequence key for this object more interesting
if 'salt' in kwargs: self.__salt__ = kwargs['salt']
classname = self.__class__.__name__
self.__init_structure__(classname, valuedict, **kwargs)
self.collection_name = self.__collection__[classname]
Expand Down Expand Up @@ -93,7 +99,8 @@ def __init_structure__(self, classname, valuedict=None, **kwargs):
self.__instdict__ = copy.deepcopy(self.__structure__[classname])

for k in kwargs:
if k in self.__instdict__:
if k=='salt': continue
elif k in self.__instdict__:
if hasattr(kwargs[k], 'get_values'):
self.__instdict__[k] = kwargs[k].get_values()
else:
Expand All @@ -108,11 +115,12 @@ def save(self, storage = None):
return
if not self.id:
new_id = RedisConn.incr(':'.join([REDIS_NAMESPACE, self.__class__.__name__.lower() + '_key']))
self.id = new_id
if self.__salt__:
self.id = hashlib.md5(str(new_id)+self.__salt__).hexdigest()
else:
self.id = new_id
# print ':'.join([REDIS_NAMESPACE, self.collection_name, str(self.id)]), json.dumps(self.__instdict__)
RedisConn.set(':'.join([REDIS_NAMESPACE, self.collection_name, str(self.id)]), json.dumps(self.__instdict__))
# print '==============================================================================================='
# print json.dumps(self.__instdict__)
#self.save_redis_recursive(':'.join([self.collection_name, str(self.id)]), self.__instdict__)


Expand All @@ -138,11 +146,15 @@ def get_values(self):
return copy.deepcopy(self.__instdict__)

@classmethod
def get(cls, id, storage = None): # storage=None for backword capability
def get(cls, id, storage = None,salt=None): # storage=None for backword capability
"Get object from Redis storage by ID"
if salt:
idtoget = hashlib.md5(id+salt).hexdigest()
else:
idtoget = id
# First try to find object by Id
# example: gameserver:scratchgames:101
inst_data = RedisConn.get(':'.join([REDIS_NAMESPACE, cls.get_collection_name(), str(id)]))
inst_data = RedisConn.get(':'.join([REDIS_NAMESPACE, cls.get_collection_name(), str(idtoget)]))
if not inst_data: # No objects with such ID
raise DoesNotExist('No model in Redis srorage with such id')
else:
Expand Down
2 changes: 1 addition & 1 deletion http.py
Expand Up @@ -96,7 +96,7 @@ def __init__(self, response_dict):
logging.debug('response_dict is %s. Set-cookies dict is %s' % (response_dict, set_cookies_dict))
if set_cookies_dict:
for cookie in set_cookies_dict:
logging.debug('Try to set cookie %s to value %i' % (cookie, set_cookies_dict[cookie]))
logging.debug('Try to set cookie %s to value %s' % (cookie, set_cookies_dict[cookie]))
self.set_cookie(cookie, str(set_cookies_dict[cookie]))
response_dict.pop(SET_COOKIES)

Expand Down

0 comments on commit be2dbf9

Please sign in to comment.