[Redis] Installation configuration and data type

Redis installation configuration and data type



Redis, an open source use ANSI C language, network support, memory can also be based on persistent log type, Key-Value database, and provides multi-lingual API;

Redis is like a HashMap, but it does not run in the JVM, but runs as a separate process.

​ The access speed of data is faster than that of the database (Mysql), and it is generally used as a cache, so commonly used data can be considered here to improve performance .

Redis installation

Window installation

window installation address

The downloaded Redis supports 32bit and 64bit. Choose according to your actual situation, add the 64bit content cp to the custom drive letter installation directory and name it redis. Such as G:\redis.
1. Open the redis-server.exe in the running root directory , keep the running window open, don't close it.

If you want to be convenient, you can add the path of redis to the environment variables of the system, so you don’t have to lose the path.

Insert picture description here
2. Open redis-cli.exe in the directory , or execute the command: redis-cli.exe -h 127.0.0.1 -p 6379
Insert picture description here

Redis configuration

The Redis configuration file is located in the Redis installation directory and the file name is redis.conf

View and set configuration items through the CONFIG command

1. View the configuration

Command format:

redis 127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME

Example:

redis 127.0.0.1:6379> CONFIG GET loglevel

1) "loglevel"
2) "notice"

Use the symbol * to get all configuration items

2. Modify the configuration

Command format:

redis 127.0.0.1:6379> CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE

Example:

redis 127.0.0.1:6379> CONFIG SET loglevel "notice"
OK
redis 127.0.0.1:6379> CONFIG GET loglevel

1) "loglevel"
2) "notice"

3. Description of configuration item parameters

  1. daemonize: Redis does not run as a daemon by default. You can change this configuration item to yes to enable the daemon.

    When redis runs as a daemon, redis will write the pid to the /var/run/redis.pid file by default, which can be specified by pidfile:

    pidfile /var/run/redis.pid
    
  2. port: Specify the reids listening port, the default port is 6379.

  3. bind: bind host address, default 127.0.0.1

  4. timeout: how long the client is idle to close the connection, if it is 0, it means to close the function

  5. loglevel: Logging level.

    Redis supports a total of four levels: debug, verbose, notice, warning, the default is verbose

  6. databases: Set the number of databases, the default is 0, you can use the SELECT <dbid> command to specify the database id on the connection

  7. save <seconds> <changes>: Indicates how long (in s) there are several update operations to synchronize the data to the data file.

    Three conditions are provided in the default configuration file in redis:

    • save 900 1
    • save 300 10
    • save 60 10000
  8. rdbcompression: Specify whether to compress data when storing it in the local database. The default is yes.

    Redis uses LZF compression. If you want to save CPU time, you can turn off this option, but it will cause the database file size to become very large.

  9. dbfilename: Specify the local database file name, the default value is dump.rdb

  10. dir: Specify the local database storage directory

  11. slaveof <masterip> <masterport>:

    Set when the machine is the slav service, set the ip address and port of the master service, when redis starts, it will automatically synchronize data from the master

  12. masterauth <master-password>: When the master service is set to password protection, the password for the slav service to connect to the master.

  13. requirepass foobared:

    Set the redis connection password. If the connection password is set, the client needs to provide the password through the AUTH <password> command when connecting to redis. The default is no

  14. maxclients 128:

    Set the maximum number of client connections at the same time. By default, there is no limit.

    The number of client connections that Redis can open at the same time is the maximum number of file descriptors that can be opened by the Redis process. If maxclients 0 is set, it means that there is no limit .

    When the number of client connections reaches the limit, Redis will close the new connection and return a max number of clients reached error message to the client

  15. maxmemory <bytes>: Specify the maximum memory limit of Redis

    Redis will load the data into the memory when it starts. After reaching the maximum memory, Redis will first try to clear the expired or about to expire keys. After this method is processed, the maximum memory setting is still reached, and no more writes can be made. Operation, but the read operation can still be performed.

    Redis’s new vm mechanism will store Key in memory, and Value will be stored in the swap area

  16. appendonly no: Specify whether to log after each update operation, the default is no

    Redis writes data to disk asynchronously by default. If it is not turned on, it may cause data loss for a period of time when the power is off.

    Because redis itself synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time.

  17. appendfilename appendonly.aof: Specify the update log file name, the default is appendonly.aof

  18. appendfsync everysec: Specify update log conditions.

    There are three optional values:

    • no: Means to wait for the operating system to synchronize the data cache to the disk (fast)
    • always: Indicates that fsync() is manually called to write data to disk after each update operation (slow, safe)
    • everysec: means to synchronize once per second (compromise, default)
  19. vm-enabled no: Specify whether to enable the virtual memory mechanism, the default value is no

    The VM mechanism stores the data in pages, and Redis swaps the less-accessed pages, that is, cold data, to disk, and the more accessed pages are automatically swapped out from the disk to the memory.

  20. vm-swap-file /tmp/redis.swap: virtual memory file path, the default is /tmp/redis.swap, multiple redis instances cannot be shared.

  21. vm-max-memory 0: Store all data larger than vm-max-memory in virtual memory, the default is 0.

    All Redis index data (keys) are stored in memory, regardless of the value of vm-max-memory.

    When vm-max-memory is set to 0, all values ​​actually exist on the disk.

  22. vm-page-size 32: Specify the page size in the swap file, the default is 32 bytes.

    Redis swap files are divided into many pages. One object can be stored on multiple pages, but one page cannot be shared by multiple objects.

    The vm-page-size should be set according to the size of the stored data. If storing a lot of small objects, the page size is best set to 32 or 64bytes; if storing large objects, you can use a larger page, if you are not sure , Just use the default value

  23. vm-pages 134217728: Specify the number of pages in the swap file. The default size is 2 27 or 134217728.

  24. vm-max-threads 4: Set the number of threads accessing the swap file, the default value is 4.

    It is best not to exceed the number of cores of the machine. If it is set to 0, then all operations on the swap file are serial, which may cause a relatively long delay.

  25. glueoutputbuf yes: Set whether to combine smaller packages into one package and send when you want the client to reply. The default is on.

  26. hash-max-zipmap-entries 64: Specifies to use a special hash algorithm when a certain number is exceeded

  27. hash-max-zipmap-value 512: Specifies that a special hash algorithm is used when the largest element exceeds a certain critical value

  28. activerehashing yes: Specify whether to activate the reset hashing, the default is yes

  29. include /path/to/local.conf: Specify to include other configuration files. You can use the same configuration file between multiple redis instances on the same host, and each instance also has its own specific configuration file.

