Sobes.tech
Junior — Middle

Can you list the main commands used in Redis and briefly describe their purposes?

sobes.tech AI

Answer from AI

Basic Redis commands and their purposes:

  • SET key value — set the value for a key.
  • GET key — get the value of a key.
  • DEL key — delete a key.
  • EXPIRE key seconds — set the lifetime of a key.
  • LPUSH key value — add an element to the beginning of a list.
  • RPUSH key value — add an element to the end of a list.
  • LPOP key — remove and return the first element of a list.
  • SADD key member — add an element to a set.
  • SMEMBERS key — get all elements of a set.
  • HSET key field value — set a field in a hash.
  • HGET key field — get the value of a field in a hash.

Example of usage in Go with the go-redis library:

client := redis.NewClient(&redis.Options{
    Addr: "localhost:6379",
})

err := client.Set(ctx, "key", "value", 0).Err()
val, err := client.Get(ctx, "key").Result()