Redis general directive

Key Universal operation

  • Key is a string used to retrieve data stored in Redis

Which operations should be designed by key

  • Operations related to the state of the key, for example, delete, determine the existence, and obtain the type

  • Key validity control operations, such as setting the validity period, determining whether the key is valid, and switching valid states

  • Quick query operations for keys, for example, querying keys based on specified policies

  • Deleting a specified key

    del key

  • Gets whether the key exists

    exists key

  • Gets the type of the key

    type key

127.0.0.1:6379 >setHset hash1 hash1 hash1integer) 1
127.0.0.1:6379> lpush list1 list1
(integer) 1
127.0.0.1:6379> sadd set1 set1
(integer1 127.0.0.1:6379> zadd zset1 1 zset1 (integer6379 > 1 127.0.0.1) :typeZset1 zset 127.0.0.1:6379 >typeThe STR string 127.0.0.1:6379 >type hash1
hash127.0.0.1:6379 > exists STR (integer1 127.0.0.1:6379> exists STRQWFQW (integer) 0
127.0.0.1:6379> del zset1
(integer) 1
127.0.0.1:6379> del zset1
(integer0 127.0.0.1:6379> exists zset1 (integer) 0

Copy the code

Key extension operation (timeliness control)

  • Sets the validity period for the specified key

    expire key seconds

Pexpire key milliseconds expireat key timestamp # Pexpireat key milliseconds-timestamp

  • Gets the validity time of the key

    ttl key

pttl key

  • The toggle key is converted from time-sensitive to permanent

    persist key

127.0.0.1:6379> expire list1 30
(integer) 1
127.0.0.1:6379> ttl list1
(integer) 25
127.0.0.1:6379> ttl list1
(integer) 12
127.0.0.1:6379> ttl list1	# invalid key returns -2
(integer) -2
127.0.0.1:6379> ttl list2	# unaged key returns -1
(integer) -1

127.0.0.1:6379> persist list2	Permanent return 0 for keys that are not set to age
(integer) 0
127.0.0.1:6379> expire list2 60
(integer) 1
127.0.0.1:6379> ttl list2
(integer) 57
127.0.0.1:6379> persist list2	# Setting the time-sensitive key returns 1 success
(integer) 1
127.0.0.1:6379> ttl list2
(integer1) -Copy the code

Key Extension operation (query mode)

  • Query key

    keys pattern

Query mode rules

* Matches any number of any symbols? Matches a specified symbol with an arbitrary symbol []

Keys * query all keys starting with it* query all keys ending with heima?? Keys user:? Query all keys u[st]er:1 that start with u and end with er:1 and contain a single letter, s or TCopy the code
127.0.0.1:6379 > keys us * 1)"user:id:110:fans"
 2) "user:id:120"
 3) "user1"
 4) "user2"
 5) "user5"
 6) "user:1:age"
 7) "user"
 8) "user:1:name"
 9) "user3"
10) "user:1:gender"
11) "user4"
12) "user:id:110:views"
13) "user:id:110:likes"127.0.0.1:6379 > keys s * 1)"shop:001"
2) "s1"
3) "set1"
4) "s2"
5) "scores"
6) "sss"
7) "socres"
8) "ss"
9) "s3"127.0.0.1:6379 > keys to use? 1)"user"127.0.0.1:6379 > keys s? r 1)"str"127.0.0.1:6379 > keys?????? 1)"list"
2) "set1"
3) "user"
4) "logs"
5) "news"127.0.0.1:6379 >setThe name niu OK 127.0.0.1:6379 >setNbme zhu OK 127.0.0.1:6379> keys n[]me (empty array) 127.0.0.1:6379> keys n[ABC]me 1)"nbme"
2) "name"
Copy the code

Key Other operations

  • As the key name

    rename key newkey

renamenx key newkey

  • Sort all keys

    sort

  • Other key general operations

    help @generic

127.0.0.1:6379> rename user user100
OK
127.0.0.1:6379> get user100
"zhu"127.0.0.1:6379 > keys user * 1)"user:id:110:fans"
 2) "user:id:120"
 3) "user1"
 4) "user2"
 5) "user5"127.0.0.1:6379 > renamenx user1, user2# failed to change the name if it exists
(integer) 0
127.0.0.1:6379> lpush aa 123
(integer) 1
127.0.0.1:6379> lpush aa 321
(integer) 2
127.0.0.1:6379> lpush aa 222
(integer) 3 127.0.0.1:6379> lrange AA 0-1 1)"222"
2) "321"
3) "123"127.0.0.1:6379 > sort of aa# sort only the original list
1) "123"	
2) "222"
3) "321"127.0.0.1:6379> lrange AA 0-1 1)"222"
2) "321"
3) "123"
Copy the code

General database instruction

Duplicate problem of key

  • Keys are defined by the programmer
  • In the process of using Redis, with the increase of operation data, a large number of data and corresponding keys will appear
  • Data does not distinguish between categories, categories mixed together, prone to duplication or conflict

The solution

  • Redis provides 16 databases for each service, numbered from 0 to 15
  • The data between each database is independent of each other

Db Basic Operations

  • Switching databases

    select index

  • Other operating

    quit