Redis data types

There are 5 data types in Redis:

  • String
  • List
  • Hash dictionary
  • Set
  • Sorted Set

Different data types have different command methods.

String related commands

set (key) (value)			设置key = value
append (key) (value2)		在key对应的value尾部追加value2
get	(key)					获取key对应的value
incr (key)				将key对应的value加1
incrby (key) (value1)		将key对应的value加上value1
ttl (key)					获取key到期的剩余时间
rename (key) (key_new)		对key重命名

SET key value                   设置key=value
GET key                         或者键key对应的值
GETRANGE key start end          得到字符串的子字符串存放在一个键
GETSET key value                设置键的字符串值,并返回旧值
GETBIT key offset               返回存储在键位值的字符串值的偏移
MGET key1 [key2..]              得到所有的给定键的值
SETBIT key offset value         设置或清除该位在存储在键的字符串值偏移
SETEX key seconds value         键到期时设置值
SETNX key value                 设置键的值,只有当该键不存在
SETRANGE key offset value       覆盖字符串的一部分从指定键的偏移
STRLEN key                      得到存储在键的值的长度
MSET key value [key value...]   设置多个键和多个值
MSETNX key value [key value...] 设置多个键多个值,只有在当没有按键的存在时
PSETEX key milliseconds value   设置键的毫秒值和到期时间
INCR key                        增加键的整数值一次
INCRBY key increment            由给定的数量递增键的整数值
INCRBYFLOAT key increment       由给定的数量递增键的浮点值
DECR key                        递减键一次的整数值
DECRBY key decrement            由给定数目递减键的整数值
APPEND key value                追加值到一个键
DEL key                         如果存在删除键
DUMP key                        返回存储在指定键的值的序列化版本
EXISTS key                      此命令检查该键是否存在
EXPIRE key seconds              指定键的过期时间
EXPIREAT key timestamp          指定的键过期时间。在这里,时间是在Unix时间戳格式
PEXPIRE key milliseconds        设置键以毫秒为单位到期
PEXPIREAT key milliseconds-timestamp        设置键在Unix时间戳指定为毫秒到期
KEYS pattern                    查找与指定模式匹配的所有键
MOVE key db                     移动键到另一个数据库
PERSIST key                     移除过期的键
PTTL key                        以毫秒为单位获取剩余时间的到期键。
TTL key                         获取键到期的剩余时间。
RANDOMKEY                       从Redis返回随机键
RENAME key newkey               更改键的名称
RENAMENX key newkey             重命名键,如果新的键不存在
TYPE key                        返回存储在键的数据类型的值。

List

lpush (listkey) (value)			给listkey列表添加(一个或多个)元素(在前面添加)
rpush (listkey) (value)			给listkey列表添加(一个或多个)元素(在后面添加)
llen (listkey)					获取listkey的长度
lrange (listkey) (start) (end)	从start位置至end位置获取其中的元素

BLPOP key1 [key2 ] timeout 取出并获取列表中的第一个元素,或阻塞,直到有可用
BRPOP key1 [key2 ] timeout 取出并获取列表中的最后一个元素,或阻塞,直到有可用
BRPOPLPUSH source destination timeout 从列表中弹出一个值,它推到另一个列表并返回它;或阻塞,直到有可用
LINDEX key index 从一个列表其索引获取对应的元素
LINSERT key BEFORE|AFTER pivot value 在列表中的其他元素之后或之前插入一个元素
LLEN key 获取列表的长度
LPOP key 获取并取出列表中的第一个元素
LPUSH key value1 [value2] 在前面加上一个或多个值的列表
LPUSHX key value 在前面加上一个值列表,仅当列表中存在
LRANGE key start stop 从一个列表获取各种元素
LREM key count value 从列表中删除元素
LSET key index value 在列表中的索引设置一个元素的值
LTRIM key start stop 修剪列表到指定的范围内
RPOP key 取出并获取列表中的最后一个元素
RPOPLPUSH source destination 删除最后一个元素的列表,将其附加到另一个列表并返回它
RPUSH key value1 [value2] 添加一个或多个值到列表
RPUSHX key value 添加一个值列表,仅当列表中存在

