1. Configure Alibaba cloud Yum source
Download profile
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
Generate cache
yum makecache
2. Install redis
2.1 installation dependency
yum install -y gcc-c++ gcc tcl
2.2 upload and decompress the compressed package
tar -xvf redis-3.0.0.tar.gz
2.3 compilation
cd redis-3.0.0
make
# make test
Create installation directory
mkdir -p /usr/local/soft/redis
install
make PREFIX=/usr/local/soft/redis install
2.4 copying configuration files
cp /usr/local/soft/redis-3.0.0/redis.conf /usr/local/soft/redis/bin/
Configure environment variables
vim /etc/profile
increase
export REDIS_HOME=/usr/local/soft/redis/
Add $redis after path_ HOME/bin
Make environment variables effective
source /etc/profile
5. Start redis
Background start
cd /usr/local/soft/redis/bin
nohup redis-server redis.conf &
Turn off redis
ps -aux | grep redis
kill -9
6. Connect to redis
redis-cli
Insert a piece of data
set key1 value1
Query data
get key1
3. Use of redis
Set string
SET key value [EX seconds] [PX milliseconds] [NX|XX]
Ex sets the expiration time in seconds, which is equivalent to set key seconds value
PX sets the expiration time in milliseconds, which is equivalent to psetex key milliseconds value
Nx key can only be set if it does not exist, which is equivalent to setnx key value
Only when XX key exists can it be set
Set string values for multiple keys
MSET key value [key value …]
Sets the string value when the key does not exist
MSETNX key value [key value …]
Note: This is the atomic operation setting. If there is an error, it will be all errors
be overdue
In redis, you can set a lifetime (seconds or milliseconds) for keys. When this duration is reached, these keys will be automatically deleted
Set the number of seconds or milliseconds after expiration
EXPIRE key seconds PEXPIRE key milliseconds
Sets the expiration date of the specified UNIX timestamp
EXPIREAT key timestamp PEXPIREAT key milliseconds-timestamp
Delete expired
PERSIST key
View remaining lifetime
TTL key (return hours and seconds)
Pttl key (return milliseconds)
If the key exists but the TTL is not set, – 1 is returned,
But it is still in the lifetime. It returns the remaining seconds or milliseconds. The key once existed,
But it has died, return – 2 (return – 1 before version 2.8)
Find key
KEYS pattern
Pattern value
*
Any length character?
Any character [] character set indicates that it can be any one in the set
Key type
TYPE key
Does the key exist
EXISTS key
Key rename
RENAME key newkey
RENAMENX key newkey
Key delete
DEL key [key …]
Flush all (delete all key value pairs)
Get value
GET key
Gets the value of more than one given key
MGET key [key …]
Returns the old value and sets the new value
GetSet key valueif the key does not exist, it is created and assigned
String length
STRLEN key
Append string
Append key valueappend if the key exists; If it does not exist, it is equivalent to set key value
Get substring
Getange key start end the index value starts from 0, a negative number indicates the number from the right to the left of the string, and – 1 indicates the last character
Overlay string
SETRANGE key offset value
Increase or decrease of step 1
INCR key
DECR key
The string value is interpreted as a 64 bit signed decimal integer, and the result is still converted to a string
Step increase or decrease
INCRBY key decrement
DECR key decrement
The string value is interpreted as a 64 bit signed decimal integer, and the result is still converted to a string
4. Use of redis bitmap
Bitmap is not a real data type, it is defined in a string type
A string type value can store up to 512M bytes
Upper bit limit: 2 ^ (9 + 10 + 10 + 3) = 2 ^ 32B
a –> 01100001
b –> 01100010
and
BITOP and dest1 a b
a and b —> 0110 0000
or
BITOP or dest2 a b
a or b –> 01100011
xor
BITOP xor dest2 a b
a or b –> 00000011
not
BITOP not dest4 a
a —> 10011110
Redis’s data is stored in memory, and the bit operation efficiency is very high
Each value of DeST * can be obtained through the getbit command
Set the value on a bit
SETBIT key offset value
Offset offset, starting from 0. Value is not written. The default is 0
Gets the value on a bit
GETBIT key offset
Returns the first occurrence position of the specified value 0 or 1 in the specified interval
BITPOS key bit [start] [end]
be careful:
Except for the not operation, other operations can accept one or more keys as input
When bitop processes strings of different lengths, the missing part of the shorter string is treated as 0
An empty key is also regarded as a string sequence containing 0
Statistics refers to the number of values of 1 on the positioning interval
BITCOUNT key [start] [end]
Start from 0 from left to right and – 1 from right to left. Note that the official start and end are bits and bytes after the test
BITCOUNT testkey 0
0 indicates the statistics of the first byte from the index of 0 bytes to the index of 0 bytes
BITCOUNT testkey 0
-1 is equivalent to bit testkey. The most commonly used is bit testkey