Redis Series - Operating Commands and Data Types

Keywords: Java Redis Database Linux

In this chapter, we will briefly introduce the data types commonly used in Redis and some of the commands we commonly use, let's go.

Preparatory environment:

Redis. See the previous section for details.


The next command we use is the client tool that comes with redis. There is a redis cli under the directory where redis is installed. We can start it. Before we start, we need to start redis first.


[root@VM_0_10_centos bin]# ll
total 15524
-rw-r--r-- 1 root root      18 Sep 30 19:48 dump.rdb
-rwxr-xr-x 1 root root 4588958 Sep 30 11:11 redis-benchmark
-rwxr-xr-x 1 root root   22249 Sep 30 11:11 redis-check-aof
-rwxr-xr-x 1 root root   45459 Sep 30 11:11 redis-check-dump
-rwxr-xr-x 1 root root 4691881 Sep 30 11:11 redis-cli
-rw-r--r-- 1 root root   41404 Sep 30 11:15 redis.conf
lrwxrwxrwx 1 root root      12 Sep 30 11:11 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 6450265 Sep 30 11:11 redis-server
[root@VM_0_10_centos bin]# ./redis-cli> 







1. String

String is also Redis's most important, simplest, and most commonly used data type.

Redis strings are byte sequences. Redis strings are binary secure, which means they have a known
There are no special characters to terminate, so you can store anything, with 512 megabytes as the upper limit.


  • set sets a value key value form
  • get gets the value of a key
  • incr increments the current key value by 1 and returns the incremental value
  • incrby can specify the value of an incremental parameter and return the incremental value
  • decr decreases the current key value by 1 and returns the decreased value
  • decrby specifies the value of a parameter that decreases once and returns the decreased value
  • incrbyfloat can increment a double-precision floating-point number
  • The append function is to append value to the end of the key value. If the key does not exist, set the value of the key to value. return
  • The return value is the total length of the appended string.
  • mget/mset is similar to get/set, but mget/mset can get/set multiple key values at the same time
  • del deletes value according to key
  • flushdb clears all data from the current library


redis> SET name kevin
redis> GET name
"kevin"> set num 10
OK> incr num
(integer) 11> get num
"11"> incrby num 10
(integer) 21> get num
"21"> incrbyfloat num 20
"41"> get num
"41"> incrbyfloat num 10.1
"51.1"> get num
"51.1"> set name arebirth
OK> append name ||arebirth
(integer) 18> get name
"arebirth||arebirth"> mset key val k2 v2
OK> mget key k2
1) "val"
2) "v2"> del num
(integer) 1> get num
(nil)> flushdb
OK> get k2

2 Hash (Hash table)

Redis's hash is a collection of key-value pairs. The hash value of Redis is a mapping between string fields and string values.
So they are used to represent objects. They look abstract. Let's do it in practice.


  • hset stores a set of hash key-value pairs
  • hset key field value
  • hget gets the value of a hash key
  • hget key field
  • hmset stores one or more hashes as a set of key-value pairs
  • hmset key field1 value1 ......fieldN keyN
  • hmget gets the values of multiple specified keys
  • hmget key field1 ... fieldN
  • hexists determines whether the field name in the hash table exists or not. If there is a return 1, it will return 0.
  • hexists key field
  • hdel Deletes one or more fields
  • hdel key field
  • hgetall gets a set of hashes that are key-value pairs
  • hgetall key
  • hvals returns only field values
  • hvals key
  • hkeys returns only field names
  • hkeys key
  • The number of elements of hash that hlen returns to key
  • hlen key

Example:> hset user username arebirth
(integer) 1> hget user username
"arebirth"> hmset user pwd 123456 age 17
OK> hmget user pwd age
1) "123456"
2) "17"> hexists user aa
(integer) 0> hexists user username
(integer) 1> hdel user age
(integer) 1> hget user age
(nil)> hgetall user
1) "username"
2) "arebirth"
3) "pwd"
4) "123456"> hvals user
1) "arebirth"
2) "123456"> hkeys user
1) "username"
2) "pwd"> hlen user
(integer) 2

3 List linked list

Redis's linked list is a simple list of strings, sorted in insertion order. You can add elements to the list of Redis
Head or tail

Much like ListLink in our Java, bi-directional lists


  • lpush key value is added to the left of the list
  • rpush key value is added to the right side of the list
  • lpop key moves an element from the left
  • rpop key removes an element from the right
  • llen key returns the number of elements in the linked list equivalent to select count(*) in the relational database.
  • The lrange key start end lrange command returns all elements of the index from start to stop. Column of Redis
  • The table start index is 0.
  • Lrange also supports negative index lrange nn-2-1 such AS-1 for the rightmost first element-2 for the rightmost second
  • Five elements, in turn, by analogy.
  • The lindex key index number command is essential if the list type is to be used as an array.
  • The lindex command is used to return elements of the specified index, which starts at 0.
  • If the negative number represents the index calculated from the right, the index of the rightmost element is - 1.
  • Lset key index number value is another command that manipulates lists by indexing, which will index the index
  • The element assignment value of

