Redis installation and related basic commands

Today we will take a look at the installation and use of redis. Everything is summarized for easy use in the future.
The data stored in redis is in the form of KV, which can be persisted.
First, we need to install it first. What I choose here is to download the source code package online, and then compile it.

cd /opt 
wget http://download.redis.io/releases/redis-4.0.8.tar.gz

After downloading, we need to unzip

tar -zxvf redis-4.0.8.tar.gz

After decompression, enter the folder and execute the make command to compile

cd redis-4.0.8
make

After compiling, we need to install and point to the installation directory, without spaces around the equal sign

cd bin
make install PREFIX=/usr/local/redis-4.0.8

After that, we copy redis.conf to our installation directory

cp redis.conf /usr/local/redis-4.0.8/redis.conf

The relevant configuration items in the configuration file are explained in my other blogs. You can refer to what you need to change.
For better operation, I only change one of the configurations, which is to change daemonize no to yes, and let it be in the background run

cd /usr/local/redis-4.0.8
vim redis.conf

After the modification, we can start

cd bin
./redis-server ../redis.conf

We can check whether there is redis in the process

ps -aux|grep redis

Redis runs in a single thread and can process about 100,000 requests per second. This needs to be combined with the host's hardware. We can run the command to check it. My machine is about 70,000.

./redis-benchmark 

Well, now, we can connect to the redis client to try various commands.
Connect to the client. You can also say that the
default connection for something is 6379, so if our port has not been changed, we can directly execute the command to connect redis client

./redis-cli

If we modify the port to 6378, then the port needs to be added when connecting, including if there are multiple redis instances locally, then the port needs to be added when using the client to connect to redis

./redis-cli -p 6378

There is also a remote connection. At this time, you need to add ip. If the port is not the default 6379, you also need to add the port. If a password is set in the configuration file, then you need to add a password.

./redis-cli -h ip -p 端口 -a 密码

We can also connect first, and then enter the password to verify

auth password

OK, the client is connected, we can send a password ping, and will reply with a PONG to
Insert picture description here
get a value in the configuration file

config get key

Set a value in the configuration file

config set key value

The configuration file defaults to 16 databases, we can switch the database by command

select 切换的库下标

View the current number of library keys

DBSIZE

I want to see what specific keys are

keys *

If we want to match a key, we can use? As a placeholder, which only occupies one digit. For example, the matching key is k2

keys k?

Determine whether a key exists, return 1 if it exists, return 0 if it does not exist

EXISTS 查询的key

Remove a key-value pair from the current library and move it to another library

move 移除的key 移动到的库索引

Check how many seconds have expired, -1 means never expire, -2 means expired

ttl key

Set the expiration time for the given key, it will not exist after expiration

expire key 秒钟

Check what type of your key is

type key

Add value. There are five types of values: String, List, Set, Hash, and Zset sorted set.

String
String is the most basic type of Redis. You can understand it as exactly the same type as Memcached. A key corresponds to a value.
The String type is binary safe, meaning that the Redis String can contain any data, such as jpg images or serialized objects.
The String type is the most basic data type of Redis. The string value in a Redis can be up to 512M.
Settings

set key value

Delete key

del key

Append value

append key value

View length

STRLEN key

When the value is a number, you can directly add one

INCR key

When the value is a number, you can directly subtract one

DECR key

When the value is a number, it can be added directly

INCRBY k2 value

When the value is a number, it can be subtracted directly

DECRBY k2 value

Get the value between the start index and the end index, when the end index is -1, it is the end

GETRANGE key 开始下标 结束下标

Set value override

SETRANGE key 开始下标 替换的值

Set the value and set the expiration time

setex key 过期时间 value

If there is no setting value, it will not be overwritten if it exists

setnx key value

Set multiple values

mset key value key value key value ......

Get multiple values

mget key key key ......

Make multiple non-existent settings, but can't already exist and non-existent mixed settings, either all exist or none exist

msetnx key value key value key value ......

Set a new value, and return the old value, if the old value does not exist, return null

getset key value

