In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about the operation of Python redis. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Bitmap operation bitmap
Define
1. Bitmap is not a real data type, it is defined in a string type. 2. A value of a string type can store up to 512m bytes. The upper limit of bits: 2 ^ 32 # 1MB = 1024KB# 1KB = 1024Byte (bytes) # 1Byte = 8bit (bits)
Strong point
Real-time statistics can be carried out, extremely saving space. Officially, in a simulated environment of 128 million users, on a MacBookPro, typical statistics such as "number of daily users" consume less time than 50ms and occupy 16MB memory.
SETBIT command
Description: sets the binary value at a location
Syntax: SETBIT key offset value
Parameter: offset-offset starts at 0
Value-0 or 1
Example:
# default extension bit fills 127.0.0.1 ab 6379 > SET mykey abOK127.0.0.1:6379 > GET mykey "ab" 127.0.0.1 ab 6379 > SETBIT mykey 01 (integer) 0127.0.0.1 ab 6379 > GET mykey "\ xe1b" 127.0.0.1
GETBIT command
Description: get the value on a bit
Syntax: GETBIT key offset
Example:
127.0.0.1GETBIT mykey 6379 > GETBIT mykey 3 (integer) 0127.0.0.1 > GETBIT mykey 0 (integer) 1127.0.0.1
BITCOUNT command
Description: how many 1s are there in the value corresponding to the statistical key
Syntax: BITCOUNT key start end
Parameter: start/end represents a byte index
Example:
127.0.0.1 SET mykey1 abOK127.0.0.1:6379 [4] > BITCOUNT mykey (integer) 6127.0.1 integer > BITCOUNT mykey 0 (integer) 3
Application scenario case
# Statistics on the number of website users going online (looking for active users) the user name is key, the day of launch is regarded as offset, and the day of launch is set to users whose sample user name is user1:login, the first day of this year is launched, and the 30th day of launch is SETBIT user1:login 0 1 SETBIT user1:login 29 1 BITCOUNT user1:loginHash hash data type
Define
1. Key-value pairs composed of field and associated value 2. Field and value are string types 3. A hash contains up to 2 ^ 32-1 key-value pairs.
Advantages
1. Save memory space-under certain conditions, [1, the number of fields is less than 512, and the 2:value cannot exceed 64 bytes] 2. The values of fields can be obtained as needed.
Disadvantages (not suitable for hash situations)
1. Use the expired key function: the key expiration function can only perform expiration operations on keys, but not on hash fields. 2. The storage consumption is greater than the string structure.
Basic command operation
# 1. Set a single field HSET key field valueHSETNX key field value# 2, set multiple fields HMSET key field value field value# 3, return the number of fields HLEN key# 4, determine whether a field exists (not exist return 0) HEXISTS key field# 5, return field value HGET key field# 6, return multiple field values HMGET key field filed# 7, return all key value pairs HGETALL key# 8, return all field names HKEYS key# 9, return all values HVALS key# 10, Delete specified field HDEL key field # 11, perform integer increment operation on field corresponding value HINCRBY key field increment# 12, perform floating point increment operation HINCRBYFLOAT key field increment on field corresponding value
Python operation hash
# 1. Update the attributes of a piece of data. If not, create a new hset (name, key, value) # 2, read the specified attributes of this data, return string type hget (name, key) # 3, update the data in batches (if not, create) attributes. The parameter is dictionary hmset (name, mapping) # 4, batch read data (if not new) attributes hmget (name, keys) # 5, get all attributes and corresponding values of this data. Return dictionary type hgetall (name) # 6, get all attribute names of this data, return list type hkeys (name) # 7, delete the specified attribute hdel (name, * keys) of this data
Application scenario: user dimension data statistics
User dimension statistics include: followers, fans, favorites and posts. Users are key, and different dimensions are field,value. For example, five people are followed by HSET user:10000 fans 5 HINCRBY user:10000 fans 1.
Python operation hash
Import redis# creates a connection object for redis database r = redis.Redis (password='123456') # operates hashr.hset ('pyhk1',' username', 'aid2102') r.hmset (' pyhk1', {'age': 18,' major': 'python'}) print (r.hget (' pyhk1', 'username'). Decode () print (r.hmget (' pyhk1', ['username') 'age']) print (r.hgetall (' pyhk1')) # dictionary derived data = {k.decode (): v.decode () for k, v in r.hgetall ('pyhk1'). Items ()} print (data) r.hdel (' pyhk1','age') print (r.hgetall ('pyhk1')) # Delete key r.delete (' pyhk1') collection data type (set)
Characteristics
1. Unordered, deduplicated 2, element is of string type 3, containing up to 2 ^ 32-1 elements
Basic command
# 1. Add one or more elements to remove duplicates automatically The return value is the number of elements successfully inserted into the collection SADD key member1 member2# 2, view all elements in the collection SMEMBERS key# 3, delete one or more elements, there is no element that automatically ignores SREM key member1 member2# 4, whether the element exists SISMEMBER key member# 5, and randomly returns the specified number of elements in the collection. Default is 1 SRANDMEMBER key [count] # 6, pop-up member SPOP key [count] # 7, and return the number of elements in the collection. It does not traverse the entire collection, but it stores SCARD key# 8 in the key, moves elements from the source collection to the target set SMOVE source destination member# 9, subtracts (number1 1 2 3 number2 1 24 result is 3) SDIFF key1 key2# 10, subtracts are saved to another collection SDIFFSTORE destination key1 key2# 11, intersection SINTER key1 key2SINTERSTORE destination key1 key2# 11, union SUNION key1 key2SUNIONSTORE destination key1 key2
Case: common concern of Sina Weibo
# requirements: when a user visits another user, it shows which of the two users have followed the same user # Design: put the users followed by each user in the collection Find the intersection # to achieve: user001 = {'peiqi','qiaozhi','danni'} user002 = {' peiqi','qiaozhi','lingyang'} user001 and user002's common concern is: the SINTER user001 user002 result is: {'peiqi','qiaozhi'}
Python operation set
Import redisr = redis.Redis (password='123456')''Marshal: Zhang Fei, Xu Yun, Zhao Yun, Ma Chao, Zhou Yu, Wen Chen: Zhuge Liang, Zhou Yu, Sima Yi. Result: 1. Pure generals 2. Pure Wenchen 3. Both civil and military 4. Operations of set collection type r.sadd ('generals', 'Zhang Fei','Xu Jun', 'Zhao Yun','Ma Chao', 'Zhou Yu') r.sadd ('Wenchen', 'Zhuge Liang', 'Zhou Yu', 'Sima Yi') data1 = r.sdiff ('generals', 'Wenchen') result = [] for item in data1: result.append (item.decode ()) print ('Pure Wushu General:' Result) data2 = r.sdiff ('Wenchen', 'General') result = [] for item in data2: result.append (item.decode ()) print ('Pure Wenchen:', result) data3 = r.sinter ('Wenchen', 'General') result = [] for item in data3: result.append (item.decode () print ('Wenchen:', result) data4 = r.sunion ('Wenchen' 'generals') result = [] for item in data4: result.append (item.decode ()) print ('generals:', result) ordered set sortedset
Characteristics
1. Ordered, deduplicated 2, elements are string type 3, each element is associated with a floating point score (score), and the elements in the set are arranged in the order from small to large (points can be the same) 4, up to 2 ^ 32-1 elements
Example
An ordered collection of fruit prices
Score 2.04.06.08.010.0 element watermelon, grape, mango, banana, apple
An orderly collection of employees' salaries
Score 600080001000012000
Element lucytomjimjack
A number of people who are reading some technical books.
Score 300400555666777 element core programming Avanti bin Laden Armstrong Bill Gates
Ordered collection of common commands
# add a member to the ordered set the return value is the number of elements successfully inserted into the collection zadd key score member# View specified interval element (ascending order) zrange key start stop [withscores] # View specified interval element (descending order) zrevrange key start stop [withscores] # View the score zscore key member# of the specified element return specified interval element # offset: how many elements are skipped # count: return Several # parentheses: open interval zrangebyscore fruits (2.08.0zrangebyscore key min max [withscores] [limit offset count] # shows 10 members per page Display the membership information on page 5: # limit 40 1 records MySQL: 10 records per page Display the record on page 5 # limit 40 limit 1 minute limit 2 Magi 3 shows that: article 345 record # Delete member zrem key member# increase or decrease score zincrby key increment member# return element ranking zrank key member# return element reverse ranking zrevrank key member# delete elements in specified range zremrangebyscore key min max# returns the number of elements in the set zcard key# returns the number of elements in the specified range zcount key min maxzcount salary 6000 8000 zcount salary (6000 800mm 6000)
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.