Sobes.tech
Junior — Middle

What are the main key operations available in Redis?

sobes.tech AI

Answer from AI

The following main operations on keys are available in Redis:

  • SET key value — set the value of a key.
  • GET key — get the value of a key.
  • DEL key [key ...] — delete one or more keys.
  • EXISTS key [key ...] — check if a key exists.
  • EXPIRE key seconds — set the lifetime of a key in seconds.
  • TTL key — get the remaining lifetime of a key.
  • KEYS pattern — get a list of keys matching a pattern (not recommended for production due to performance issues).
  • SCAN cursor [MATCH pattern] [COUNT count] — iterative search for keys with optional filtering.
  • RENAME key newkey — rename a key.
  • TYPE key — find out the type of the value stored at a key.

Example of usage in Python with the redis library:

import redis

r = redis.Redis()
r.set('mykey', 'value')
value = r.get('mykey')
r.expire('mykey', 60)  # the key will expire in 60 seconds
keys = r.keys('*')