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

What kind of spark will Python and redis spark?

編輯:Python

Redis type

Redis There are five basic types :

  • character string

  • hash

  • list

  • aggregate

  • Ordered set

Each different type ,Redis The client provides many different operation methods , The following will demonstrate some of the most commonly used based on Python The operation of .

Python Use pip install Redis

pip install redis

The source code to share

# coding:utf-8
import redis
r = redis.Redis(host='127.0.0.1', port=6379, password='')  # Instantiate a redis The object comes out
def redisOperation():
    **1、 String type :--Redis  character string (String)**
    #redis  Commands related to string data types are used for management  redis  A string value
    r.set("name","tony") # Set the specified  key  Value
    print(r.get("name")) # Get specified  key  Value
    r.incr("count") #  take  key  The value of the number stored in is increased by one
    print(r.get("count"))
    r.decr("count") #  take  key  Subtract one from the number stored in
    print(r.get("count"))
    r.delete("name") #  Delete the specified  key  Value
    **2、 Hash type :--Redis Hash (Hash)**
    # Redis hash It's a string Type of field and value Mapping table ,hash Ideal for storing objects .
    r.hset("monut", "high", 2000) #  Hash table  key  In the field  field  The value of the set  value
    r.hsetnx("mount","name","tony") #  Assign value only when the field does not exist
    r.hexists("mount","high") #  Determine whether the key value exists
    r.exists("monut") #  Judge whether the key exists
    r.hkeys("monut") #  Get all the fields in the hash table
    r.hvals("monut") #  Get the values in all hash tables
    r.hlen("monut") #  Get the number of fields in the hash table
    r.hdel("mount", "high")  #  Delete one or more hash table fields
    print("the queue is:", r.hget('ASYNCHRONOUS_CALLBACK_QUEUE', 'myname')) # Gets the value of the specified field stored in the hash table
    print("the hash is:",r.hgetall('ASYNCHRONOUS_CALLBACK_HASH')) #  Gets the specified in the hash table  key  All fields and values of
    **3、 List the type :--Redis list (List)**
    # Redis List is a simple list of strings , Sort by insertion order . You can add an element to the head of the list ( On the left ) Or tail ( On the right )
    r.lpush("list1","ok") # LPUSH  Insert a value named  list1  In the list of
    r.lpop("list1") #  Move out and get the first element of the list
    r.lrange("list1",1,4) #  Get the elements in the specified range of the list
    r.llen("list1")  #  Get list length
    r.rpop("list1") #  Remove the last element of the list , The return value is the removed element
    r.rpush("list1",1000) # RPUSH  Insert a value named  list1  In the list of
    **4、 Collection types :--Redis aggregate (Set)**
    # Redis Of Set yes String Unordered collection of type . Collection members are unique , This means that duplicate data cannot appear in the collection .
    #Redis The middle set is implemented through a hash table , So add the , Delete , The complexity of searching is O(1).
    r.sadd("set1",100,188) #  Add one or more members to the collection
    r.sismember("set1",100) #  Judge  member  Is the element a collection  key  Members of
    r.srem("set1",188) #  Remove one or more members of the collection
    r.smembers("set1") #  Returns all members of the collection
    r.scard("set1") #  Get the number of members of the collection
    **5、 Ordered set type :--Redis  Ordered set (sorted set)**
    ##Redis An ordered set is the same as a set string Collection of type elements , And duplicate members are not allowed .
    # The difference is that each element is associated with a double Score of type .redis It's the scores that sort the members of a collection from small to large .
    # Members of an ordered set are unique , But fractions (score) But it can be repeated .
    # Collections are implemented through hash tables , So add the , Delete , The complexity of searching is O(1).
    r.zadd("sortset",100,"mysql") #  Add one or more members... To an ordered collection , Or update scores of existing members
    r.zcard("sortset") #  Get the number of members of the ordered set
    r.zrem("sortset","mysql") #  Remove one or more members of an ordered collection
    r.zcount("sortset",20,100) #  Calculates the number of members of a specified interval fraction in an ordered set
    r.zrank("sortset","mysql") #  Returns the index of a specified member in an ordered collection
    r.zscore("sortset","mysql") #  Back to the ordered set , The score of a member
    r.zrangebyscore("sortset",10,100) #  Returns an ordered set through an index interval to synthesize members in a specified interval
    r.dbsize()# Returns the... Of the current database  key  The number of
if __name__=="__main__":
    redisOperation()

Source code description

1. The above source code is Python Based on a variety of Redis Operation method of data type , It is used to obtain Redis Key value specified in , along with it It supports modifying the retrieved value again The operation of .

2. In the actual testing work, we often encounter queries Redis Database data requirements , With the above operation methods , It can facilitate the work of Redis Fast query and acquisition of data .

Welcome to your attention 【 The way of immeasurable testing 】 official account , reply 【 Claim resources 】

Python+Unittest frame API automation 、

Python+Unittest frame API automation 、

Python+Pytest frame API automation 、

Python+Pandas+Pyecharts Big data analysis 、

Python+Selenium frame Web Of UI automation 、

Python+Appium frame APP Of UI automation 、

Python Programming learning resources dry goods 、

Vue Front end component framework development 、

Resources and code Free ~
Below the official account is two-dimensional code. , You can directly scan wechat and pay attention .

remarks : My official account has been officially opened. , betake IT Sharing of Internet technology .

contain : Data analysis 、 big data 、 machine learning 、 Test Development 、API Interface automation 、 Test operation and maintenance 、UI automation 、 Performance testing 、 code detection 、 Programming technology, etc .

WeChat search official account :“ The way of immeasurable testing ”, Or scan the qr code below :

   

  Add the attention , Let's grow together !


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