@@ -98,6 +98,32 @@ def listen(self):
98
98
except ValueError :
99
99
pass #Ignore non table-formated redis entries
100
100
101
+ def __fire_with_op (self , table , key , data , op_str = 'add' ):
102
+ if self .handlers .has_key (table ):
103
+ handler = self .handlers [table ]
104
+ handler (table , key , data , op_str )
105
+
106
+ def listen_with_op (self ):
107
+ """Start listen Redis keyspace events and will trigger corresponding handlers when content of a table changes.
108
+ """
109
+ self .pubsub = self .redis_clients [self .CONFIG_DB ].pubsub ()
110
+ self .pubsub .psubscribe ("__keyspace@{}__:*" .format (self .db_map [self .CONFIG_DB ]['db' ]))
111
+ for item in self .pubsub .listen ():
112
+ if item ['type' ] == 'pmessage' :
113
+ key = item ['channel' ].split (':' , 1 )[1 ]
114
+ try :
115
+ (table , row ) = key .split (self .TABLE_NAME_SEPARATOR , 1 )
116
+ if self .handlers .has_key (table ):
117
+ client = self .redis_clients [self .CONFIG_DB ]
118
+ data = self .__raw_to_typed (client .hgetall (key ))
119
+ op = client .keys (key )
120
+ op_str = 'add'
121
+ if len (op ) == 0 :
122
+ op_str = 'del'
123
+ self .__fire_with_op (table , row , data , op_str )
124
+ except ValueError :
125
+ pass #Ignore non table-formated redis entries
126
+
101
127
def __raw_to_typed (self , raw_data ):
102
128
if raw_data == None :
103
129
return None
0 commit comments