Merge pull request #864 from niphlod/fix/redis_sessions

fixes issue with deleting sessions
This commit is contained in:
mdipierro committed 2015-03-22 00:36:47 -05:00
commit d7435a07ad
1 file changed
+23 -6
+23 -6
View File
@@ -59,8 +59,7 @@ class RedisClient(object):
self.app = '' self.app = ''
self.r_server = redis.Redis(host=host, port=port, db=self.db, password=self.password) self.r_server = redis.Redis(host=host, port=port, db=self.db, password=self.password)
if with_lock: if with_lock:
RedisClient._release_script = \ RedisClient._release_script = self.r_server.register_script(_LUA_RELEASE_LOCK)
self.r_server.register_script(_LUA_RELEASE_LOCK)
self.tablename = None self.tablename = None
self.session_expiry = session_expiry self.session_expiry = session_expiry
self.with_lock = with_lock self.with_lock = with_lock
@@ -111,8 +110,9 @@ class MockTable(object):
def __call__(self, record_id, unique_key=None): def __call__(self, record_id, unique_key=None):
# Support DAL shortcut query: table(record_id) # Support DAL shortcut query: table(record_id)
q = self.id # This will call the __getattr__ below # This will call the __getattr__ below
# returning a MockQuery # returning a MockQuery
q = self.id
# Instructs MockQuery, to behave as db(table.id == record_id) # Instructs MockQuery, to behave as db(table.id == record_id)
q.op = 'eq' q.op = 'eq'
@@ -125,9 +125,11 @@ class MockTable(object):
def __getattr__(self, key): def __getattr__(self, key):
if key == 'id': if key == 'id':
# return a fake query. We need to query it just by id for normal operations # return a fake query. We need to query it just by id for normal operations
self.query = MockQuery(field='id', db=self.r_server, self.query = MockQuery(
field='id', db=self.r_server,
prefix=self.keyprefix, session_expiry=self.session_expiry, prefix=self.keyprefix, session_expiry=self.session_expiry,
with_lock=self.with_lock, unique_key=self.unique_key) with_lock=self.with_lock, unique_key=self.unique_key
)
return self.query return self.query
elif key == '_db': elif key == '_db':
# needed because of the calls in sessions2trash.py and globals.py # needed because of the calls in sessions2trash.py and globals.py
@@ -155,6 +157,7 @@ class MockTable(object):
release_lock(self.r_server, key_lock, newid) release_lock(self.r_server, key_lock, newid)
return newid return newid
class MockQuery(object): class MockQuery(object):
"""a fake Query object that supports querying by id """a fake Query object that supports querying by id
and listing all keys. No other operation is supported and listing all keys. No other operation is supported
@@ -219,6 +222,8 @@ class MockQuery(object):
# means that the session has been found and needs an update # means that the session has been found and needs an update
if self.op == 'eq' and self.field == 'id' and self.value: if self.op == 'eq' and self.field == 'id' and self.value:
key = self.keyprefix + ':' + str(self.value) key = self.keyprefix + ':' + str(self.value)
if not self.db.exists(key):
return None
with self.db.pipeline() as pipe: with self.db.pipeline() as pipe:
pipe.hmset(key, kwargs) pipe.hmset(key, kwargs)
if self.session_expiry: if self.session_expiry:
@@ -228,6 +233,17 @@ class MockQuery(object):
release_lock(self.db, key + ':lock', self.value) release_lock(self.db, key + ':lock', self.value)
return rtn return rtn
def delete(self, **kwargs):
# means that we want this session to be deleted
if self.op == 'eq' and self.field == 'id' and self.value:
id_idx = "%s:id_idx" % self.keyprefix
key = self.keyprefix + ':' + str(self.value)
with self.db.pipeline() as pipe:
pipe.delete(key)
pipe.srem(id_idx, key)
rtn = pipe.execute()
return rtn[1]
class RecordDeleter(object): class RecordDeleter(object):
"""Dumb record deleter to support sessions2trash.py""" """Dumb record deleter to support sessions2trash.py"""
@@ -261,5 +277,6 @@ end
def release_lock(conn, lockname, identifier): def release_lock(conn, lockname, identifier):
return RedisClient._release_script(keys=[lockname], args=[identifier], return RedisClient._release_script(
keys=[lockname], args=[identifier],
client=conn) client=conn)