Example:> clear> lpush chain 1
(integer) 1> lpush chain 2
(integer) 2> lpush chain 3
(integer) 3> rpush chain 4
(integer) 4> rpush chain 5
(integer) 5> rpush chain 6
(integer) 6> lrange chain 0 -1
1) "3"
2) "2"
3) "1"
4) "4"
5) "5"
6) "6"> lpop chain
"3"> rpop chain
"6"> lrange chain 0 -1
1) "2"
2) "1"
3) "4"
4) "5"> llen chain
(integer) 4> lindex chain 2
"4"> lset chain 2 5
OK> lindex chain 2



4 Set set


The set of Redis is an unordered set of strings.


  • sadd key value adds a string element to the set set set corresponding to the key and successfully returns 1 if the element has already
  • Returns 0 in the collection
  • scard key returns the number of elements of set, if set is empty or key does not exist, returns 0
  • smembers key returns all the elements of the key corresponding to the set, and the result is disordered
  • sismember key value determines whether the value is in the set, and the presence returns 1,0 to indicate that there is no or key does not exist.
  • stay
  • srem key value removes the given element from the key corresponding set and successfully returns 1 if value does not exist in the collection
  • Return 0 in or key does not exist

Example:> sadd opr a
(integer) 1> sadd opr a
(integer) 0> sadd opr b
(integer) 1> scard opr
(integer) 2> smembers opr
1) "b"
2) "a"> sismember opr a
(integer) 1> sismember opr b
(integer) 1> srem opr a
(integer) 1> smembers opr
1) "b"


5 SortedSet (Ordered Set) zset

The ordered set of Redis is similar to the set of Redis and the set of non-repetitive strings.

We can set its ranking order by specifying the size value.


  • zadd key score value adds one or more values and their socre s to the set
  • zrange key start end 0 and - 1 represent elements from index 0 to the last element (same as LRANGE command)
  • Similarity)
  • Zrange key 0-1 withscores can also be output with score, using the WITHSCORES parameter
  • Zremrange by score key start end can be used for range deletion operations

Example:> zadd score 1 a
(integer) 1> zadd score 0.1 b
(integer) 1> zadd score 2 c
(integer) 1> zrange core 0 -1
(empty list or set)> zrange score 0 -1
1) "b"
2) "a"
3) "c"> zrange score 0 -1 withscores
1) "b"
2) "0.10000000000000001"
3) "a"
4) "1"
5) "c"
6) "2"> zremrangebyscore score 0 10
(integer) 3> zrange score 0 -1 withscores
(empty list or set)


6 Redis Other Common Commands

  • ping tests whether redis are linked if a link is returned to PONG
  • echo value tests whether redis are linked if the link returns the value given after echo command
  • keys * Returns all key s to add * wildcards
  • exists key determines whether a key of string type exists or not, returns 0 if there is a return 1 or otherwise
  • expire key time(s) sets the expire time unit second of a key. When the time arrives, the key and value will be deleted.
  • The remaining time of the key whose expiration time has been set in the ttl key query If - 2 is returned, the key-value pair has been deleted.
  • persist removes the expiration time of a given key
  • select dbindex select database (0-15)
  • move key dbIndex transfers keys from the current database to other databases
  • dbsize returns the number of key s in the current database
  • info Gets Server Information and Statistics
  • flushdb deletes key s from the currently selected database
  • flushall deletes all key s in all databases
  • quit exits the connection


Example:> ping
PONG> echo arebirth
"arebirth"> keys *
(empty list or set)> set a a
OK> keys *
1) "a"> exists a
(integer) 1> exists aa
(integer) 0> expire a 20
(integer) 1> ttl a
(integer) 18> tt a
(error) ERR unknown command 'tt'> ttl a
(integer) 14> ttl a
(integer) 11> persist a
(integer) 1> ttl a
(integer) -1> expire a 5
(integer) 1> ttl a
(integer) 4> ttl a
(integer) -2> exists a
(integer) 0> select 1
OK[1]> set aa aa
OK[1]> move aa 0
(integer) 1[1]> dbsize
(integer) 0[1]> select 0
OK> dbsize
(integer) 1> keys *
1) "aa"> info
# Server
os:Linux 2.6.32-642.6.2.el6.x86_64 x86_64

# Clients

# Memory

# Persistence

# Stats

# Replication


# Cluster

# Keyspace
db0:keys=1,expires=0,avg_ttl=0> flushdb
OK> flushall
OK> quit




There are a lot of commands in Redis, but these are often used.

Posted by turtlefox on Wed, 09 Oct 2019 08:08:05 -0700