程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python series articles - detailed explanation of Python redis

編輯:Python
  • redis
import redis # Import redis modular
# The following connection pooling methods are recommended
# Set up decode_responses=True, Written in KV Right V by string type , If it is not added, the type of bytes written is .
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0, decode_responses=True)
rs = redis.Redis(connection_pool=pool)
# key="color",value="red", Set expiration time 5 second
rs.set('color', 'red', ex=5)
# And rs.set('color', 'red', ex=5) identical
rs.setex('color', 5, 'red')
# Print to get color The value of the key , Get value after timeout None
print(rs.get('color'))
# If color There is output None, If it doesn't exist , The output True
print(rs.set('color', 'green', nx=True))
# If color There is output True, If it doesn't exist , The output None
print(rs.set('color', 'yellow', xx=True))
# Batch assignment
rs.mset({'key1':'value1', 'key2':'value2', 'key3':'value3'})
# Batch fetch value
rs.mget('key1', 'key2', 'key3')
# Set the new value to blue, And return the value before setting
print(rs.getset('color', 'blue'))
rs.set('lang', 'Chinese')
# Index to 1-3 character
print(rs.getrange('lang', 1, 3)) # Return results : hin
# From the index number to 4 Characters begin to be replaced backwards
rs.setrange('lang', 4, 'a is great') # Return results :14
# stay lang Characters are appended to the corresponding value "!"
rs.append('lang', '!') # Return results : 15
print(rs.get('lang')) # Return results :China is great!
# return lang Length of corresponding value
print(rs.strlen('lang')) # Return results :15
# If total The corresponding value does not exist , be total The current value is set to 10
rs.incr('total', amount=10)
# At present total The corresponding value increases 1
rs.incr('total') # The result is 11
# At present total The corresponding value decreases 1
rs.decr('total') # The result is 10
# Each new element is inserted into list Leftmost , If list If it doesn't exist, it will create a new
rs.lpush('leftList', 1,2,3,4,5)
print(rs.lrange('leftList', 0, -1)) # Return results :['5', '4', '3', '2', '1']
# The newly inserted element is on the right , If list If it doesn't exist, create a new one
rs.rpush('rightList', 6,7,8,9,10)
print(rs.lrange('rightList', 0, -1)) # Return results :['6', '7', '8', '9', '10']
# stay list New element on the left , If list If it does not exist, it will not be created
rs.lpushx('noList', 'apple')
print(rs.llen('noList')) # Return results :0
# stay list The first one traversed from the left is '7' The elements of , Behind it ( If it is inserted before, use 'before') Insert elements '08'
rs.linsert('rightList', 'after', '7', '08')
print(rs.lrange('rightList', 0, -1)) # Return results :['6', '7', '08', '8', '9', '10']
# take list The middle index number is 1 The element of is changed to '-7'
rs.lset('rightList', 1, '-7')
print(rs.lrange('rightList', 0, -1)) # Return results :['6', '-7', '08', '8', '9', '10']
# Delete list The first one traversed from the left is '8' The elements of
rs.lrem('rightList', '8', 1)
print(rs.lrange('rightList', 0, -1)) # Return results :['6', '-7', '08', '9', '10']
# Pop the first element on the left
rs.lpop('rightList') # The return value is :'6'
print(rs.lrange('rightList', 0, -1)) # Return results :['-7', '08', '9', '10']
# Take out list The index number in is 1 Value
print(rs.lindex('rightList', 1)) # Return results :08
# Single key value operation
# Set up hash be known as hName The bond and the value of
rs.hset('hName', 'key1', 'value1')
rs.hset('hName', 'key2', 'value2')
# take hName Of key1 Corresponding value
print(rs.hget('hName', 'key1')) # Return results :value1
# Batch key value operation
rs.hmset('hName', {'key3': 'value3', 'key5': 'value5'})
print(rs.hmget('hName', 'key1', 'key2', 'key3')) # Return results :['value1', 'value2', 'value3']
# Take out hName All key values
print(rs.hgetall('hName')) # Return results :{'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key5': 'value5'}
# take hName All of the keys
print(rs.hkeys('hName')) # Return results :['key1', 'key2', 'key3', 'key5']
# take hName All of the values
print(rs.hvals('hName')) # Return results :['value1', 'value2', 'value3', 'value5']
# obtain hName Corresponding hash Key value to number
print(rs.hlen('hName')) # Return results :4
# Judge key2 Whether there is
print(rs.hexists('hName', 'key2')) # Return results :True
# Delete key2 Corresponding key value pairs
rs.hdel('hName', 'key2')
# Judge again key2 Whether there is
print(rs.hexists('hName', 'key2')) # Return results :False
# Add set elements , If the set does not exist, create a new
rs.sadd('mySet', 'one', 'two', 3)
# Returns the number of collection elements
print(rs.scard('mySet'))
# Return all elements
print(rs.smembers('mySet')) # result :{'two', 'one', '3'}
# Return all members
print(rs.sscan('mySet')) # result :(0, ['3', 'one', 'two'])
# Create a collection again mySet2
rs.sadd('mySet2', 3, 5, 7)
# Get the intersection of two sets
print(rs.sinter('mySet', 'mySet2')) # Return results :{'3'}
# Get the union of two sets
print(rs.sunion('mySet', 'mySet2')) # Return results :{'5', 'two', 'one', '7', '3'}
# Get the difference between two sets
print(rs.sdiff('mySet', 'mySet2')) # Return results :{'two', 'one'}
# take mySet and mySet2 Union , Save the results to storeSet Collection
print(rs.sunionstore('sotreSet', 'mySet', 'mySet2'))
print(rs.smembers('sotreSet')) # Return results :{'5', 'two', 'one', '7', '3'}
# Judge one Whether the element exists in the collection
print(rs.sismember('sotreSet', 'one'))
# Randomly delete and return an element in the collection
print(rs.spop('sotreSet'))
# Delete the element in the collection whose value is 5 The elements of
print(rs.srem('sotreSet', 5))
# Add set elements , If the set does not exist, create a new
rs.zadd('fruits', {'apple':1, 'banana':3, 'orange':5})
# Traverse all elements
print(rs.zrange("fruits", 0, -1)) # result :['apple', 'banana', 'orange']
# withscores=True It means to take a fraction
print(rs.zrange("fruits", 0, -1, withscores=True)) # result :[('apple', 1.0), ('banana', 3.0), ('orange', 5.0)]
# Traverse all elements according to the score from big to small
print(rs.zrevrange("fruits", 0, -1)) # result :['orange', 'banana', 'apple']
# obtain orange The fraction corresponding to the element
rs.zscore('fruits', 'orange') # result :5.0
# Take out the score >=3 and fraction <=5 The elements of
print(rs.zrangebyscore('fruits', 3, 5))
# Take out the score <=5 and fraction >=3 The elements of , Sort according to the score from big to small
print(rs.zrevrangebyscore('fruits', 5, 3))
# Traverse all elements , Returns a tuple
print(rs.zscan('fruits')) # result :(0, [('apple', 1.0), ('banana', 3.0), ('orange', 5.0)])
# Print the number of set elements
print(rs.zcard('fruits')) # result :3
# Returns the fraction in the set >=1 and fraction <=3 Element number
print(rs.zcount('fruits', 1, 3))
# Put in the collection apple The fraction of the element +5
rs.zincrby('fruits', 5, 'apple')
print(rs.zrange("fruits", 0, -1, withscores=True)) # Return results :[('banana', 3.0), ('orange', 5.0), ('apple', 6.0)]
# return orange The index number of the element in the collection
rs.zrank('fruits', 'orange') # result :1
# Sort by score from big to small , Take out banana Element index number
rs.zrevrank('fruits', 'banana') # result :2
# # Delete... From the collection apple Elements
rs.zrem('fruits', 'apple')
print(rs.zrange("fruits", 0, -1)) # Return results :['banana', 'orange']
# # Delete the set index number >=0 and Reference no. <=2 The elements of
rs.zremrangebyrank('fruits', 0, 2)
# Delete set score >=1 and fraction <=5 The elements of
rs.zremrangebyscore('fruits', 1, 5)
# Delete key by color The object of
rs.delete('color')
# Inquire about key by color Does the object exist
print(rs.exists('color')) # result :False
rs.sadd('mySet5', 'one', 'two')
# Set up key Timeout for
rs.expire('mySet5', time=5) # Company : second
# rename key Value
rs.rename('mySet5', 'set5')
# Randomly return one in the current library key, But it will not be deleted
print(rs.randomkey())
# View a certain key The type of the corresponding value
print(rs.type('mySet')) # Return results :set
# Through fuzzy matching, we can find the key
print(rs.keys('my*')) # Return results :['mySet', 'mySet2']
# The iteration method of each type of element
#hash Type iteration
for i in rs.hscan_iter("hName"):
print(i)
#set Type iteration
for j in rs.sscan_iter("mySet"):
print(j)
#zset Type iteration
for k in rs.zscan_iter("fruits"):
print(k)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved