@@ -20,6 +20,12 @@ def float_or_long(n):
20
20
else :
21
21
return long (n )
22
22
23
+ def long_or_none (r ):
24
+ "Return a long or None from a Redis reply"
25
+ if r :
26
+ return long (r )
27
+ return r
28
+
23
29
def json_or_none (r ):
24
30
"Return a deserialized JSON object or None"
25
31
if r :
@@ -53,8 +59,15 @@ class Client(StrictRedis):
53
59
'JSON.SET' : lambda r : r and nativestr (r ) == 'OK' ,
54
60
'JSON.NUMINCRBY' : float_or_long ,
55
61
'JSON.NUMMULTBY' : float_or_long ,
56
- 'JSON.STRAPPEND' : long ,
57
- 'JSON.STRLEN' : long ,
62
+ 'JSON.STRAPPEND' : long_or_none ,
63
+ 'JSON.STRLEN' : long_or_none ,
64
+ 'JSON.ARRAPPEND' : long_or_none ,
65
+ 'JSON.ARRINDEX' : long_or_none ,
66
+ 'JSON.ARRINSERT' : long_or_none ,
67
+ 'JSON.ARRLEN' : long_or_none ,
68
+ 'JSON.ARRPOP' : json_or_none ,
69
+ 'JSON.ARRTRIM' : long_or_none ,
70
+ 'JSON.OBJLEN' : long_or_none ,
58
71
}
59
72
60
73
def __init__ (self , ** kwargs ):
@@ -150,12 +163,78 @@ def JSONNumMultBy(self, name, path, number):
150
163
151
164
def JSONStrAppend (self , name , string , path = Path .rootPath ()):
152
165
"""
153
- Appends to the string JSON value under ``path`` at key ``name`` the provided ``string``
166
+ Appends to the string JSON value under ``path`` at key ``name`` the
167
+ provided ``string``
154
168
"""
155
169
return self .execute_command ('JSON.STRAPPEND' , name , str_path (path ), json .dumps (string ))
156
170
157
171
def JSONStrLen (self , name , path = Path .rootPath ()):
158
172
"""
159
- Returns the length of the string JSON value under ``path`` at key ``name``
173
+ Returns the length of the string JSON value under ``path`` at key
174
+ ``name``
160
175
"""
161
176
return self .execute_command ('JSON.STRLEN' , name , str_path (path ))
177
+
178
+ def JSONArrAppend (self , name , path = Path .rootPath (), * args ):
179
+ """
180
+ Appends the objects ``args`` to the array under the ``path` in key
181
+ ``name``
182
+ """
183
+ pieces = [name , str_path (path )]
184
+ for o in args :
185
+ pieces .append (json .dumps (o ))
186
+ return self .execute_command ('JSON.ARRAPPEND' , * pieces )
187
+
188
+ def JSONArrIndex (self , name , path , scalar , start = 0 , stop = - 1 ):
189
+ """
190
+ Returns the index of ``scalar`` in the JSON array under ``path`` at key
191
+ ``name``. The search can be limited using the optional inclusive
192
+ ``start`` and exclusive ``stop`` indices.
193
+ """
194
+ return self .execute_command ('JSON.ARRINDEX' , name , str_path (path ), json .dumps (scalar ), start , stop )
195
+
196
+ def JSONArrInsert (self , name , path , index , * args ):
197
+ """
198
+ Inserts the objects ``args`` to the array at index ``index`` under the
199
+ ``path` in key ``name``
200
+ """
201
+ pieces = [name , str_path (path ), index ]
202
+ for o in args :
203
+ pieces .append (json .dumps (o ))
204
+ return self .execute_command ('JSON.ARRINSERT' , * pieces )
205
+
206
+ def JSONArrLen (self , name , path = Path .rootPath ()):
207
+ """
208
+ Returns the length of the array JSON value under ``path`` at key
209
+ ``name``
210
+ """
211
+ return self .execute_command ('JSON.ARRLEN' , name , str_path (path ))
212
+
213
+ def JSONArrPop (self , name , path = Path .rootPath (), index = - 1 ):
214
+ """
215
+ Pops the element at ``index`` in the array JSON value under ``path`` at
216
+ key ``name``
217
+ """
218
+ return self .execute_command ('JSON.ARRPOP' , name , str_path (path ), index )
219
+
220
+ def JSONArrTrim (self , name , path , start , stop ):
221
+ """
222
+ Trim the array JSON value under ``path`` at key ``name`` to the
223
+ inclusive range given by ``start`` and ``stop``
224
+ """
225
+ return self .execute_command ('JSON.ARRTRIM' , name , str_path (path ), start , stop )
226
+
227
+ def JSONObjKeys (self , name , path = Path .rootPath ()):
228
+ """
229
+ Returns the key names in the dictionary JSON value under ``path`` at key
230
+ ``name``
231
+ """
232
+ return self .execute_command ('JSON.OBJKEYS' , name , str_path (path ))
233
+
234
+ def JSONObjLen (self , name , path = Path .rootPath ()):
235
+ """
236
+ Returns the length of the dictionary JSON value under ``path`` at key
237
+ ``name``
238
+ """
239
+ return self .execute_command ('JSON.OBJLEN' , name , str_path (path ))
240
+
0 commit comments