SET

Sets the value of the specified key.

Syntax:

SET key value

Example:

SET user:123 name “John Doe” email “john@example.com” age 30

Time Complexity: O(1)

Complete Guide to Redis Commands

Redis is an open-source, advanced key-value store and an apt solution for building high-performance, scalable web applications. Redis has three main peculiarities that set it apart:

  • Redis holds its database entirely in the memory, using the disk only for persistence.
  • Redis has a relatively rich set of data types compared to many key-value data stores.
  • Redis can replicate data to any number of slaves.

Table of Content

  • 1. KEY-VALUE OPERATIONS
  • 1.1) SET:
  • 1.2) GET:
  • 1.4) DEL:
  • 1.4) EXISTS:
  • 1.5) TTL:
  • 1.6) EXPIRE :
  • 1.7) INCR
  • 1.8) DECR
  • 1.9) APPEND
  • 2. LISTS:
  • 2.1) LPUSH
  • 2.2) RPUSH
  • 2.3) LPOP
  • 2.4) RPOP
  • 2.5) LLEN
  • 2.5) LRANGE
  • 3. SETS:
  • 3.1) “sadd” command:
  • 3.2) “smembers” command:
  • 3.3) “scard” command:
  • 3.4) “sismember” command:
  • 3.5) “sdiff” command:
  • 3.6) “sdiffstore” command:
  • 3.7) “sinter” command:
  • 3.8) “sinterstore” command:
  • 3.9) “sunion” command:
  • 3.10) “sunionstore” command:
  • 3.11) “srem” command:
  • 4. HASHES:
  • 4.1) HSET (Hash Set):
  • 4.2) HGET (Hash Get):
  • 4.3) HDEL (Hash Delete):
  • 4.4) HGETALL (Hash Get All):
  • 4.5) HKEYS (Hash Keys):
  • 4.6) HVALS (Hash Values):
  • 5. PUB/SUB:
  • 5.1) PUBLISH
  • 5.2) SUBSCRIBE
  • 5.3) UNSUBSCRIBE
  • 6. OTHER COMMANDS:
  • 6.1) SELECT
  • 6.2) FLUSHDB
  • 6.3) SAVE
  • 6.4) BGSAVE

Similar Reads

1. KEY-VALUE OPERATIONS

1.1) SET:...

1.1) SET:

Sets the value of the specified key....

1.2) GET:

Retrieves the value associated with the specified key....

1.4) DEL:

Deletes one or more keys and their associated values.Syntax:...

1.4) EXISTS:

Checks if the specified key exists in the database.Syntax:...

1.5) TTL:

Get the remaining time to live of a key in seconds....

1.6) EXPIRE :

Set a key’s time to live in seconds.Syntax:...

1.7) INCR

Increments the number stored at a key by one....

1.8) DECR

Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers....

1.9) APPEND

The amortized time complexity is O(1) assuming the appended value is small and the already present value is of any size, since the dynamic string library used by Redis will double the free space available on every reallocation.If key already exists and is a string, this command appends the value at the end of the string. If key does not exist it is created and set as an empty string, so APPEND will be similar to SET in this special case....

2. LISTS:

2.1) LPUSH...

2.1) LPUSH

adds a new element to the head of a list;...

2.2) RPUSH

adds to the tail....

2.3) LPOP

removes and returns an element from the head of a list....

2.4) RPOP

Does the same but from the tails of a list....

2.5) LLEN

returns the length of a list....

2.5) LRANGE

Returns the specified elements of the list stored at key. The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on....

3. SETS:

3.1) “sadd” command:...

3.1) “sadd” command:

Creates a set, and adds element to it....

3.2) “smembers” command:

Shows all the elements, present in that set....

3.3) “scard” command:

Shows no. of elements, present in that set.Syntax:...

3.4) “sismember” command:

Checks if that element exists in that set, if yes then returns 1Syntax:...

3.5) “sdiff” command:

Shows difference of two sets by displaying those elements, elements that are in set 1 butt not in set 2....

3.6) “sdiffstore” command:

Stores elements that differ in two sets, elements that are in set1 but not in set2 in a new setSyntax:...

3.7) “sinter” command:

Shows elements that are present in both sets that is intersection of 2 given setsSyntax:...

3.8) “sinterstore” command:

Stores elements that are present in both sets in a new set....

3.9) “sunion” command:

Shows all the unique elements that are there overall including those 2 sets as union of setsSyntax:...

3.10) “sunionstore” command:

Stores elements that are union of two sets in a new set...

3.11) “srem” command:

Removes a particular element from given set....

4. HASHES:

4.1) HSET (Hash Set):...

4.1) HSET (Hash Set):

Sets the value of a field in a Hash.If the field does not exist, it creates the field and assigns the value....

4.2) HGET (Hash Get):

Retrieves the value of a field in a Hash....

4.3) HDEL (Hash Delete):

Deletes one or more fields from a Hash.Syntax:...

4.4) HGETALL (Hash Get All):

Retrieves all fields and values of a Hash....

4.5) HKEYS (Hash Keys):

The HKEYS command retrieves all the field names in a hash.Syntax:...

4.6) HVALS (Hash Values):

The HVALS command retrieves all the values in a hash.Syntax:...

5. PUB/SUB:

Channels are the communication pathways in Redis Pub/Sub. Messages are published to specific channels, and subscribers listen to messages on one or more channels. Channels are identified by names, e.g., “news,” “chatroom,” “events,” etc. When a message is published on a channel, all subscribers in that channel receive the message....

5.1) PUBLISH

Publishers use the PUBLISH command to send messages to channels. The...

5.2) SUBSCRIBE

Subscribers use the SUBSCRIBE command to start listening to one or more channels. The...

5.3) UNSUBSCRIBE

Subscribers can unsubscribe from specific channels using the UNSUBSCRIBE command. The...

6. OTHER COMMANDS:

6.1) SELECT...

6.1) SELECT

Select the Redis logical database having the specified zero-based numeric index. New connections always use the database 0.Selectable Redis databases are a form of namespacing: all databases are still persisted in the same RDB / AOF file....

6.2) FLUSHDB

Delete all the keys of the currently selected DB. This command never fails.By default, FLUSHDB will synchronously flush all keys from the database. Starting with Redis 6.2, setting the lazyfree-lazy-user-flush configuration directive to “yes” changes the default flush mode to asynchronous....

6.3) SAVE

The SAVE commands performs a synchronous save of the dataset producing a point in time snapshot of all the data inside the Redis instance, in the form of an RDB file....

6.4) BGSAVE

Save the DB in background....

Contact Us