Show List

Working with keys in Redis

In Redis, keys are used to store and retrieve data. Here's an explanation of some of the ways to work with keys in Redis along with examples:

  • SET key value: Set a key to hold a string value.

sql
Copy code
SET name "John"
  • GET key: Get the value of a key.

  • sql
    Copy code
    GET name
  • EXISTS key: Check if a key exists.

  • sql
    Copy code
    EXISTS name
  • DEL key: Delete a key.

  • css
    Copy code
    DEL name
  • INCR key: Increment the value of a key by 1.

  • sql
    Copy code
    SET counter 0 INCR counter
  • EXPIRE key seconds: Set a timeout on a key, after which the key will be automatically deleted.

  • sql
    Copy code
    SET session_id "abcd1234" EXPIRE session_id 3600
  • KEYS pattern: Return all keys matching a given pattern.

  • sql
    Copy code
    SET user:1 name "John" SET user:2 name "Jane" KEYS user:*

    These are just a few examples of the many ways to work with keys in Redis. Redis offers a wide range of key-based operations, which can be used to build sophisticated applications.


        Leave a Comment


    • captcha text