In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
For redis installation, please click here: Redis installation
Redis data structure
1 、 string
String is a simple type, which can be understood as the same type as Memcached, and the operations supported by a key corresponding to a value; are also similar to those of Memcached, but it has more features. You can store binary objects.
Make an alias before the operation, because the command to enter the redis is very long, and it is troublesome to type it every time.
[root@gz1 etc] # alias redis-cli='/usr/local/redis/bin/redis-cli' [root@gz1 etc] # vim .bashrc / / Boot will automatically load alias redis-cli='/usr/local/redis/bin/redis-cli' [root@gz1 etc] # redis-cli / / if the password is set The command to log in is: redis-cli-a 123456127.0.0.1 get key1 6379 > example 1: 127.0.0.1 get key1 6379 > set key1 helloOK127.0.0.1:6379 > get key1 "hello" 127.0.0.1 mset key1 helloworld key2 howareyou key3 youarewelcomeOK127.0.0.1:6379 > mget key1 key2 key31) "helloworld" 2) "howareyou" 3) "youarewelcome" Ctrl+d exit
2 、 list
List is a linked list structure (equivalent to a pipeline, which can insert and extract values from left and right at any time, different from stations that can only be first in and out). The main functions are push, pop, getting all values in a range, and so on. The key in the operation is understood as the name of the linked list. Using the List structure, we can easily achieve functions such as ranking the latest messages (such as Weibo's TimeLine). Another application of list is message queue, which can use the push operation of list to store the task in list, and then the worker thread uses the pop operation to fetch the task for execution.
127.0.0.1 lpush list1 6379 > lpush list1 aaa (integer) 2127.0.0.1 lpush list1 aaa 6379 > lpush list1 bbb (integer) 3127.0.0.1 lpush list1 bbb 6379 > lpush list1 "ccc ddd" (integer) 4127.0.1 6379 > lrange list1 0-1 / / get all values from the left Not from the command on the right 1) "ccc ddd" 2) "bbb" 3) "aaa" 4) "123" 127.0.0.1 ccc ddd 6379 > lpop list1 / / take a value from the left "ccc ddd" 127.0.0.1 aaa 6379 > lrange list1 0-11) "bbb" 2) "aaa" 127.0.0.1Ze6379 > rpush list1 fff / / insert the value (integer) 3127.0.0.1 from the right Lrange list1 0-11) "bbb" 2) "aaa" 3) "fff" 127.0.0.1 aaa 6379 > rpop list1 / / take a value from the right "127.0.1 aaa 6379 > lrange list1 0-11)" ccc ddd "2)" bbb "3)" aaa "
Set
Set is set, which is similar to the concept of set in our mathematics, such as adding and deleting elements, intersecting multiple sets, union, difference and so on. The key in the operation is understood as the name of the collection.
For example, in the Weibo app, you can store all the followers of a user in a collection and all their fans in a collection. Because Redis humanizes the collection by providing operations such as intersection, union, difference, etc., you can also use unused commands to choose whether to return the results to the client or store them in a new collection.
In addition, QQ also has a social function called "friend tag". You can tag your friends, such as "Gao Fu Shuai", "Bai Fu Mei", "Brother" and so on. At this time, you can use the collection of Redis to store each user's tag in a collection.
Example:
127.0.0.1 set1 6379 > sadd set1 aaa / / add set set1 values (integer) 1127.0.0.1 integer > sadd set1 bbb (integer) 1127.0.0.1 integer 6379 > sadd set1 ccc (integer) 1127.0.0.1 integer 6379 > sadd set1 ddd (integer) 1127.0.1 purl 6379 > smembers set1 / / get values in the set (unordered list) 1) "aaa" 2) "bbb" 3) "ddd" 4) "ccc"
Sorted set
Sorted set is an ordered set, which has one more weight parameter score than set, so that the elements in the collection can be arranged in an orderly manner according to score, such as a Sorted Sets that stores the scores of the whole class, whose collection value can be the student number of the students, and score can be the examination results, so that when the data is inserted into the collection, it has been sorted naturally.
Example:
127.0.0.1 zadd set2 6379 > score 10 aaa / / 10 as the weight parameter Same as (integer) 1127.0.0.1 bbb (integer) 1127.0.0.1 zadd set2 6379 > zadd set2 6 ccc (integer) 1127.0.0.1 bbb 6379 > zadd set2 13 "ab cd" (integer) 1127.0.0.1 ef-gh 6379 > zadd set2 8 "ef-gh" (integer) 1127.0.16379 > zrange set2 0-1 / / positive sequence output 1) "bbb" 2) "ccc" 3) "ef-gh" 4 ) "aaa" 5) "ab cd" 127.0.0.1) zrevrange set2 0-1 / / output in reverse order 1) "ab cd" 2) "aaa" 3) "ef-gh" 4) "ccc" 5) "bbb"
Hash
In Redis, we often package some structured information into hashmap, which is serialized on the client side and stored as a string value (usually in JSON format). Multiple key-value pairs can be stored in a hash, such as the user's nickname, age, and gender can be stored in a hash.
Example:
127.0.0.1 hset hash2 age 6379 > hset hash2 name tpp (integer) 1127.0.0.1 hset hash2 age 6379 > hset hash2 sex man (integer) 1127.0.0.1 hset hash2 age 6379 > hgetall hash2 / / take out 1) "name" 2) "tpp" 3) "age" 4) "25" 5) "sex" 6) "man" 127.0.1 > hget hash2 name / take out a single "tpp"
Redis persistence
Redis provides two ways of persistence, namely RDB (Redis DataBase) and AOF (Append Only File).
In short, RDB is to generate snapshots of the data stored by redis at different points in time and store them on media such as disks.
AOF is to achieve persistence from a different point of view, recording all write instructions executed by redis, and data recovery can be achieved by repeating these write instructions from front to back the next time redis is restarted.
In fact, both RDB and AOF can be used at the same time. In this case, if redis is restarted, AOF will be preferred for data recovery. This is because the data recovery in AOF is more complete.
If there is no need for data persistence, you can also turn off RDB and AOF mode, so that redis will become a pure memory database, just like Memcached.
Parsing of Redis configuration file
Configuration file: / usr/local/redis/etc/redis.conf
1. View and reset the configuration
127.0.1 dump.rdb 6379 > config get * 1) "dbfilename" 2) "dump.rdb" 3) "requirepass" 4) "5)" masterauth "6)" 7) "unixsocket" 8) "9)" logfile "10)" / usr/local/redis/var/redis.log "11)" pidfile "12)" / usr/local/redis/var/redis.pid "13)" maxmemory " 14) "0" 15) "maxmemory-samples" 16) "3" 17) "timeout" 18) "tcp-keepalive" 20) "0" 21) "auto-aof-rewrite-percentage" 22) "100" 23) "auto-aof-rewrite-min-size" 24) "67108864" 25) "hash-max-ziplist-entries" 26) "512" 27) "hash-max-ziplist-value" 28) "64" 29) "list-max-ziplist-entries" 30) "512" 31) "list-max-ziplist-value" 32) "64" 33) "set-max-intset-entries" 34) "512" 35) "zset-max-ziplist-entries" 36) "12837)" zset-max-ziplist-value "38)" 64 "39)" hll-sparse-max-bytes "40)" 3000 "41)" lua-time-limit "42) "5000" 43) "slowlog-log-slower-than" 44) "10000" 45) "latency-monitor-threshold" 46) "0" 47) "slowlog-max-len" 48) "12849)" port "50)" 6379 "51)" tcp-backlog "52)" 511 "53)" databases "54)" 16 "55)" repl-ping-slave-period "56)" 10 "57)" repl-timeout "58) "60" 59) "repl-backlog-size" 60) "1048576" 61) "repl-backlog-ttl" 62) "3600" 63) "maxclients" 64) "4064" 65) "watchdog-period" 66) "0" 67) "slave-priority" 68) "100" 69) "min-slaves-to-write" 70) "0" 71) "min-slaves-max-lag" 72) "10" 73) "hz" 74) "10" 75) "repl-diskless-sync-delay" 76) "5" 77) "no-appendfsync-on-rewrite" 78) "no" 79) "slave-serve-stale-data" 80) "yes" 81) "slave-read-only" 82) "stop-writes-on-bgsave-error" 83) "yes" 85) "daemonize" 86) "yes" 87) "rdbcompression" 88) "yes" 89) "rdbchecksum" 90) "yes" 91) "activerehashing" 92) "yes" 93) "repl-disable-tcp-nodelay" 94) "no" 95) "repl-diskless-sync" 96) "no" 97) "aof-rewrite-incremental-fsync" 98) "yes" 99) "aof-load-truncated" 100) "yes" 101) "appendonly" 102) "no" 103) "dir" 104) "/ usr/local/redis/var" "maxmemory-policy") "volatile-lru" 10000) "loglevel" 10000) "debug" 113) "client-output-buffer-limit" 114) "normal 000 slave 268435456 67108864 60 pubsub 33554432 8388608 60"unixsocketperm" 116) "0" 117) "slaveof" 10000) ")" notify-keyspace-events " 120) "" 121) "bind" 122) ""
2. General configuration of Redis
1) daemonize no / / by default, redis does not run in the form of daemon. You can control how redis runs through the daemonize configuration item.
2) pidfile / path/to/redis.pid / / when running as daemon, redis generates a pid file / var/run/redis.pid by default
3) bind 192.168.1.2 10.8.4.2 / / specifies the bound ip, which can have multiple
4) port 6379 / / designated listening port
5) unixsocket / tmp/redis.sock / / can also monitor socket
6) unixsocketperm 755 / / you can specify a permission of 755 when listening to sockets
7) timeout 0 / / when a redis-client has not requested to send the server, then the server has the right to actively close the connection. You can set the "idle timeout" through timeout. 0 means never close.
8) tcp-keepalive 0 / / tcp connection survival policy can be set through the tcp-keepalive configuration item (in seconds). If it is set to 60 seconds, the server side will issue an ACK request to the client whose connection is idle every 60 seconds to check whether the client has hung up, and the client will close its connection if it is unresponsive. If set to 0, no survival test will be performed.
9) loglevel notice / / log level, including debug, verbose, notice and warning.
10) logfile "" / / define the log path
11) syslog-ident redis / / if you want the log to be printed to syslog, control it through syslog-enabled. In addition, syslog-ident allows you to specify log flags in syslog.
12) syslog-facility local0 / / specify the device of syslog, which can be USER or local0-local7
13) databases 16 / / set the total number of databases, select n Select databases, 0-15
3. Redis snapshot configuration (RDB persistence)
1) save 9001 / / means that every 15 minutes and at least one key change is triggered.
2) save 30010 / / means that every 5 minutes and at least 10 key changes are triggered.
3) save 60 10000 / / means that every 60 seconds and at least 10000 key changes are triggered.
4) save "" / / this disables RDB persistence
5) stop-writes-on-bgsave-error yes / / RDB persistent writing to disk cannot avoid failure. By default, redis stops the write operation immediately if it fails. If you think it doesn't matter, you can use this option to turn off this feature.
6) rdbcompression yes / / whether to compress
7) rdbchecksum yes / / whether to perform data verification
8) dbfilename dump.rdb / / defines the name of the snapshot file
9) dir. / define the snapshot file storage path
4. Redis security-related configuration
1) requirepass 123456 / / set the password for redis-server. Login command is: redis-cli-a 123456
2) rename-command config tpplinux.config / / rename the config command to tpplinux.config to avoid misoperation, but it is not recommended to enable this feature if AOF persistence is used.
3) the definition of rename-command config "/ / is empty, which means that the config command is disabled.
5. Configuration related to Redis restrictions
1) maxclients 10000 / / limit the maximum number of client connections
2) maxmemory / / sets the maximum memory usage (in byte)
3) maxmemory-policy volatile-lru / / specify memory removal rules
4) maxmemory-samples 3 / / LRU algorithm and minimum TTL algorithm are not accurate algorithms, but estimates. So you can set the sample size. If redis checks the three key by default and selects the one of the LRU, then you can change the number of key samples.
6. Configuration related to Redis AOF persistence
1) appendonly yes / / enable persistence
2) appendfilename "appendonly.aof" / / specify the name of the aof file (in binary log form) and save it in the directory specified by the dir parameter
3) appendfsync everysec / / specifies the fsync () call mode. There are three modes: no (no fsync is called), always (fsync is called every time you write), and ecverysec (fsync is called once per second). The first is the fastest; the second is the safest, but the performance is poorer; the third is this scheme, which defaults to the third.
4) no-appendfsync-on-reweite no / / use no to avoid disk io blocking when the volume of writes is very large.
5) auto-aof-rewrite-percentage 10 / / specifies the circumstances under which aof rewriting will be triggered. The value is a ratio, and 10 means that the rewriting mechanism will be triggered when the growth of the aof file reaches 10%.
6) there is a condition for auto-aof-rewrite-min-size 64mb / / rewriting, that is, it cannot be lower than 64MB.
7. Redis slow log related configuration
For slow logs, you can set two parameters, one is the execution time in microseconds, and the other is the length of the slow log. When a new command is written to the log, the oldest one is removed from the command log queue.
1) if slowlog-log-slower-than is 10000 / / slower than 10000 microseconds, log is recorded.
2) slowlog-max-len 128 / / Log length
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.