@@ -13,6 +13,19 @@ def str_path(p):
13
13
else :
14
14
return p
15
15
16
+ def float_or_long (n ):
17
+ "Return a number from a Redis reply"
18
+ if isinstance (n , str ):
19
+ return float (n )
20
+ else :
21
+ return long (n )
22
+
23
+ def json_or_none (r ):
24
+ "Return a deserialized JSON object or None"
25
+ if r :
26
+ return json .loads (r )
27
+ return r
28
+
16
29
def bulk_of_jsons (b ):
17
30
"Replace serialized JSON values with objects in a bulk array response (list)"
18
31
for index , item in enumerate (b ):
@@ -34,17 +47,19 @@ class Client(StrictRedis):
34
47
}
35
48
36
49
MODULE_CALLBACKS = {
37
- 'JSON.DEL' : lambda r : int ( r ) ,
38
- 'JSON.GET' : lambda r : json . loads ( r ) ,
50
+ 'JSON.DEL' : long ,
51
+ 'JSON.GET' : json_or_none ,
39
52
'JSON.MGET' : bulk_of_jsons ,
40
53
'JSON.SET' : lambda r : r and nativestr (r ) == 'OK' ,
54
+ 'JSON.NUMINCRBY' : float_or_long ,
55
+ 'JSON.NUMMULTBY' : float_or_long ,
56
+ 'JSON.STRAPPEND' : long ,
57
+ 'JSON.STRLEN' : long ,
41
58
}
42
59
43
60
def __init__ (self , ** kwargs ):
44
61
super (Client , self ).__init__ (** kwargs )
45
-
46
62
self .__checkPrerequirements ()
47
-
48
63
# Inject the callbacks for the module's commands
49
64
self .response_callbacks .update (self .MODULE_CALLBACKS )
50
65
@@ -103,7 +118,6 @@ def JSONSet(self, name, path, obj, nx=False, xx=False):
103
118
``xx`` if set to True, set ``value`` only if it exists
104
119
"""
105
120
pieces = [name , str_path (path ), json .dumps (obj )]
106
-
107
121
# Handle existential modifiers
108
122
if nx and xx :
109
123
raise Exception ('nx and xx are mutually exclusive: use one, the '
@@ -112,11 +126,36 @@ def JSONSet(self, name, path, obj, nx=False, xx=False):
112
126
pieces .append ('NX' )
113
127
elif xx :
114
128
pieces .append ('XX' )
115
-
116
129
return self .execute_command ('JSON.SET' , * pieces )
117
130
118
131
def JSONType (self , name , path = Path .rootPath ()):
119
132
"""
120
133
Gets the type of the JSON value under ``path`` from key ``name``
121
134
"""
122
135
return self .execute_command ('JSON.TYPE' , name , str_path (path ))
136
+
137
+ def JSONNumIncrBy (self , name , path , number ):
138
+ """
139
+ Increments the numeric (integer or floating point) JSON value under
140
+ ``path`` at key ``name`` by the provided ``number``
141
+ """
142
+ return self .execute_command ('JSON.NUMINCRBY' , name , str_path (path ), json .dumps (number ))
143
+
144
+ def JSONNumMultBy (self , name , path , number ):
145
+ """
146
+ Multiplies the numeric (integer or floating point) JSON value under
147
+ ``path`` at key ``name`` with the provided ``number``
148
+ """
149
+ return self .execute_command ('JSON.NUMMULTBY' , name , str_path (path ), json .dumps (number ))
150
+
151
+ def JSONStrAppend (self , name , string , path = Path .rootPath ()):
152
+ """
153
+ Appends to the string JSON value under ``path`` at key ``name`` the provided ``string``
154
+ """
155
+ return self .execute_command ('JSON.STRAPPEND' , name , str_path (path ), json .dumps (string ))
156
+
157
+ def JSONStrLen (self , name , path = Path .rootPath ()):
158
+ """
159
+ Returns the length of the string JSON value under ``path`` at key ``name``
160
+ """
161
+ return self .execute_command ('JSON.STRLEN' , name , str_path (path ))
0 commit comments