redis / docs

Documentation for Redis, Redis Cloud, and Redis Enterprise
https://redis.io/docs/latest/
Other
23 stars 116 forks source link

Feedback: Redis hashes Expiration - Typo in keys and Error in return values #594

Closed aneroid closed 1 month ago

aneroid commented 2 months ago

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':

event = {
    'air_quality': 256,
    'Battery_level':89
}

client.hset('sensor:sensor1', mapping=event)

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:

event = {
    'air_quality': 256,
    'battery_level':89
}

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.

dwdougherty commented 1 month ago

Thank you, @aneroid . I'll get this corrected.