474834 (2) [Avatar] Offline
#1
        elif conn.ttl(lockname) < 0:                    
            conn.expire(lockname, lock_timeout) 

It looks like the expiration time will be reset once the lock expires. If the process acquired the lock crashes, wouldn't these two lines make the lock live forever?
Can you explain in which situation we will need this piece of code and what's incorrect with my concern?
Thanks!
josiah.carlson (79) [Avatar] Offline
#2
Your understanding of what is happening here is incorrect.

We set the expiration time on a key that does not have an expiration time, because that expiration was never set.

Keys that are expired are deleted and don't exist, so won't be inside that elif clause.

Note that this code and the book were written before you could pass both EX and NX to the SET command, which is why we use SETNX with a later EXPIRE.

If the book were to be written (or a new edition released) today, it would be a single SET call with both the EX and NX clauses, instead of these two steps/options/clauses.
474834 (2) [Avatar] Offline
#3
Thanks! It helps a lot