Redis Cheatsheet

Commands::

# ---------------------------
# Keys Operations
# ---------------------------

EXISTS key                  # Check if a key exists. Returns 1 if exists, 0 otherwise.
DEL key                     # Delete a key.
EXPIRE key seconds          # Set a time-to-live (TTL) in seconds for a key.
TTL key                     # Get the remaining TTL for a key (-2 if non-existent, -1 if no TTL).
KEYS pattern                # List all keys matching a pattern.
RENAME oldKey newKey        # Rename a key.
TYPE key                    # Get the data type of the value stored at key.

# ---------------------------
# String Operations
# ---------------------------

SET key value               # Set a value for a key.
GET key                     # Get the value of a key.
SETEX key seconds value     # Set a key with an expiration time (in seconds).
INCR key                    # Increment the integer value of a key by 1.
DECR key                    # Decrement the integer value of a key by 1.
APPEND key value            # Append value to the end of the key's current value.
GETRANGE key start end      # Get a substring of the string value stored at key.

# ---------------------------
# Hash Operations
# ---------------------------

HSET key field value        # Set the value of a field in a hash.
HGET key field              # Get the value of a field in a hash.
HDEL key field              # Delete a field from a hash.
HGETALL key                 # Get all fields and values in a hash.
HINCRBY key field increment # Increment a hash field's value by a given amount.
HLEN key                    # Get the number of fields in a hash.

# ---------------------------
# List Operations
# ---------------------------

LPUSH key value             # Prepend a value to the list.
RPUSH key value             # Append a value to the list.
LPOP key                    # Remove and return the first element of the list.
RPOP key                    # Remove and return the last element of the list.
LRANGE key start stop       # Get a range of elements from the list.
LLEN key                    # Get the length of the list.
LREM key count value        # Remove count occurrences of value from the list.

# ---------------------------
# Set Operations
# ---------------------------

SADD key value              # Add one or more members to a set.
SREM key value              # Remove one or more members from a set.
SISMEMBER key value         # Check if a value is a member of a set (returns 1 or 0).
SMEMBERS key                # Get all members of a set.
SCARD key                   # Get the number of members in a set.
SDIFF key1 key2             # Get members in key1 but not in key2.

# ---------------------------
# Sorted Set (ZSet) Operations
# ---------------------------

ZADD key score member       # Add a member to a sorted set with a score.
ZREM key member             # Remove a member from a sorted set.
ZRANGE key start stop       # Get members in a range (sorted by score).
ZREVRANGE key start stop    # Get members in reverse order (sorted by score).
ZSCORE key member           # Get the score of a member in a sorted set.
ZRANK key member            # Get the rank of a member (sorted by score ascending).

# ---------------------------
# Pub/Sub Operations
# ---------------------------

PUBLISH channel message     # Publish a message to a channel.
SUBSCRIBE channel           # Subscribe to a channel to listen for messages.
UNSUBSCRIBE channel         # Unsubscribe from a channel.

# ---------------------------
# Transactions
# ---------------------------

MULTI                      # Start a transaction.
EXEC                       # Execute all commands in the transaction.
DISCARD                    # Cancel the transaction.
WATCH key                  # Watch a key for changes to abort the transaction.

# ---------------------------
# Scripting (Lua)
# ---------------------------

EVAL script numkeys key1.. # Evaluate a Lua script directly on the server.
EVALSHA sha numkeys keys   # Execute a cached Lua script.

# ---------------------------
# HyperLogLog
# ---------------------------

PFADD key element          # Add an element to a HyperLogLog (approximates cardinality).
PFCOUNT key                # Get the approximated cardinality from a HyperLogLog.

# ---------------------------
# Geospatial
# ---------------------------

GEOADD key longitude latitude member    # Add a geospatial item.
GEODIST key member1 member2 unit        # Get the distance between two members.
GEORADIUS key longitude latitude radius unit WITHCOORD # Get members within a radius.
GEORADIUSBYMEMBER key member radius unit WITHDIST     # Get members near a specific member.

# ---------------------------
# Stream Operations
# ---------------------------

XADD stream maxlen count id field value # Add an entry to a stream.
XREAD COUNT count STREAMS key id        # Read data from one or more streams.
XDEL stream id                          # Delete an entry from a stream.

Example::

# ---------------------------
# 1. Keys Management
# ---------------------------

EXISTS key
# Checks if a key exists.
# Example:
SET myKey "value"
EXISTS myKey       # Output: 1 (key exists)
EXISTS unknownKey  # Output: 0 (key does not exist)

DEL key
# Deletes one or more keys.
# Example:
SET myKey "value"
DEL myKey
EXISTS myKey       # Output: 0 (key no longer exists)

