Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Redis String Data Type: Complete Command Reference

Tech May 9 3

SET

127.0.0.1:6379> set user_id 42
OK

GET

127.0.0.1:6379> get user_id
42

#Retrieve all keys
127.0.0.1:6379> keys *
item3
item2
item1

SET (Update)

127.0.0.1:6379> set user_id 99
OK
127.0.0.1:6379> get user_id
99

DEL

127.0.0.1:6379> del user_id
1
127.0.0.1:6379> get user_id
(nil)

EXISTS

127.0.0.1:6379> EXISTS username
0
127.0.0.1:6379> SET username admin
OK
127.0.0.1:6379> EXISTS username
1

TTL

127.0.0.1:6379> TTL username
-1		#No expiration

127.0.0.1:6379> TTL username
3		#Expires in 3 seconds

127.0.0.1:6379> TTL username
-2		#Key has expired

Setting Expiration: PX / EX

#Set expiration in milliseconds
127.0.0.1:6379> set username john px 10000
OK

#Set expiration in seconds
127.0.0.1:6379> set password secret ex 10
OK

SETEX (Combine SET and EX)

127.0.0.1:6379> SETEX username 10 john
OK

PSETEX (Combine SET and PX)

127.0.0.1:6379> PSETEX username 1000 john
OK

NX - Set Only If Key Does Not Exist

127.0.0.1:6379> set counter 100
OK
127.0.0.1:6379> set counter 200 nx		#Key counter exists, value unchanged
(nil)
127.0.0.1:6379> get counter
100

127.0.0.1:6379> set new_counter 300 nx		#Key new_counter does not exist, creates it
OK
127.0.0.1:6379> get new_counter
300

SETNX

127.0.0.1:6379> SETNX counter 999
0
127.0.0.1:6379> get counter
100
127.0.0.1:6379> SETNX total 500
1
127.0.0.1:6379> get total
500

XX - Set Only If Key Exists

127.0.0.1:6379> get counter
100
127.0.0.1:6379> set counter 888 xx
OK
127.0.0.1:6379> get counter
888
127.0.0.1:6379> set missing_key 999 xx
(nil)
127.0.0.1:6379> get missing_key
(nil)

MSET - Set Multiple Values

127.0.0.1:6379> MSET product_id 25 price 49.99 quantity 100
OK
127.0.0.1:6379> keys *
product_id
price
quantity
127.0.0.1:6379> get product_id
25
127.0.0.1:6379> get price
49.99
127.0.0.1:6379> get quantity
100

GETSET

127.0.0.1:6379> get product_id
25
127.0.0.1:6379> getset product_id 30
25
127.0.0.1:6379> get product_id
30

SETRANGE - Update by Endex

127.0.0.1:6379> set code abcdefg
OK
127.0.0.1:6379> get code
abcdefg
127.0.0.1:6379> SETRANGE code 3 XX
7
127.0.0.1:6379> get code
abcXXfg

MGET - Get Multiple Values

127.0.0.1:6379> get code
abcXXfg
127.0.0.1:6379> get price
49.99
127.0.0.1:6379> MGET code price
abcXXfg
49.99

GETRANGE - Substring

127.0.0.1:6379> get code
abcdefg
127.0.0.1:6379> GETRANGE code 2 -1		#Get all from index 2
cdefg
127.0.0.1:6379> GETRANGE code 2 2		#Get single character at index 2
c
127.0.0.1:6379> GETRANGE code 2 4		#Get range from index 2 to 4
cde

INCR - Increment

127.0.0.1:6379> set views 1
OK
127.0.0.1:6379> INCR views
2
127.0.0.1:6379> INCR views
3
127.0.0.1:6379> INCR views
4
127.0.0.1:6379> INCR views
5

DECR - Decrement

127.0.0.1:6379> get views
5
127.0.0.1:6379> DECR views
4
127.0.0.1:6379> DECR views
3
127.0.0.1:6379> DECR views
2
127.0.0.1:6379> DECR views
1

INCRBY - Increment by Step

127.0.0.1:6379> get balance
10
127.0.0.1:6379> INCRBY balance 50
60
127.0.0.1:6379> INCRBY balance 50
110
127.0.0.1:6379> INCRBY balance 50
160
127.0.0.1:6379> INCRBY balance 50
210
127.0.0.1:6379> INCRBY balance 50
260

DECRBY - Decrement by Step

127.0.0.1:6379> get balance
260
127.0.0.1:6379> DECRBY balance 30
230
127.0.0.1:6379> DECRBY balance 30
200
127.0.0.1:6379> DECRBY balance 30
170
127.0.0.1:6379> DECRBY balance 30
140

APPEND

127.0.0.1:6379> get message
hi
127.0.0.1:6379> APPEND message world
8
127.0.0.1:6379> get message
hiworld

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.