@@ -555,6 +555,20 @@ def parse_module_result(response):
555
555
return True
556
556
557
557
558
+ def parse_set_result (response , ** options ):
559
+ """
560
+ Handle SET result since GET argument is available since Redis 6.2.
561
+ Parsing SET result into:
562
+ - BOOL
563
+ - String when GET argument is used
564
+ """
565
+ if options .get ('get' ):
566
+ # Redis will return a getCommand result.
567
+ # See `setGenericCommand` in t_string.c
568
+ return response
569
+ return response and str_if_bytes (response ) == 'OK'
570
+
571
+
558
572
class Redis :
559
573
"""
560
574
Implementation of the Redis protocol.
@@ -683,7 +697,7 @@ class Redis:
683
697
'SENTINEL SENTINELS' : parse_sentinel_slaves_and_sentinels ,
684
698
'SENTINEL SET' : bool_ok ,
685
699
'SENTINEL SLAVES' : parse_sentinel_slaves_and_sentinels ,
686
- 'SET' : lambda r : r and str_if_bytes ( r ) == 'OK' ,
700
+ 'SET' : parse_set_result ,
687
701
'SLOWLOG GET' : parse_slowlog_get ,
688
702
'SLOWLOG LEN' : int ,
689
703
'SLOWLOG RESET' : bool_ok ,
@@ -1804,6 +1818,9 @@ def getset(self, name, value):
1804
1818
"""
1805
1819
Sets the value at key ``name`` to ``value``
1806
1820
and returns the old value at key ``name`` atomically.
1821
+
1822
+ As per Redis 6.2, GETSET is considered deprecated.
1823
+ Please use SET with GET parameter in new code.
1807
1824
"""
1808
1825
return self .execute_command ('GETSET' , name , value )
1809
1826
@@ -1964,7 +1981,7 @@ def restore(self, name, ttl, value, replace=False, absttl=False):
1964
1981
return self .execute_command ('RESTORE' , * params )
1965
1982
1966
1983
def set (self , name , value ,
1967
- ex = None , px = None , nx = False , xx = False , keepttl = False ):
1984
+ ex = None , px = None , nx = False , xx = False , keepttl = False , get = False ):
1968
1985
"""
1969
1986
Set the value at key ``name`` to ``value``
1970
1987
@@ -1980,8 +1997,13 @@ def set(self, name, value,
1980
1997
1981
1998
``keepttl`` if True, retain the time to live associated with the key.
1982
1999
(Available since Redis 6.0)
2000
+
2001
+ ``get`` if True, set the value at key ``name`` to ``value`` and return
2002
+ the old value stored at key, or None when key did not exist.
2003
+ (Available since Redis 6.2)
1983
2004
"""
1984
2005
pieces = [name , value ]
2006
+ options = {}
1985
2007
if ex is not None :
1986
2008
pieces .append ('EX' )
1987
2009
if isinstance (ex , datetime .timedelta ):
@@ -2001,7 +2023,11 @@ def set(self, name, value,
2001
2023
if keepttl :
2002
2024
pieces .append ('KEEPTTL' )
2003
2025
2004
- return self .execute_command ('SET' , * pieces )
2026
+ if get :
2027
+ pieces .append ('GET' )
2028
+ options ["get" ] = True
2029
+
2030
+ return self .execute_command ('SET' , * pieces , ** options )
2005
2031
2006
2032
def __setitem__ (self , name , value ):
2007
2033
self .set (name , value )
0 commit comments