Redis basic commands with example
set:- Set key
to hold the string value
. If key
already holds a value, it is overwritten, regardless of its type.
The SET command supports a set of options that modify its behavior:
EX
seconds -- Set the specified expire time, in seconds.PX
milliseconds -- Set the specified expire time, in milliseconds.EXAT
timestamp-seconds -- Set the specified Unix time at which the key will expire, in seconds.PXAT
timestamp-milliseconds -- Set the specified Unix time at which the key will expire, in milliseconds.NX
-- Only set the key if it does not already exist.XX
-- Only set the key if it already exist.KEEPTTL
-- Retain the time to live associated with the key.GET
-- Return the old string stored at key, or nil if key did not exist. An error is returned and SET aborted if the value stored at key is not a string.
get:- Get the value of key
. If the key does not exist the special value nil
is returned.
delete:- Removes the specified keys. A key is ignored if it does not exist.
lpush:- Insert all the specified values at the head of the list stored at key
. If key
does not exist, it is created as empty list before performing the push operations. When key
holds a value that is not a list, an error is returned.
rpush:- Insert all the specified values at the tail of the list stored at key
. If key
does not exist, it is created as empty list before performing the push operation. When key
holds a value that is not a list, an error is returned.
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
lindex:- Returns the element at index index
in the list stored at key
. The index is zero-based, so 0
means the first element, 1
the second element and so on.
ltrim:- Trim an existing list so that it will contain only the specified range of elements specified. Both start
and stop
are zero-based indexes, where 0
is the first element of the list (the head), 1
the next element and so on.
lpop:- Removes and returns the first elements of the list stored at key
.
By default, the command pops a single element from the beginning of the list.
rpop:-Removes and returns the last elements of the list stored at key
.
By default, the command pops a single element from the end of the list.
blpop:- BLPOP is a blocking list pop primitive. It is the blocking version of LPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the head of the first list that is non-empty, with the given keys being checked in the order that they are given.
brpop:- BRPOP is a blocking list pop primitive. It is the blocking version of RPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the tail of the first list that is non-empty, with the given keys being checked in the order that they are given.
incr:- Increments the number stored at key
by one. If the key does not exist, it is set to 0
before performing the operation.
decr:- Decrements the number stored at key
by one. If the key does not exist, it is set to 0
before performing the operation.
That’s it for this time! I hope you enjoyed this post. As always, I welcome questions, notes, comments and requests for posts on topics you’d like to read. See you next time! Happy Coding !!!!!