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 data manipulation-string

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

| | Save text, store numbers (integer, floating point), binary number | string operation-- set the string set key value # if the string key key already exists | Then overwrite the old value with the new value-- get the string get key # to return the value stored by the string key key-- set setnx key value # only if the key key does not exist, set the value of the key key to value only if the key does not exist, the effect is the same as set key value NX. NX means "Not exists". The # key does not exist and the naming returns 1 when the setting is successful; when the setting fails because the key already exists, the command returns 0. -- setting or getting the value of multiple character keys at the same time mset key value... (key value) mget key value... (key value) # multiple get,set executing at the same time-setting multiple nonexistent keys msetnx key value [key value...] # only if none of the given keys exist, setting values for all given keys has the same effect as executing multiple setnx at the same time. If at least one of the given keys exists, msetnx will not perform any setup operations. -- sets the new value and returns the old value getset key new-value # sets the value of the string key to new-value and returns the old value (old value) that the string key stored before setting the new value. -append content to the end of the string append key value # push the value value to the end of the stored content of the string key key-return the length of the value stored by the string key key # return the length of the value stored by the string key key # because the data of Redis cannot have two keys of the same name at the same time So we usually use a format like field1::field2::field3 to distinguish between multiple string keys of the same type. It is best to unify the use of symbols. Such as: website user corresponding email address: huangz:: email key, peter::email key to distinguish. (you can also choose other delimiters you like) | string index (positive or negative)-range setting setrange key index value # starting with the index index, overwrite the string value stored by the given value key with value. Only positive indexes are accepted. After the command returns overwrite, the string length-range value getrange key start end # returns the string value stored by the key key, the content between the two indexes start and end (closed interval). The index of the range value can be positive or negative. | set and get the number # as long as the value stored in the string key can be interpreted as a 64-bit integer or a 64-bit floating-point number of IEEE-754, the user can execute a command on the numeric value on the string key. # 10086 can be interpreted as an integer; 3.14 can be interpreted as a floating point number; + 123 can be interpreted as an integer; 1234. (many) values are too large to be stored using 64-bit integers; scientific counting such as 2.0e7 cannot be interpreted by Redis; 123ABC contains text and cannot be interpreted -- increase or decrease the value of a number (integer) # for a string key key that holds numbers, we can use the incrby,decrby command to increase or decrease its value incrby key increment increments the value stored by key, returns the value of key decrby key increment subtracts the value stored by key, and returns the value of key # if key does not exist during execution, the name command initializes the key key to 0 And then add or subtract-- add one and subtract one (integer) incr key equals incrby key 1 decr key equals decrby key 1 # because it's very common to add and subtract one for numbers. So Redis specifically created these two worthwhile command (counter API implementation) counter.py files: Counter (name, redis_client), Counter.incr (), Counter.get (), Count.reset (implementation of id generator API) id_generator.py file: IdGenerator (name, redis_client) IdGenerator.gen () IdGenerator.int ()-floating point self-increment and self-subtraction # the value stored for the string key key plus the floating point increment increment Return key value attached: there is no response to the decrbyfloat, but you can achieve the effect of decrbyfloat by giving a negative value. Redis > set num 10 redis > incrbyfloat num 3.14 redis > incrbyfloat num-2.04 Note: even if the string key stores numeric values, it can still execute commands such as append, strlen and setrange. Redis will convert numeric values into strings before executing commands | binary data operations-- set and get binary data # set,get,setnx Commands such as append set binary data # because the client redis_cli that comes with Redis cannot easily set binary data # so here we use Python client to do > > import redis > > r = redis.Redis () > > r.set ('bits' 0b10010100) # set the value of the string key bits to binary 10010100 > > True > > bin (int (r.get ('bits') # get the binary value stored by the string key bits (to be converted) > >' 0b10010100' > r.append ('bits') 0b111) # push 0b111 (that is, decimal 7) to the end of the existing binary bit in bits > 4 > > bin (int (r.get ('bits') # the value before push is 0b10010100 = 1488b10111001111' # the value after push is 0b1011100111 = 1487Mue-binary index # is different from the index when storing text When a string key stores binary bits, the binary index decreases from left to right. -- sets the value of the binary bit setbit key index value # sets the value of the binary bit on the given index to value The command returns the original stored old value of the set bit redis > setbit bits 21 (integer) 0mi-gets the value of the binary bit getbit key index # returns the value of the binary bit on a given index-- calculates the number of binary bits with a value of 1 bitcount key [start] [end] # calculates and returns the value stored by the string key The number of binary bits set to 1 is set by setting the start and end parameters to limit a range, and negative values can be used. -- binary bitop operation destkey key [key...] # performs bit operations on one or more string keys that hold binary bits and saves the results to destkey. Operation can be AND,OR,NOT. XOR ADN: logic union of one or more key OR: logic or XOR of one or more key: logic XOR of one or more key # logic algorithm: a ^ b = (a 'and b) or (an and b') NOT: the return value of the logic non # command for a given key is the byte length b1001101 b2 OR > bitop And b1-and-b2 # 00000101 (integer) 1 redis > bitop or b1-or-b2 b2 # 11111101 (integer) 1 redis > bitop xor b1-xor-b2 b1 b2 # 11111000 (intefer) 1 redis > bitop not noy-b1 b1 # 10110010 (integer) 1 # implement online headcount online_count.py # use Redis to cache popular images cache= Cache (redis_client) # set cached customers End file = open ('reids-logo.jpg' 'r') # Open file data = file.read () # read file data file.close () # close file cache.put ('redis-logo', data) # with the name redis-logo Cache the picture cache.get ('redis-logo') # retrieve the data of the redis-logo image | Note when storing Chinese-strlen,setrange,getrange is not suitable for Chinese # an English character only needs to be stored using a single byte, while a Chinese character needs to be stored with multiple bytes. # strlen,setrange,getrange is set for English, and they only work when the character is a single byte, but once we store multi-byte characters like Chinese, the three commands no longer apply. -- strlen example $redis-cli-- raw # must turn on the-- raw option when using Chinese in redis-cli To display Chinese redis > set msq 'world hello' OK redis > get msg redis > strlen msg # where strlen shows' world hello 'with a byte length of 12 bytes 12 # but what we really want to know is how many characters are contained in the msg key-the setrange and getrange examples setrange and getrange are similar: because of these two lives So that the index used is based on bytes rather than characters So when we call setrange or getrange to deal with Chinese, we can't get the result we want. -- conclusion # do not use strlen, setrange and getrange to process Chinese. # exception: if you want to know how many bytes are stored in Chinese, you can use strlen.

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