List (list)
Redis list is a simple list of strings, sorted in the order of insertion. You can add an element to the head (left) or tail (right) of the list. Its bottom layer is actually a linked list.
If the key does not exist, create a new linked list.
If the key already exists, add new content.
If the values ​​are all removed, the corresponding key will also inform you that
the operation of the linked list is extremely efficient in both the head and tail, but if it is performed on the middle element Operation, efficiency is very bleak.
Set the value from left to right

LPUSH key value value ......

Set value from right to left

RPUSH key value value ......

View the value, you can choose according to the subscript, when the end subscript is -1, it is the end

LRANGE key 开始下标 结束下标

Pop the value from the left

LPOP key

Pop value from right

RPOP key

Get the value of a subscript

lindex key index

Get the length

llen key

Delete N values

lrem key n value

Intercept a certain value and assign the intercepted value

ltrim key 开始下标 结束下标

The value popped from the right of the source list is pushed into the target list from the left

rpoplpush 源列表 目标列表

Insert a value in front of a value

linsert key before 基值 插入值

Insert a value after a value

linsert key after 基值 插入值

Set (collection)
Redis Set is an unordered collection of String type, which is implemented through HashTable.
Set a set

sadd key k1 v1 k2 v2 k3 v3

View the value of set

smembers key

Check if there is a key in the set, return 1 if it exists, return 0 if it does not exist

sismember key

Get the number of elements in the set

scard key

Delete an element of the set

srem key value

Randomly select n numbers from the set

srandmember key n

Randomly pop out of the stack, default one, if you write n, then randomly pop out n

spop key n(不写,默认出n个)

Move the value of set1 to set2

smove set1 set2 value

Difference set, elements in set1 but not in set2

sdiff set1 set2

Intersection, elements that exist in both set1 and set2

sinter set1 set2

Union, the set after all elements of set1 and set2 are deduplicated

sunion set1 set2

Hash (hash)
Redis hash is a collection of key-value pairs.
Redis hash is a mapping table of String type field and value. Hash is especially suitable for storing objects.
Similar to the
value of Map<String,Object> in Java , key is the key of redis, and kv is the element of map.

hset key k v

Get the value, first get the element, then get the value in the element

hget key k

Set multiple values

hmset key k v k v k v ......

Get multiple values

hmget key k k k ......

Get all the values

hgetall key

Delete some elements

hdel key k k k ......

Get the number of elements

hlen key

Determine whether an element exists, return 1 if it exists, return 0 if it does not exist

hexists key k

Get the key of all elements

hkeys key

Get the value of all elements

hvals key

If the value of the element is an integer, you can increment an integer

hincrby key k n

If the value of the element is a decimal, you can increase a certain decimal

hincrbyfloat key k n

If an element does not exist, set the value, if there is no set value, return 1 if the set value is successful, and return 0 if the set value fails

hsetnx key k v

Zset (sorted set: ordered collection)
Redis zset is also a collection of String type elements like set, and duplicate members are not allowed.
The difference is that each element is associated with a double type score.
Redis uses scores to sort the members of the set from small to large. The members of zset are unique, but the score (score) can be repeated.
Set value, value is a key-value pair, including score and value

zadd key score value score value score value ......

View value

zrange key 开始下标 结束下标

View value with score

zrange key 开始下标 结束下标 withscores

View the value of a certain score interval

zrangebyscore key 开始分数 结束分数

View the value of a score interval with scores

zrangebyscore key 开始分数 结束分数 withscores

View the value that does not include a certain score range, but does not include the start score or the end score

zrangebyscore key (开始分数 (结束分数

View a certain score interval, and intercept n values ​​from the starting subscript

zrangebyscore key 开始分数 结束分数 limit 开始下标 n

Delete element

zrem key 值

View length

zcard key

Count the number of scores in a certain interval

zcount key 开始分数 结束分数

View the index of the value

zrank key value

View the score of the value

zscore key value

Get the subscript value in reverse order

zrevrank key value

Sort zset in reverse order

zrevrange key 开始下标 结束下标

Get the value of a certain score interval in reverse order

zrevrangebyscore key 开始分数 结束分数

Clear library data (use with caution)

FLUSHDB

Clear all library data (use with caution)

FLUSHALL

Finally, I will provide you with a website of Redis operation manual, you can refer to the Redis command reference

Guess you like

Origin blog.csdn.net/weixin_45345374/article/details/112527528