EXPIRE key seconds
# Sets a time-to-live (TTL) for a key in seconds.
# Example:
SET tempKey "value"
EXPIRE tempKey 10  # Key will expire after 10 seconds
TTL tempKey        # Output: 10 (remaining TTL)

TTL key
# Gets the remaining TTL for a key.
# Example:
SET tempKey "value"
EXPIRE tempKey 10
TTL tempKey        # Output: 10
TTL unknownKey     # Output: -2 (key does not exist)

KEYS pattern
# Lists all keys matching a pattern.
# Example:
SET key1 "value1"
SET key2 "value2"
KEYS key*          # Output: ["key1", "key2"]

RENAME oldKey newKey
# Renames a key.
# Example:
SET oldKey "value"
RENAME oldKey newKey
EXISTS oldKey      # Output: 0
EXISTS newKey      # Output: 1

TYPE key
# Gets the data type of the value stored at a key.
# Example:
SET myKey "value"
TYPE myKey         # Output: "string"

# ---------------------------
# 2. String Operations
# ---------------------------

SET key value
# Sets a value for a key.
# Example:
SET myKey "value"

GET key
# Gets the value of a key.
# Example:
GET myKey          # Output: "value"

SETEX key seconds value
# Sets a key with an expiration time (in seconds).
# Example:
SETEX tempKey 10 "value"
TTL tempKey        # Output: 10

INCR key
# Increments the integer value of a key by 1.
# Example:
SET counter 5
INCR counter       # Output: 6

DECR key
# Decrements the integer value of a key by 1.
# Example:
SET counter 5
DECR counter       # Output: 4

APPEND key value
# Appends value to the end of the key's current value.
# Example:
SET myKey "Hello"
APPEND myKey ", World!"
GET myKey          # Output: "Hello, World!"

GETRANGE key start end
# Gets a substring of the string value stored at a key.
# Example:
SET myKey "Hello, World!"
GETRANGE myKey 0 4 # Output: "Hello"

# ---------------------------
# 3. Hash Operations
# ---------------------------

HSET key field value
# Sets the value of a field in a hash.
# Example:
HSET myHash field1 "value1"

HGET key field
# Gets the value of a field in a hash.
# Example:
HGET myHash field1 # Output: "value1"

HDEL key field
# Deletes a field from a hash.
# Example:
HSET myHash field1 "value1"
HDEL myHash field1
HGET myHash field1 # Output: (nil)

HGETALL key
# Gets all fields and values in a hash.
# Example:
HSET myHash field1 "value1" field2 "value2"
HGETALL myHash     # Output: {"field1": "value1", "field2": "value2"}

HINCRBY key field increment
# Increments a hash field's value by a given amount.
# Example:
HSET myHash counter 5
HINCRBY myHash counter 2 # Output: 7

HLEN key
# Gets the number of fields in a hash.
# Example:
HSET myHash field1 "value1" field2 "value2"
HLEN myHash        # Output: 2

# ---------------------------
# 4. List Operations
# ---------------------------

LPUSH key value
# Prepends a value to the list.
# Example:
LPUSH myList "item1" "item2"

RPUSH key value
# Appends a value to the list.
# Example:
RPUSH myList "item3"

LPOP key
# Removes and returns the first element of the list.
# Example:
LPUSH myList "item1" "item2"
LPOP myList        # Output: "item2"

RPOP key
# Removes and returns the last element of the list.
# Example:
RPUSH myList "item1" "item2"
RPOP myList        # Output: "item2"

LRANGE key start stop
# Gets a range of elements from the list.
# Example:
LPUSH myList "item1" "item2" "item3"
LRANGE myList 0 -1 # Output: ["item3", "item2", "item1"]

LLEN key
# Gets the length of the list.
# Example:
LPUSH myList "item1" "item2"
LLEN myList        # Output: 2

LREM key count value
# Removes count occurrences of value from the list.
# Example:
LPUSH myList "item1" "item2" "item1"
LREM myList 1 "item1"
LRANGE myList 0 -1 # Output: ["item2", "item1"]

# ---------------------------
# 5. Set Operations
# ---------------------------

SADD key value
# Adds one or more members to a set.
# Example:
SADD mySet "item1" "item2"

SREM key value
# Removes one or more members from a set.
# Example:
SREM mySet "item1"

SISMEMBER key value
# Checks if a value is a member of a set.
# Example:
SISMEMBER mySet "item1" # Output: 1 (true)

SMEMBERS key
# Gets all members of a set.
# Example:
SMEMBERS mySet      # Output: ["item2"]

SCARD key
# Gets the number of members in a set.
# Example:
SCARD mySet         # Output: 1

SDIFF key1 key2
# Gets members in key1 but not in key2.
# Example:
SADD set1 "a" "b"
SADD set2 "b" "c"
SDIFF set1 set2     # Output: ["a"]