In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Introduction to Redis
Redis is an open source (BSD license), in-memory data structure server that can be used as a database, cache and message queue agent.
It supports strings, hash tables, lists, collections, ordered collections, bitmaps, hyperloglogs and other data types.
Built-in replication, Lua scripting, LRU recall, transactions, and different levels of disk persistence, while providing high availability through Redis Sentinel and automatic partitioning through Redis Cluster.
In short, Redis is an in-memory database for "key / value" data types, which can meet our needs for fast reading and writing of massive data.
Redis is a NoSQL product.
Official website: http://www.redis.io
Or: http://redis.cn/
Characteristics of Redis
Redis supports data persistence. You can save the data in memory on disk and load it again when you restart it.
Redis not only supports simple KMTV data, but also provides storage of data structures such as list,set,zset,hash.
Redis supports data backup, that is, data backup in master-slave master-slave mode.
Advantages of Redis
Extremely high performance-Redis reads at 11w/s and writes at 8.1w/s.
Rich data types-Redis supports Strings,Lists,Hashes,Sets, or Ordered Sets data type manipulation for binary cases.
Atomicity-all operations of Redis are atomic, while Redis also supports the execution of atomicity after several operations are merged.
Rich features-Redis also supports public/subscribe, notification, key expiration and other features.
Redis application
Applied in scenarios with high concurrency and real-time requests, eg Sina Weibo
Hash: follower list, fan list
String: Weibo, fans
(avoid using select count (*) from...)
Sorted set:
TopN, popular Weibo
And github.
Stackoverflow also
Using redis.
Redis installation configuration
Redis installation
It can be used by decompressing directly under windows, which mainly describes the installation method under Linux:
Decompress:
Tar-zxvf soft/redis-3.2.0.tar.gz-C app/
Rename:
Mv app/redis-3.2.0/ app/redis
Compile:
Make
Installation:
Make install PREFIX=/home/uplooking/app/redis
Redis configuration
Initial configuration
Fix the configuration item in redis.conf
Bind uplooking01
Daemonize yes (running in the background)
Logfile / opt/redis-3.2.0/logs/redis.log (log file, directory must exist)
Start the service
After the make is completed, the compiled redis server program redis-server and the client program redis-cli for testing will appear in the redis-3.2.0/src directory.
Redis-3.2.0] $src/redis-server redis.conf
Of course, if make install is executed again, a bin directory will be generated under the specified installation directory, and there will also be related commands to manipulate Redis.
Start the client
Redis-3.2.0] $src/redis-cli-h localhost-p 6379
Shut down the service
Ps-ef | grep redis
Just find the process id and go directly to kill-9.
Redis configuration View
The configuration file for Redis is located in the installation directory of Redis, and the file name is redis.conf. You can view or set configuration items through the config command.
View
Syntax, the format of Redis config command is as follows:
Redis localost:6379 > config get config_set_name
Eg: redis localost:6379 > config get loglevel
1) "loglevel"
2) "notice"
Use the * sign to get all configuration items: redis localost:6379 > config get *
Editing
You can modify the configuration by modifying the redis.conf file or by using the config set command
Basic syntax:
Redis localost:6379 > config set conf_setting_name new_value
Eg: redis localost:6379 > config set loglevel "warning"
Redis localost:6379 > config get loglevel
1) "loglevel"
2) "warning"-> is valid for the current service, and will not expire until the service is restarted.
Redis operation
String (string)
String is the most basic type of redis, which you can understand as exactly the same type as memcached, with a key corresponding to a value.
The string type is binary safe. It means that the string of redis can contain any data. Such as jpg images or serialized objects.
String type is the most basic data type of Redis, and a key can store 512MB as much as possible.
Example
Redis 127.0.0.1 redis.net.cn 6379 > SET name "redis.net.cn"
OK
Redis 127.0.0.1 6379 > GET name
"redis.net.cn"
In the above example, we used Redis's SET and GET commands. The key is name and the corresponding value is redis.net.cn.
Note: a key can store 512MB at most.
Common string operation commands are as follows:
Set key value sets the value of the specified key (overrides the data type ignored) set name yaohuiyingget key gets the value of the specified key get namegetrange key start end returns the subcharacter getrange name 1 4 of the string value in key Note: the string index starts at 0 Get index fragment [start, end], header and tail getset key value sets the value of the given key to value, and returns the old value of key (old value) getset name liuxiangqian returns the value of yaohuiyingmget key1 [key2..] Get all (one or more) values of a given key mget name name1setex key seconds value associates the value value to key and sets the expiration time of key to seconds (in seconds) setnx key value only sets the value of key when key does not exist, the only difference from set is that it is set only when key does not exist, while key exists Unable to overwrite setrange key offset value to overwrite the string value stored in a given key with the value parameter. Starting from the offset offset, the original value of key is partially overwritten. From the offset offset, how long the value is, the length of the string value stored by key is returned by strlen key [key value.] Simultaneously set one or more key-value pairs to MSETNX key value [key value.] Set one or more key-value pairs at the same time if and only if psetex key milliseconds value does not exist for all given key this command is similar to the SETEX command, but it sets the lifetime of the key in milliseconds instead of, as the SETEX command does, incr key increments the corresponding numeric values stored in key by a pair of value corresponding to key by + 1 But value must be a string of numeric type INCRBY key increment will add the value stored by key plus the given increment value (increment) the defined step size incrbyfloat key increment the value stored by key plus the given floating point increment value (increment) decr key will subtract the numerical value stored in key minus the value stored by decrby key decrement key minus the given decrement value (decrement) append key value if key already exists and is a string The APPEND command appends value to the end of the original value of key
Hash (hash)
Redis hash is a collection of key-value pairs. Redis hash is a mapping table for field and value of type string, and hash is particularly suitable for storing objects.
Example
Redis 127.0.0.1 description 6379 > HMSET w3ckey name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000
OK
Redis 127.0.0.1 6379 > HGETALL w3ckey
1) "name"
2) "redis tutorial"
3) "description"
4) "redis basic commands for caching"
5) "likes"
6) "20"
7) "visitors"
8) "23000"
In the above example, the hash data type stores some description information (name,description, likes, visitors) into the w3ckey of hash. Use hgetall to get all the content.
Each hash can store 232-1 key-value pairs (over 4 billion).
Common command operations are as follows:
Hexists key field looks at the hash table key. Whether the specified field exists or not means no. 1 indicates that hget key field gets the value of the specified field in the hash table hgetall key gets all fields and values hincrby key field increment is the integer value of the specified field plus incremental incrementhincrbyfloat key field increment is the floating point value of the specified field plus incremental incrementhkeys key gets the number of all fields hlen key gets key the number of fields hdel key field2 [field2] delete one or more A hash is a table field hmget key field1 [field2] gets the values of all given fields hmset key F1 v1 [f2 v2] simultaneously sets multiple file-value to key hset key field value sets the field of the hash table key to valuehsetnx key field value when field does not exist Set the value of the related field hvals key to get all the values hscan key cursor [MATCH pattern] [COUNT count] key-value pairs in the iterative hash table
List (list)
The Redis list is a simple list of strings sorted in the order in which they are inserted. You can add an element to the head (left) or tail (right) of the list.
Example
Redis 127.0.0.1 6379 > lpush redis.net.cn redis
(integer) 1
Redis 127.0.0.1 6379 > lpush redis.net.cn mongodb
(integer) 2
Redis 127.0.0.1 6379 > lpush redis.net.cn rabitmq
(integer) 3
Redis 127.0.0.1 6379 > lrange redis.net.cn 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
Redis 127.0.0.1 purl 6379 >
Lists can store up to 232-1 elements (4294967295, each list can store more than 4 billion).
Common operation commands are as follows:
Note: the operation of list can be divided into operations from the left or from the right.
Lmuri-> left (head), RMurray-> right (tail)
Blpop key1 [key2] timeout removes and fetches the first element in the list, in short, deleting key1 key2. If it does not block the list until it times out [in seconds] or finds a popup element. If there is no element, the blocking condition is either the time exceeds the timeout Or find a new element coming in brpop key1 [key2] timeout and get the last element in the list. If it does not block the list until it times out or finds that the popup element is the same as blpop, the difference is that the direction of deleting the element is inconsistent. Brpoplpush source destination timeout pops up a value from the list. Insert the pop-up element into another list and return it If no element blocks the list until the wait timeout is known or the popup element is found uplooking01:6379 > brpoplpush season season1 100 "spring" (7.59s) lindex key index gets the elements in the list by index. Note one: lindex key-1 gets the The last element linsert key before | after pivot value inserts the element uplooking01:6379 > lrange season 0-11) "winter" 2) "autumn" 3) "summer" uplooking01:6379 > linsert season after summer spring (integer) before or after the element in the list ) 4 uplooking01:6379 > lrange season 0-11) "winter" 2) "autumn" 3) "summer" 4) "spring" llen key get list length List size lpop key moves out and gets the first element of the list lpush key value1 [value2] inserts one or more values into the list header lpushx key value inserts one or more values into the existing list header lrange key start stop gets the element in the list specified return [start Stop] if you want to remove all the elements in the lrange, lrange key 0-1lrem key count value removes the list element count > 0: removes the element with the value value from start to tail 1 means to remove 1 element count
< 0: 从尾往头移除值为 value 的元素,-2时,表示移除2个元素 count = 0: 移除所有值为 value 的元素。lset key index value 通过索引设置列表元素的值(list.set(i, value)) 需要大家注意的是一个特殊的索引-1,表示最后一个元素ltrim key start stop 对一个列表进行修剪(trim),也就是说,让列表只保留指定区间内的元素, 删除其它元素 [start, stop] uplooking01:6379>Lrange season 0-11) "winter" 2) "autumn" 3) "summer" 4) "spring" uplooking01:6379 > ltrim season 12 OK uplooking01:6379 > lrange season 0-11) "autumn" 2 ) "summer" rpop key moves out and gets the last element of the list rpoplpush source destination removes the last element of the list And add the element to another list and return rpush key value1 [value2] add one or more values to the list rpushx key value add values to the existing list
Set (collection)
The Set of Redis is an unordered collection of type string.
The collection is implemented through a hash table, so the complexity of adding, deleting, and finding is all O (1).
Sadd command
Add a string element to the set collection corresponding to key, and successfully return 1. If the element already exists in the collection, there is no return error for the set corresponding to 0MagneKey.
Sadd key member
Example
Redis 127.0.0.1 6379 > sadd redis.net.cn redis
(integer) 1
Redis 127.0.0.1 6379 > sadd redis.net.cn mongodb
(integer) 1
Redis 127.0.0.1 6379 > sadd redis.net.cn rabitmq
(integer) 1
Redis 127.0.0.1 6379 > sadd redis.net.cn rabitmq
(integer) 0
Redis 127.0.0.1 6379 > smembers redis.net.cn
1) "rabitmq"
2) "mongodb"
3) "redis"
Note: rabitmq has been added twice in the above example, but depending on the uniqueness of the elements in the collection, the second inserted element will be ignored.
The maximum number of members in the collection is 4294967295 (each collection can store more than 4 billion members).
Common operation commands are as follows:
Sadd key member1 [member2] add one or more members to the collection scard key gets the number of members of the set (size) sdiff key1 [key2] returns the difference set A = {1, 2, 3, 4, 5} set B = {2, 3, 6, 8} A ∩ B = {2, 3} A ∪ B = {1, 2, 3, 4 5,6,8} Amurb (difference) = {1,4,5,6,8} {1,4 5} √ uplooking01:6379 > sdiff A B 1) "1" 2) "4" 3) "5" sdiffstore dest key1 [key2] returns and saves the given set difference to dest uplooking01:6379 > sdiffstore tmp A B (integer) 3 uplooking01:6379 > smembers Tmp 1) "1" 2) "4" 3) "5" sinter key1 [key2] returns the intersection of a given set uplooking01:6379 > sinter A B 1) "2" 2) "3" sinterstore dest key1 [key2] returns and saves the intersection of a given set Go to sismember key member in dest to determine whether the member element is a member of the set key uplooking01:6379 > sismember A 3 (integer) 1 uplooking01:6379smembers key returns all members of the collection smove src dest member moves the member element from src to dest spop key removes and returns a random element srandmember key [count] in the collection Returns one or more random numbers in the set randomly returns count elements in the set key No count. Return 1 uplooking01:6379 > srandmember A "1" uplooking01:6379 > srandmember A 21) "3" 2) "4" srem key member1 [member2] remove one or more members of a set sunion key1 [key2] returns the union of all given sets-that is, the union in mathematics Concept uplooking01:6379 > sunion A B 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 6) "6" 7) "8" sunionstore dest key1 [key2] Returns and saves the union of a given collection to the elements in the destsscan key cursor [match pattern] [count count] iterative set
Zset (ordered set)
Redis zset, like set, is a collection of elements of type string, and duplicate members are not allowed. The difference is that each element is associated with a score of type double. Redis sorts the members of the collection from small to large by scores.
The members of the zset are unique, but the score can be repeated.
The zadd command adds an element to the collection, and updates the corresponding score if the element exists in the collection
Zadd key score member
Example
Redis 127.0.0.1 redis 6379 > zadd redis.net.cn 0
(integer) 1
Redis 127.0.0.1 mongodb 6379 > zadd redis.net.cn 0
(integer) 1
Redis 127.0.0.1 rabitmq 6379 > zadd redis.net.cn 0
(integer) 1
Redis 127.0.0.1 rabitmq 6379 > zadd redis.net.cn 0
(integer) 0
Redis 127.0.0.1 6379 > ZRANGEBYSCORE redis.net.cn 0 1000
1) "redis"
2) "mongodb"
3) "rabitmq"
Common operation commands are as follows:
Zadd key score1 member1 [score2 member2] wants to add one or more members to an ordered collection Or update the scores of existing members uplooking01:6379 > zadd website 0.01 www.uplooking.com (integer) 1 uplooking01:6379 > zadd website 0.05 www.baidu.com (integer) 1 uplooking01:6379 > zadd website 0.1 www.google .com (integer) 1 uplooking01:6379 > zadd website 1 www.taobao.com (integer) 1 uplooking01:6379 > zadd website 0.005 www.jd.com (integer) 1zcard key get the number of members of an ordered set uplooking01:6379 > Zcard website (integer) 5zcount key min max calculates the number of members with a specified interval score in an ordered set to obtain a fraction interval [min] Max] element uplooking01:6379 > zcount website 0.01 0.1 (integer) 3zincrby key increment member ordered set increments incr uplooking01:6379 > zincrby website 0.01 www.uplooking.com "0.02" zinterstore dest nkeys key [key..] Calculate the intersection of a given ordered set or sets and save it in a new ordered set dest. Zlexcount key min max calculates the number of members in a specified dictionary interval in an ordered set. It needs to be clear that it can be used to represent the element with the lowest score. Use + to represent the element with the highest score zlexcount key-+ to get the size min=== > [member1 max=== > [member2] of the collection, and the number of elements is in the interval [member1. The number of elements in member2] redis > ZADD myzset 1 a 2 b 3 c 4 d 5 e 6 f 7 g (integer) 7 redis > zrange myzset 0-11) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" 7) "g" redis > ZLEXCOUNT myzset-+ (integer) 7 redis > ZLEXCOUNT myzset [c + (integer) 5 Redis > ZLEXCOUNT myzset-[c (integer) 3 redis > zrange key start stop [withscores] returns the ordered set by the index interval to synthesize the members in the specified interval a special writing zrange key 0-1 returns all the elements zrangebylex key min max [limit offset count] returns the member zrangebyscore key of the ordered set through the dictionary interval Min max [withscores] [limit] returns members in the specified interval of the ordered set by score zrank key member returns the index zrem key member [member...] of the specified member in the ordered set. Removing one or more members from an ordered set returns 1, deletion succeeds, and returns 0 Without this element zremrangebylex key min max removes all members of a given dictionary interval in an ordered set zremrangebyrank key start stop removes all members of a given ranking interval in key ranking is the index zremrangebyscore key start stop removes all members of a given score range in key is scorezrevrange key start stop [withscores] returns members in a specified range of ordered key Through the index, the score from high to low and the zrange key start stop in turn zrevrangebysocre key max min [withscores] returns the members within the specified score range in the ordered key, through the index, the score from high to low zrevrank key memeber returns the ranking of the specified member in the key, and the key members are sorted by decreasing score value zscore key member returns the score zunionstore dest numkeys key of member in key [key...] Calculate the union of a given ordered set or sets And store the elements in the zscan key cursor [match pattern] [count count] iterative ordered set in the new dest (including element members and element scores) uplooking01:6379 > zscan website 1 1) "0" 2) 1) "www.jd.com" 2) 0.00500000000000001 "3)" www.uplooking.com "4)" 0.02 "5)" www.baidu.com "6)" 0.050000000000000003 "7)" www.taobao.com " 8) "1"
Common Redis commands
Keys *
List all the key
Type key
Get the data type of the corresponding key
Select [0-15]
Switch to a database
Rename oldKey newKey
Rename oldKey to newKey
There are two ways to belong to passwords in a terminal:
The first kind:
When connecting: redis-3.2.0] # src/redis-cli-h uplooking03-a uplooking
The second part:
Enter the password after login:
Auth uplooking (password)
Command to stop the redis service:
Src/redis-cli-h uplooking01-p 6379 shutdown
1. Key value related commands
Keys * remove all current key
Exists name to see if redis has the key of name.
Del name Delete key name
Expire confirm 100sets the key100 of confirm to expire in seconds
Ttl confirm obtains the valid duration of the key of confirm.
Select 0 Select to 0 database redis default database is 0x15 for a total of 16 databases
Move confirm 1 moves the key in the current database to another database
Persist confirm removes the expiration time of the key confirm.
Randomkey randomly returns a key in the database
Rename key2 key3 renamed key2 to key3
Type key2 returns the data type of key
2. Server-related commands
Ping PONG returns whether the response is connected successfully.
Echo prints something on the command line
The database numbered by select 000015
Quit / exit exit client
Dbsize returns the number of all key in the current database
Info returns information about redis
Config get dir/* transfers and stores received requests in real time
Flushdb deletes all key in the currently selected database
Flushall deletes databases from all databases
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.