Hash dictionary, hash table

hset person (key) (value)		设置person哈希表中的key键对应值为value
hgetall person					显示person哈希表中所有的key和value
hkeys person					显示person哈希表中所有的keys
hvals person					


HDEL key field[field...] 删除对象的一个或几个属性域,不存在的属性将被忽略
HEXISTS key field 查看对象是否存在该属性域
HGET key field 获取对象中该field属性域的值
HGETALL key 获取对象的所有属性域和值
HINCRBY key field value 将该对象中指定域的值增加给定的value,原子自增操作,只能是integer的属性值可以使用
HINCRBYFLOAT key field increment 将该对象中指定域的值增加给定的浮点数
HKEYS key 获取对象的所有属性字段
HVALS key 获取对象的所有属性值
HLEN key 获取对象的所有属性字段的总数
HMGET key field[field...] 获取对象的一个或多个指定字段的值
HSET key field value 设置对象指定字段的值
HMSET key field value [field value ...] 同时设置对象中一个或多个字段的值
HSETNX key field value 只在对象不存在指定的字段时才设置字段的值
HSTRLEN key field 返回对象指定field的value的字符串长度,如果该对象或者field不存在,返回0.
HSCAN key cursor [MATCH pattern] [COUNT count] 类似SCAN命令

Set collection

SADD key member [member ...] 	添加一个或者多个元素到集合(set)里
SCARD key 						获取集合里面的元素数量
SDIFF key [key ...] 			获得队列不存在的元素
SDIFFSTORE destination key [key ...] 获得队列不存在的元素,并存储在一个关键的结果集
SINTER key [key ...] 			获得两个集合的交集
SINTERSTORE destination key [key ...] 获得两个集合的交集,并存储在一个集合中
SISMEMBER key member 			确定一个给定的值是一个集合的成员
SMEMBERS key 					获取集合里面的所有key
SMOVE source destination member 移动集合里面的一个key到另一个集合
SPOP key [count] 				获取并删除一个集合里面的元素
SRANDMEMBER key [count] 		从集合里面随机获取一个元素
SREM key member [member ...] 	从集合里删除一个或多个元素,不存在的元素会被忽略
SUNION key [key ...] 			添加多个set元素
SUNIONSTORE destination key [key ...] 合并set元素,并将结果存入新的set里面
SSCAN key cursor [MATCH pattern] [COUNT count] 迭代set里面的元素

Sorted Set

 
ZADD key score1 member1 [score2 member2] 
							添加一个或多个成员到有序集合,或者如果它已经存在更新其分数
ZCARD key 					得到的有序集合成员的数量
ZCOUNT key min max 			计算一个有序集合成员与给定值范围内的分数
ZINCRBY key increment member 在有序集合增加成员的分数
ZINTERSTORE destination numkeys key [key ...] 
							多重交叉排序集合,并存储生成一个新的键有序集合。
ZLEXCOUNT key min max 		计算一个给定的字典范围之间的有序集合成员的数量
ZRANGE key start stop [WITHSCORES] 由索引返回一个成员范围的有序集合(从低到高)
ZRANGEBYLEX key min max [LIMIT offset count]返回一个成员范围的有序集合(由字典范围)
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] 
							返回有序集key中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员,有序集成员按 score 值递增(从小到大)次序排列
ZRANK key member 			确定成员的索引中有序集合
ZREM key member [member ...] 从有序集合中删除一个或多个成员,不存在的成员将被忽略
ZREMRANGEBYLEX key min max 	删除所有成员在给定的字典范围之间的有序集合
ZREMRANGEBYRANK key start stop 在给定的索引之内删除所有成员的有序集合
ZREMRANGEBYSCORE key min max 在给定的分数之内删除所有成员的有序集合
ZREVRANGE key start stop [WITHSCORES] 
							返回一个成员范围的有序集合,通过索引,以分数排序,从高分到低分
ZREVRANGEBYSCORE key max min [WITHSCORES] 
							返回一个成员范围的有序集合,以socre排序从高到低
ZREVRANK key member 		确定一个有序集合成员的索引,以分数排序,从高分到低分
ZSCORE key member 			获取给定成员相关联的分数在一个有序集合
ZUNIONSTORE destination numkeys key [key ...] 
							添加多个集排序,所得排序集合存储在一个新的键
ZSCAN key cursor [MATCH pattern] [COUNT count] 增量迭代排序元素集和相关的分数

Guess you like

Origin blog.csdn.net/weixin_40849588/article/details/98262447