欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?redis模塊的使用教程指南

 更新時(shí)間:2022年10月21日 09:28:07   作者:世界盡頭與你  
這篇文章主要為大家詳細(xì)介紹了Python?redis模塊的使用教程指南的相關(guān)資料,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧

1.安裝模塊

Python 要使用 redis,需要先安裝 redis 模塊:

pip install redis

測試安裝:

redis 取出的結(jié)果默認(rèn)是字節(jié),我們可以設(shè)定 decode_responses=True 改成字符串

r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.set('name', 'dahezhiquan')  # 設(shè)置 name 對(duì)應(yīng)的值
print(r['name'])  # dahezhiquan
print(r.get('name'))  # dahezhiquan
print(type(r.get('name')))  # <class 'str'>

2.連接池

redis-py 使用 connection pool 來管理對(duì)一個(gè) redis server 的所有連接,避免每次建立、釋放連接的開銷。

默認(rèn),每個(gè)Redis實(shí)例都會(huì)維護(hù)一個(gè)自己的連接池??梢灾苯咏⒁粋€(gè)連接池,然后作為參數(shù) Redis,這樣就可以實(shí)現(xiàn)多個(gè) Redis 實(shí)例共享一個(gè)連接池。

redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.set('name', 'dahe')
print(r.get('name'))  # dahe

3.redis 基本命令 String

set,在 Redis 中設(shè)置值,默認(rèn),不存在則創(chuàng)建,存在則修改:

語法:

set(name, value, ex=None, px=None, nx=False, xx=False)

參數(shù):

  • ex - 過期時(shí)間(秒)
  • px - 過期時(shí)間(毫秒)
  • nx - 如果設(shè)置為True,則只有name不存在時(shí),當(dāng)前set操作才執(zhí)行
  • xx - 如果設(shè)置為True,則只有name存在時(shí),當(dāng)前set操作才執(zhí)行

案例1:(3秒后,name的值就會(huì)變?yōu)镹one)

redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.set('name', 'xiaoqian', ex=3)  # 設(shè)置過期時(shí)間為3秒
print(r.get('name'))  # xiaoqian

三秒后再次獲取name的值:

print(r.get('name'))  # None

mset,批量獲取值:

redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.set("name1", "xiaoqian")
r.set("name2", "xiaoguo")
print(r.mget('name1', 'name2'))  # ['xiaoqian', 'xiaoguo']

getset(name, value),設(shè)置新值并獲取原來的值:

redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
print(r.getset("name1", "heihei"))  # xiaoqian
print(r.get("name1"))  # heihei

strlen(name),返回name對(duì)應(yīng)值的字節(jié)長度(一個(gè)漢字3個(gè)字節(jié)):

print(r.strlen("name"))

incr(name, amount=1),自增 name 對(duì)應(yīng)的值,當(dāng) name 不存在時(shí),則創(chuàng)建 name=amount,否則,則自增:

參數(shù):

  • name - Redis的name
  • amount - 自增數(shù)(必須是整數(shù))
redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.set("like", 521)
r.incr("like", amount=1)
print(r.get("like"))  # 522

可以使用incrbyfloat方法自增浮點(diǎn)數(shù)類型

使用decr進(jìn)行自減操作

append(key, value),在redis name對(duì)應(yīng)的值后面追加內(nèi)容:

參數(shù):

  • key - redis的name
  • value - 要追加的字符串
r.append("name1", "world")
print(r.get("name1"))

4.redis 基本命令 hash

hset(name, key, value),單個(gè)增加–修改:

name對(duì)應(yīng)的hash中設(shè)置一個(gè)鍵值對(duì)(不存在,則創(chuàng)建;否則,修改)

參數(shù):

  • name - redis的name
  • key - name對(duì)應(yīng)的hash中的key
  • value - name對(duì)應(yīng)的hash中的value
redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.hset("dahe", "name", "guo")
r.hset("dahe", "age", 28)
# 獲取dahe的所有key
print(r.hkeys("dahe"))  # ['name', 'age']
print(r.hget("dahe", "name"))  # guo
print(r.hmget("dahe", "name", "age"))  # ['guo', '28']

hmset(name, mapping),在name對(duì)應(yīng)的hash中批量設(shè)置鍵值對(duì):

redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.hmset("hash1", {"k1": "v1", "k2": "v2", "k3": "v3"})
print(r.hmget("hash1", "k1", "k2", "k3"))  # ['v1', 'v2', 'v3']

