Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis note collation (1): Redis installation configuration and data type operation

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

[TOC]

About Redis Redis is an open source (BSD licensed), in-memory data structure server that can be used as a database, cache and message queuing 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/Redis features Redis supports data persistence, you can save the data in memory on disk, and you can load it again when you restart. 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. The advantage of Redis is extremely high-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 Apps are used in scenarios with high concurrency and real-time requests. Eg Sina Weibo hash: watch list, fan list string: Weibo, fans (avoid using select count (*) from...) sorted set:TopN, popular Weibo and github,stackoverflow also use redisRedis 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 compilation: make installation: make install PREFIX=/home/uplooking/app/redisRedis configuration initial configuration modified configuration item in redis.conf bind uplooking01 daemonize yes (background running) logfile / opt/redis-3.2.0/logs/redis.log (log file, directory must exist) after startup service make is complete 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 you execute make install 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 to close the service ps-ef | after grep redis finds the process id, it can 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 the 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" uses the * sign to get all configuration items: redis localost:6379 > config get * editors can modify the configuration basic syntax by modifying the redis.conf file or using the config set command: 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"-- > valid for the current service Later, when the service is restarted, the 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 OK redis 6379 > GET name "redis.net.cn" We used the SET and GET commands of Redis in the above example. 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 regardless of the data type) set name yaohuiyingget key gets the value of the specified key get namegetrange key start end returns the subcharacters of the string value in the key Getrange name 1 4 Note: string indexing 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 to set when key does not exist, the only difference from set is that It is set only when key does not exist, but key exists and cannot be overwritten by setrange key offset value. The string value stored in a given key is overwritten with the value parameter, and the original value of key is partially overwritten from the offset offset, starting with the offset offset. The length of value overrides how long strlen key returns the length of the string value stored by key mset key value [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, not like the SETEX command In seconds, incr key increases the digital value stored in key by one pair of value corresponding to key. But value must be a string of numeric type INCRBY key increment will add the value stored by key plus the defined step size of the given increment value (increment) incrbyfloat key increment the value stored by key plus the given floating point increment value (increment) decr key will store the numeric value in key Minus the value stored by a decrby key decrement key minus the given 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. Instance redis 127.0.0.1 redis basic commands for caching likes 20 visitors 23000OKredis 127.0.0.1 redis basic commands for caching > HGETALL w3ckey1) "name" 2) "redis tutorial" 3) "description" 4) "redis basic commands for caching" 5) "likes" 6) "20" 7) "visitors" 8) some description information (name,description, likes, visitors) is stored in the w3ckey of hash in the above instance hash data type. 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 gets all the field hlen key for the floating-point value of the specified field plus incremental incrementhkeys key Get the number of fields of key hdel key field2 [field2] Delete one or more hash table fields hmget key field1 [field2] get the values of all given fields hmset key F1 v1 [f2 v2] simultaneously set 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] the key values in the iterative hash table are simple string lists for the list (list) Redis list, 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 1redis 127.0.0.1 1redis 6379 > lpush redis.net.cn mongodb (integer) 2redis 127.0.0.1 1redis 6379 > lpush redis.net.cn rabitmq (integer) 3redis 127.0.0.1 lrange redis.net.cn 0101) "rabitmq" 2) "mongodb" 3) "redis" redis 127.0.0.1 > list 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 operations from the right, lmurmuri-> left (header), and Rafael-> right (tail) blpop key1 [key2] timeout remove and get the first element in the list If it doesn't block the list until it times out [in seconds] or finds a popup element, in short, delete key1 key2. 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 a popup element is found, uplooking01:6379 > brpoplpush season season1 100 "spring" (7.59s) lindex key index gets the column by index Note one of the elements in the table: lindex key-1 gets the last element linsert key before | after pivot value inserts the element uplooking01:6379 > lrange season 0-1 before or after the element in the list. 1) "winter" 2) "autumn" 3) "summer" uplooking01:6379 > linsert season after summer spring (integer) 4 Uplooking01:6379 > lrange season 0-11) "winter" 2) "autumn" 3) "summer" 4) "spring" llen key gets the 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 1 2 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 this element to another list and return rpush key value1 [value2] add one or more values to the list rpushx key value add the value set (collection) Redis to the existing list Set 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). The sadd command adds a string element to the set collection corresponding to key, and successfully returns 1. If the element already exists in the collection, there is no return error for the set corresponding to 0MagneKey. Sadd key member instance redis 127.0.0.1 1redis 6379 > sadd redis.net.cn redis (integer) 1redis 127.0.0.1 sadd redis.net.cn redis 6379 > sadd redis.net.cn mongodb (integer) 1redis 127.0.0.1 sadd redis.net.cn redis 6379 > sadd redis.net.cn rabitmq (integer) 1redis 127.0.1 sadd redis.net.cn redis 6379 > sadd redis.net.cn rabitmq (integer) 0redis 127.0.0.1 sadd redis.net.cn redis 6379 > smembers redis.net.cn1) "rabitmq" 2) "mongodb" 3) "redis" Note Meaning: rabitmq has been added twice in the above example However, 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 collection (size) sdiff key1 [key2] returns the difference set A = {1,2,3,4,5} set B = {2,3,6 for a given set 8} A ∩ B = {2,3} A ∪ B = {1,2,3,4,5,6,8} A ∩ B (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 to dest sismember key member Determine whether the member element is a member of the collection 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 spop key in dest Remove and return a random element in the set srandmember key [count] return one or more random numbers in the set randomly return 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] removal One or more members of a set sunion key1 [key2] returns the union of all given sets-the concept of union in mathematics 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 Elements joined into destsscan key cursor [match pattern] [count count] iterative sets zset (ordered sets) 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 elements to the collection If the element exists in the collection, update the corresponding score zadd key score member instance redis 127.0.0.1 1redis 6379 > zadd redis.net.cn 0redis (integer) 1redis 127.0.0.1 zadd redis.net.cn 0redis 6379 > zadd redis.net.cn 0 mongodb (integer) 1redis 127.0.0.1 rabitmq 6379 > zadd redis.net.cn 0 rabitmq (integer) 1redis 127.0.0.1 zadd redis.net.cn 0redis 6379 > zadd redis.net.cn 0 rabitmq (integer) 0redis 127.0.0.1VR 6379 > ZRANGEBYSCORE redis.net. Cn 0 10001) "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.05www.baidu.com (integer) 1 uplooking01:6379 > zadd website 0.1www.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 gets 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 get the score 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 over the score of the specified member 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] synthesize members in a specified interval by returning an ordered set through the index interval A special writing zrange key 0-1 returns all elements zrangebylex key min max [limit offset count] returns members of the ordered set zrangebyscore key min max [withscores] [limit] through the dictionary interval, returns members within the specified interval of the ordered set by scores, zrank key member returns the index zrem key member [member...] of the specified members of 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 index zremrangebyscore key start stop removes all members of a given score range in key The score is that scorezrevrange key start stop [withscores] returns the members of the specified interval in the ordered key. Through the index, the score from high to low and zrange key start stop in turn zrevrangebysocre key max min [withscores] returns the members in the specified score range in the ordered key, and through the index, the score from high to low zrevrank key memeber returns the ranking of the specified member in the key. Key members sort by decreasing score 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 (including element members and element scores) uplooking01:6379 > zscan website 1 1) "0" in the new dest 2) 1) "www.jd.com" 2) "0.0050000000000000001" 3) "www.uplooking.com" 4) "0.02" 5) "www.baidu.com" 6) "0.0500000000000003" 7) "www.taobao.com" 8) the "1" Redis common command keys * enumerates all the keytype key to obtain the data type of the corresponding key select [0-15] to switch to a database rename oldKey newKey renames oldKey to belong to the password in the newKey terminal: the first: when connecting: redis-3.2.0] # src/redis-cli-h uplooking03 -a uplooking II: enter the password after login: auth uplooking (password) commands to stop the redis service: src/redis-cli-h uplooking01-p 6379 shutdown 1, The key-value-related command keys * takes out all the current key exists name to check whether the redis has name this key del name deletes the key name expire confirm 100 setting confirm this key100 second expires ttl confirm gets the valid duration of the confirm this key select 0 to 0 database redis default database is 0,15, 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 rename key2 key3 in the database renaming key2 to key3 type key2 returns the data type of key 2. The server-related command ping PONG returns the response whether the connection was successful or not. Echo prints some content on the command line: database quit / exit with the number of select 0per15 exit client dbsize returns the number of all key in the current database info returns the relevant information of redis config get dir/* real-time transfer storage received request flushdb delete the currently selected database Delete all databases in all databases from all key flushall in

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report