TOUCH

Introduction and Use Case(s)

The TOUCH command in Redis is used to update the last access time of one or more keys without altering their values. This can be useful for cache management, where you need to keep certain keys from expiring by marking them as recently used.

Syntax

  1. TOUCH key [key ...]

Parameter Explanations

  • key: The name of the key whose last access time you want to update. You can specify multiple keys.

Return Values

The TOUCH command returns an integer indicating the number of keys that were successfully updated.

Example:

If you run TOUCH mykey1 mykey2 and both keys exist, the command might return:

  1. (integer) 2

Code Examples

  1. dragonfly> SET mykey1 "value1"
  2. OK
  3. dragonfly> SET mykey2 "value2"
  4. OK
  5. dragonfly> TOUCH mykey1 mykey2
  6. (integer) 2
  7. dragonfly> TOUCH mykey3
  8. (integer) 0

Best Practices

  • Use TOUCH to extend the life of frequently accessed keys in a cache without modifying their values.
  • Monitor the number of keys updated using the return value to ensure your intended keys are being touched.

Common Mistakes

  • Using TOUCH on keys that don’t exist will not result in an error but will return 0, indicating no keys were touched.
  • Assuming TOUCH modifies the key’s value, it only updates the last access time.

FAQs

Does the TOUCH command alter the TTL (Time to Live) of a key?

No, the TOUCH command only updates the last access time of the key; it does not change its TTL.

Can I use TOUCH with expired keys?

No, TOUCH has no effect on expired keys since they are considered non-existent in Redis.