hgetall(name),取出所有的鍵值對(duì):

print(r.hgetall("dahe"))  # {'name': 'guo', 'age': '28'}

hvals(name),得到所有的value:

print(r.hvals("dahe"))  # ['guo', '28']

hdel(name,*keys),將name對(duì)應(yīng)的hash中指定key的鍵值對(duì)刪除:

r.hdel("hash1", "k1")
print(r.hgetall("hash1"))  # {'k2': 'v2', 'k3': 'v3'}

hincrby(name, key, amount=1),自增自減整數(shù):

參數(shù):

name - redis中的name

key - hash對(duì)應(yīng)的key

amount - 自增數(shù)(整數(shù),負(fù)數(shù)表示自減)

hincrbyfloat(name, key, amount=1.0)表示自增自減浮點(diǎn)數(shù)

5.redis基本命令 list

lpush(name,values),增加:

在name對(duì)應(yīng)的list中添加元素,每個(gè)新的元素都添加到列表的最左邊

redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.lpush("score", 10, 20, 30, 40)
print(r.lrange("score", 0, -1))  # ['40', '30', '20', '10']

rpush表示從右邊增加

r.lset(name, index, value),對(duì)name對(duì)應(yīng)的list中的某一個(gè)索引位置重新賦值:

參數(shù):

  • name - redis的name
  • index - list的索引位置
  • value - 要設(shè)置的值
redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.lset("score", 0, 521)
print(r.lrange("score", 0, 4))  # ['521', '30', '20', '10', '40']

r.lrem(name, value, num),刪除:

參數(shù):

name - redis的name

value - 要?jiǎng)h除的值

num - num=0,刪除列表中所有的指定值;

  • num=2 - 從前到后,刪除2個(gè), num=1,從前到后,刪除左邊第1個(gè)
  • num=-2 - 從后向前,刪除2個(gè)
r.lrem("list2", "11", 1)    # 將列表中左邊第一次出現(xiàn)的"11"刪除
print(r.lrange("list2", 0, -1))
r.lrem("list2", "99", -1)    # 將列表中右邊第一次出現(xiàn)的"99"刪除
print(r.lrange("list2", 0, -1))
r.lrem("list2", "22", 0)    # 將列表中所有的"22"刪除
print(r.lrange("list2", 0, -1))

lindex(name, index),取值:

print(r.lindex("list2", 0))  # 取出索引號(hào)是0的值

自定義增量迭代:

由于redis類庫中沒有提供對(duì)列表元素的增量迭代,如果想要循環(huán)name對(duì)應(yīng)的列表的所有元素,那么就需要獲取name對(duì)應(yīng)的所有列表。

但是,如果列表非常大,那么就有可能在第一步時(shí)就將程序的內(nèi)容撐爆,所有有必要自定義一個(gè)增量迭代的功能:

def list_iter(name):
    """
    自定義redis列表增量迭代
    :param name: redis中的name,即:迭代name對(duì)應(yīng)的列表
    :return: yield 返回 列表元素
    """
    list_count = r.llen(name)
    for index in range(list_count):
        yield r.lindex(name, index)

# 使用
for item in list_iter('list2'): # 遍歷這個(gè)列表
    print(item)

6.redis基本命令 set

sadd(name,values),新增:

pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(connection_pool=pool)
r.sadd("set1", 33, 44, 55, 66)  # 往集合中添加元素
print(r.scard("set1"))  # 4(集合長度)
print(r.smembers("set1"))  # {'66', '44', '55', '33'}(取出集合所有元素)

srem(name, values),在name對(duì)應(yīng)的集合中刪除某些值:

r.srem("set1", 66)
print(r.smembers("set1"))  # {'44', '33', '55'}

python-redis set支持集合的所有操作,請(qǐng)參考官方文檔

7.其他常用操作

delete(*names),刪除:

根據(jù)刪除redis中的任意數(shù)據(jù)類型(string、hash、list、set、有序set)

pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(connection_pool=pool)
r.delete("set2")

expire(name ,time),為某個(gè)redis的某個(gè)name設(shè)置超時(shí)時(shí)間:

r.expire("set1", time=3)

rename(src, dst),重命名:

r.rename("dahe", "dahe-1")

8.管道