ping echo message

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> select 15
OK
127.0.0.1:6379[15]> echo abcd
"abcd"127.0.0.1:6379 [15] > ping pongTest whether the connection is successful
"pong"127.0.0.1:6379 [15] > quitCopy the code

Db related operations

  • Data movement

    move key db

  • Data to remove

    dbsize

flushdb flushall

127.0.0.1:6379[1]> get name
(nil)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> move name 1
(integer) 1
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get name
"niu"[2] failed to move 127.0.0.1:6379 if another library has the same key [2]>setName Zhu OK 127.0.0.1:6379[2]> get name"zhu"
127.0.0.1:6379[2]> flushdb 
OK
127.0.0.1:6379[2]> get name
(nil)
127.0.0.1:6379[2]> select 1
OK
127.0.0.1:6379[1]> get name
"niu"
Copy the code

Redis server configuration

Server setup

  • Set up the server to run as a daemon

    daemonize yes|no

  • Binding host Address

    The bind 127.0.0.1

  • Set the server port number

    port 6379

  • Setting the number of databases

    databases 16

Enable two Redis services [root@maomao redis_config]# mv redis.conf redis-6379.conf 
[root@maomao redis_config]# ls
redis-6379.conf
[root@maomao redis_config]# cp redis-6379.conf redis-6380.conf 
[root@maomao redis_config]# vim redis-6380.conf Change the port number and log name and dir location port 6380 logfile"6380.log"
dir /usr/local/redis/data


[root@maomao bin]# redis-server redis_config/redis-6379.conf 
[root@maomao bin]# redis-server redis_config/redis-6380.conf 
[root@maomao bin]# ps -aux |grep redisRoot 1814 0.1 0.4 162456 10060? Ssl 09:38 0:00 redis-server 127.0.0.1:6379 root 1820 0.6 0.4 162456 10060? Ssl 09:38 0:00 redis-server 127.0.0.1:6380 [root@maomao bin]# redis-cli -p 6379127.0.0.1:6379 > [root @ maomao bin]# redis-cli -p 6380127.0.0.1:6380> Viewing logs [root@maomao data]# pwd
/usr/local/redis/data
[root@maomao data]# ls
6379.log  6380.log
Copy the code

The log configuration

  • Set up the server to specify the logging level

    loglevel debug|verbose|notice|warning

  • Log file name

    Logfile Port id.log

  • Set the current service file saving location, including log files, persistent files, etc

    Dir “/ custom path /redis/data”

== Notice == : Log level Set the log level to verbose during development and notice in the production environment to simplify log output and reduce log I/O writing frequency

Client Configuration

  • Set the maximum number of client connections at a time. The default value is unlimited. When the number of client connections reaches the upper limit, Redis closes new connections

    maxclients 0

  • The connection is closed when the idle waiting period reaches the maximum value. To disable this function, set this parameter to 0

    timeout 300

Quick configuration of multiple servers

  • Import and load the specified configuration file information, which can be used to quickly create redis instance configuration files with many common redis configurations for easy maintenance

    Include /path/ server-port id.conf

supplement

pidfile /var/run/redis_6379.pid		To run in the background, we need to specify a PID file

logfile ""	The file location name of the log

always-show-logo yes	Is the logo always displayed
Copy the code

The snapshot SNAPSHOTTING

save 3600 1		If at least one key is modified in the 3600s 00s, we persist
save 300 100	If at least 100 keys have been changed in 300 seconds, we will persist
save 60 10000	If at least 10,000 keys have been changed in 60 seconds, we will persist

stop-writes-on-bgsave-error yes If something goes wrong, do you need to continue working

rdbcompression yes	Whether or not to compress RDB files (persistent files) consumes some CPU resources

rdbchecksum yes	Error check when saving RDB file

dir ./	# RDB file save directory


Copy the code

Safety and SECURITY

127.0.0.1:6379> ping
PONG
127.0.0.1:6379> config set requirepass "123456"		# set redis passwordOK 127.0.0.1:6379 > auth 123456Use password to log in
OK
127.0.0.1:6379> config get requirepass	Get password
1) "requirepass"
2) "123456"
Copy the code

Limit the CLIENTS

maxclients 10000	# set the maximum number of clients that can connect to Redis

maxmemory <bytes>	# redis configures maximum memory capacity

maxmemory-policy noeviction	The processing strategy after the memory reaches the upper limitVolatile - lRU: lRU only for keys whose expiration time is set (default value) 2. Allkeys-lru: delete keys of lRU algorithm 3. Volatile -random: delete keys that are about to expire randomly 4. Volatile - TTL: delete items that are about to expire. Noeviction: never expire, return errorCopy the code

Aof Configures the APPEND ONLY mode

appendonly no	Aof is not enabled by default, RDB is used for persistence, in most cases, RDB is sufficient

appendfilename "appendonly.aof"		The name of the persistent file

# appendfsync always # Sync every change. Consumption performance
appendfsync everysec	If you perform sync once per second, you may lose 1s of data!
At this point, the operating system synchronizes data by itself, the fastestThe configuration is detailed in Redis Persistence notesCopy the code