@@ -1689,6 +1689,57 @@ def get(self, name):
1689
1689
"""
1690
1690
return self .execute_command ('GET' , name )
1691
1691
1692
+ def getex (self , name ,
1693
+ ex = None , px = None , exat = None , pxat = None , persist = False ):
1694
+ """
1695
+ Get the value of key and optionally set its expiration.
1696
+ GETEX is similar to GET, but is a write command with
1697
+ additional options. All time parameters can be given as
1698
+ datetime.timedelta or integers.
1699
+
1700
+ ``ex`` sets an expire flag on key ``name`` for ``ex`` seconds.
1701
+
1702
+ ``px`` sets an expire flag on key ``name`` for ``px`` milliseconds.
1703
+
1704
+ ``exat`` sets an expire flag on key ``name`` for ``ex`` seconds,
1705
+ specified in unix time.
1706
+
1707
+ ``pxat`` sets an expire flag on key ``name`` for ``ex`` milliseconds,
1708
+ specified in unix time.
1709
+
1710
+ ``persist`` remove the time to live associated with ``name``.
1711
+ """
1712
+
1713
+ pieces = []
1714
+ # similar to set command
1715
+ if ex is not None :
1716
+ pieces .append ('EX' )
1717
+ if isinstance (ex , datetime .timedelta ):
1718
+ ex = int (ex .total_seconds ())
1719
+ pieces .append (ex )
1720
+ if px is not None :
1721
+ pieces .append ('PX' )
1722
+ if isinstance (px , datetime .timedelta ):
1723
+ px = int (px .total_seconds () * 1000 )
1724
+ pieces .append (px )
1725
+ # similar to pexpireat command
1726
+ if exat is not None :
1727
+ pieces .append ('EXAT' )
1728
+ if isinstance (exat , datetime .datetime ):
1729
+ s = int (exat .microsecond / 1000000 )
1730
+ exat = int (mod_time .mktime (exat .timetuple ())) + s
1731
+ pieces .append (exat )
1732
+ if pxat is not None :
1733
+ pieces .append ('PXAT' )
1734
+ if isinstance (pxat , datetime .datetime ):
1735
+ ms = int (pxat .microsecond / 1000 )
1736
+ pxat = int (mod_time .mktime (pxat .timetuple ())) * 1000 + ms
1737
+ pieces .append (pxat )
1738
+ if persist :
1739
+ pieces .append ('PERSIST' )
1740
+
1741
+ return self .execute_command ('GETEX' , name , * pieces )
1742
+
1692
1743
def __getitem__ (self , name ):
1693
1744
"""
1694
1745
Return the value at key ``name``, raises a KeyError if the key
0 commit comments