redis默認(rèn)在執(zhí)行每次請(qǐng)求都會(huì)創(chuàng)建(連接池申請(qǐng)連接)和斷開(歸還連接池)一次連接操作,如果想要在一次請(qǐng)求中指定多個(gè)命令,則可以使用pipline實(shí)現(xiàn)一次請(qǐng)求指定多個(gè)命令,并且默認(rèn)情況下一次pipline是原子性操作

實(shí)例:

pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(connection_pool=pool)
pipe = r.pipeline()  # 創(chuàng)建一個(gè)管道
pipe.set('name', 'jack')
pipe.set('role', 'sb')
pipe.sadd('age', '18')
pipe.execute()

以上就是Python redis模塊的使用教程指南的詳細(xì)內(nèi)容,更多關(guān)于Python redis模塊的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 關(guān)于Python中Math庫的使用

    關(guān)于Python中Math庫的使用

    這篇文章主要介紹了關(guān)于Python中Math庫的使用,math?庫是?Python?提供的內(nèi)置數(shù)學(xué)類函數(shù)庫,因?yàn)閺?fù)數(shù)類型常用于科學(xué)計(jì)算,需要的朋友可以參考下
    2023-04-04
  • 使用Matplotlib繪制不同顏色的帶箭頭的線實(shí)例

    使用Matplotlib繪制不同顏色的帶箭頭的線實(shí)例

    這篇文章主要介紹了使用Matplotlib繪制不同顏色的帶箭頭的線實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • 關(guān)于python tushare Tkinter構(gòu)建的簡單股票可視化查詢系統(tǒng)(Beta v0.13)

    關(guān)于python tushare Tkinter構(gòu)建的簡單股票可視化查詢系統(tǒng)(Beta v0.13)

    這篇文章主要介紹了python tushare Tkinter構(gòu)建的簡單股票可視化查詢系統(tǒng)(Beta v0.13),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • python解決字典中的值是列表問題的方法

    python解決字典中的值是列表問題的方法

    這篇文章主要介紹了字典中的值是列表問題,先用value連成一個(gè)str,最后用str.split()作一個(gè)轉(zhuǎn)換,生成一個(gè)列表.看了python cookbook,上面正好有一個(gè)recipe講到如何處理這樣的問題
    2013-03-03
  • Python實(shí)現(xiàn)七大查找算法的示例代碼

    Python實(shí)現(xiàn)七大查找算法的示例代碼

    這篇文章主要介紹了Python實(shí)現(xiàn)七大查找算法的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Python編寫可視化界面的詳細(xì)教程(Python+PyCharm+PyQt)

    Python編寫可視化界面的詳細(xì)教程(Python+PyCharm+PyQt)

    最近開始學(xué)習(xí)Python,但只限于看理論,編幾行代碼,覺得沒有意思,就想能不能用Python編寫可視化的界面,遂查找了相關(guān)資料,發(fā)現(xiàn)了PyQt,所以本文介紹了Python+PyCharm+PyQt編寫可視化界面的詳細(xì)教程,需要的朋友可以參考下
    2024-07-07
  • Python代碼集pathlib應(yīng)用之獲取指定目錄下的所有文件

    Python代碼集pathlib應(yīng)用之獲取指定目錄下的所有文件

    這篇文章主要介紹了Python代碼集pathlib應(yīng)用之獲取指定目錄下的所有文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • 20行Python代碼實(shí)現(xiàn)視頻字符化功能

    20行Python代碼實(shí)現(xiàn)視頻字符化功能

    這篇文章主要介紹了20行Python代碼實(shí)現(xiàn)視頻字符化功能,本文通過實(shí)例代碼截圖的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • python 多線程與多進(jìn)程效率測試

    python 多線程與多進(jìn)程效率測試

    這篇文章主要介紹了python 多線程與多進(jìn)程效率測試,在Python中,計(jì)算密集型任務(wù)適用于多進(jìn)程,IO密集型任務(wù)適用于多線程、接下來看看文章得實(shí)例吧,需要的朋友可以參考一下喲
    2021-10-10
  • Python+Turtle繪制航海王草帽路飛詳解

    Python+Turtle繪制航海王草帽路飛詳解

    turtle庫是一個(gè)點(diǎn)線面的簡單圖像庫,在Python2.6之后被引入進(jìn)來,能夠完成一些比較簡單的幾何圖像可視化。本文將利用turtle繪制一個(gè)可愛的草帽路飛,感興趣的可以試一試
    2022-03-03

最新評(píng)論