So when doing the 'set expiration' step, it's setting it for a field which doesn't exist, note the lower-case b for 'battery_level':
# set the TTL for two hash fields to 60 seconds
client.hexpire('sensor:sensor1', 60, 'air_quality', 'battery_level')
And when retrieving the TTL, this code would return TWO values in a list, not one. If the field doesn't exist, the return value is -2; and the number of values in the list corresponds to the number of fields being checked.
Given code (incorrect):
ttl = client.httl('sensor:sensor1', 'air_quality', 'battery_level')
print(ttl)
# prints [60]
should be:
ttl = client.httl('sensor:sensor1', 'air_quality', 'battery_level')
print(ttl)
# prints [60, -2]
However, it would be better to fix the event dict to have the correct field names:
and the corresponding code for the multiple fields' TTL retrieval would be:
# set the TTL for two hash fields to 60 seconds
client.hexpire('sensor:sensor1', 60, 'air_quality', 'battery_level')
ttl = client.httl('sensor:sensor1', 'air_quality', 'battery_level')
print(ttl)
# prints [60, 60]
Side note: that page uses the variable name r for the redis connection for most of the start of the page but then uses client for the expiration examples without initialising client anywhere. This is not consistent and may be confusing.
Page https://redis.io/docs/latest/develop/data-types/hashes
For the "Field expiration examples", the code has a typo, note the upper-case B for
'Battery_level'
:So when doing the 'set expiration' step, it's setting it for a field which doesn't exist, note the lower-case b for
'battery_level'
:And when retrieving the TTL, this code would return TWO values in a list, not one. If the field doesn't exist, the return value is
-2
; and the number of values in the list corresponds to the number of fields being checked.Given code (incorrect):
should be:
However, it would be better to fix the
event
dict to have the correct field names:and the corresponding code for the multiple fields' TTL retrieval would be:
Side note: that page uses the variable name
r
for the redis connection for most of the start of the page but then usesclient
for the expiration examples without initialisingclient
anywhere. This is not consistent and may be confusing.