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
127.0.0.1:6379> 

 

 

 

 

 

 

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

Example:

redis 127.0.0.1:6379> SET name kevin
OK
redis 127.0.0.1:6379> GET name
"kevin"

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

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:

 

127.0.0.1:6379> hset user username arebirth
(integer) 1
127.0.0.1:6379> hget user username
"arebirth"
127.0.0.1:6379> hmset user pwd 123456 age 17
OK
127.0.0.1:6379> hmget user pwd age
1) "123456"
2) "17"
127.0.0.1:6379> hexists user aa
(integer) 0
127.0.0.1:6379> hexists user username
(integer) 1
127.0.0.1:6379> hdel user age
(integer) 1
127.0.0.1:6379> hget user age
(nil)
127.0.0.1:6379> hgetall user
1) "username"
2) "arebirth"
3) "pwd"
4) "123456"
127.0.0.1:6379> hvals user
1) "arebirth"
2) "123456"
127.0.0.1:6379> hkeys user
1) "username"
2) "pwd"
127.0.0.1:6379> 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:

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

 

 

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:

127.0.0.1:6379> sadd opr a
(integer) 1
127.0.0.1:6379> sadd opr a
(integer) 0
127.0.0.1:6379> sadd opr b
(integer) 1
127.0.0.1:6379> scard opr
(integer) 2
127.0.0.1:6379> smembers opr
1) "b"
2) "a"
127.0.0.1:6379> sismember opr a
(integer) 1
127.0.0.1:6379> sismember opr b
(integer) 1
127.0.0.1:6379> srem opr a
(integer) 1
127.0.0.1:6379> 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:

127.0.0.1:6379> zadd score 1 a
(integer) 1
127.0.0.1:6379> zadd score 0.1 b
(integer) 1
127.0.0.1:6379> zadd score 2 c
(integer) 1
127.0.0.1:6379> zrange core 0 -1
(empty list or set)
127.0.0.1:6379> zrange score 0 -1
1) "b"
2) "a"
3) "c"
127.0.0.1:6379> zrange score 0 -1 withscores
1) "b"
2) "0.10000000000000001"
3) "a"
4) "1"
5) "c"
6) "2"
127.0.0.1:6379> zremrangebyscore score 0 10
(integer) 3
127.0.0.1:6379> 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:

127.0.0.1:6379> ping
PONG
127.0.0.1:6379> echo arebirth
"arebirth"
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set a a
OK
127.0.0.1:6379> keys *
1) "a"
127.0.0.1:6379> exists a
(integer) 1
127.0.0.1:6379> exists aa
(integer) 0
127.0.0.1:6379> expire a 20
(integer) 1
127.0.0.1:6379> ttl a
(integer) 18
127.0.0.1:6379> tt a
(error) ERR unknown command 'tt'
127.0.0.1:6379> ttl a
(integer) 14
127.0.0.1:6379> ttl a
(integer) 11
127.0.0.1:6379> persist a
(integer) 1
127.0.0.1:6379> ttl a
(integer) -1
127.0.0.1:6379> expire a 5
(integer) 1
127.0.0.1:6379> ttl a
(integer) 4
127.0.0.1:6379> ttl a
(integer) -2
127.0.0.1:6379> exists a
(integer) 0
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> set aa aa
OK
127.0.0.1:6379[1]> move aa 0
(integer) 1
127.0.0.1:6379[1]> dbsize
(integer) 0
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> dbsize
(integer) 1
127.0.0.1:6379> keys *
1) "aa"
127.0.0.1:6379> info
# Server
redis_version:3.0.0
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:3477127313654848
redis_mode:standalone
os:Linux 2.6.32-642.6.2.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:26037
run_id:65435cbd205e87a262b28d0823d004786c69470e
tcp_port:6379
uptime_in_seconds:685095
uptime_in_days:7
hz:10
lru_clock:10217642
config_file:/usr/local/redis/bin/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:819424
used_memory_human:800.22K
used_memory_rss:2789376
used_memory_peak:839392
used_memory_peak_human:819.72K
used_memory_lua:35840
mem_fragmentation_ratio:3.40
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:6
rdb_bgsave_in_progress:0
rdb_last_save_time:1570498470
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:129
total_commands_processed:1523
instantaneous_ops_per_sec:0
total_net_input_bytes:16512536
total_net_output_bytes:94988
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:1
evicted_keys:0
keyspace_hits:144
keyspace_misses:18
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:232
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:424.19
used_cpu_user:202.10
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

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

 

 

ps:

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