日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

和get redis_Redis练习操作

發布時間:2025/3/21 数据库 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 和get redis_Redis练习操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Ⅰ : String 字符串

String類型是Redis最基本的數據類型,一個鍵最大能存儲512MB

String數據結構是最簡單的key-value類型,value不僅可以是String,也可以是數字,是包含多種類型的特殊結構類型

String類型是二進制安全的

意思是Redis可以包含任何數據:比如序列化的對象進行存儲,比如一張圖片進行二進制存儲,再比如一個簡單的字符串,數值等等

set - 在 Redis 中設置值:不存在 則創建,存在 則修改

語法

set(self, name, value, ex=None, px=None, nx=False, xx=False, keepttl=False)參數釋義
nameRedis中的Key
valueRedis中的Value
ex過期時間(秒)
px過期時間(毫秒)
nx如果設置為True,則只有name不存在時,當前set操作才執行
xx如果設置為True,則只有name存在時,當前set操作才執行
keepttl保留與密鑰相關聯的生存時間(僅在Redis6.0及以后生效)
  • 普通使用 - name不存在 則創建,存在 則修改
from redis import Redisconn = Redis(host='127.0.0.1', password='qwer1234', port=6379)# 存1個字符串:Key是Fruit1,Value是Lemon conn.set('Fruit1', 'Lemon') res1 = conn.get('Fruit1') print(res1) # b'Lemon'# 存1個字符串:Key是Fruit1,Value是Peanut conn.set('Fruit1', 'Peanut') res1 = conn.get('Fruit1') print(res1) # b'Peanut'conn.close()
  • ex - 設置過期時間(s)
from redis import Redis import timeconn = Redis(host='127.0.0.1', password='qwer1234', port=6379)# 存1個字符串:Key是Fruit2,Value是Apple,過期時間是3秒 conn.set('Fruit2', 'Apple', ex=3) res1 = conn.get('Fruit2') print(res1) # b'Apple'time.sleep(4) res2 = conn.get('Fruit2') print(res2) # Noneconn.close()
  • px - 設置過期時間(ms)
from redis import Redis import timeconn = Redis(host='127.0.0.1', password='123456', port=6379)# 存1個字符串:Key是Fruit3,Value是Orange,過期時間是1700毫秒(1.7秒) conn.set('Fruit3', 'Orange', px=1700) res1 = conn.get('Fruit3') print(res1) # b'Orange'time.sleep(2) res2 = conn.get('Fruit3') print(res2) # Noneconn.close()
  • nx - name不存在時才生效
from redis import Redisconn = Redis(host='127.0.0.1', password='qwer1234', port=6379)# 存1個字符串:Key是Fruit4,Value是Banana,name不存在時才生效(此時name不存在) conn.set('Fruit4', 'Banana', nx=True) res1 = conn.get('Fruit4') print(res1) # b'Banana'# 存1個字符串:Key是Fruit4,Value是Banana,name不存在時才生效(此時name已存在) conn.set('Fruit4', 'Grape', nx=True) res2 = conn.get('Fruit4') print(res2) # b'Banana'conn.close()
  • xx - name存在時才生效
from redis import Redisconn = Redis(host='127.0.0.1', password='qwer1234', port=6379)# 存1個字符串:Key是Fruit5,Value是Pear,name存在時才生效(此時name不存在) conn.set('Fruit5', 'Pear', xx=True) res1 = conn.get('Fruit5') print(res1) # None# 存1個字符串:Key是Fruit5,Value是Watermelon conn.set('Fruit5', 'Watermelon') res2 = conn.get('Fruit5') print(res2) # b'Watermelon'# 存1個字符串:Key是Fruit5,Value是Pineapple,name存在時才生效(此時name已存在) conn.set('Fruit5', 'Pineapple', xx=True) res3 = conn.get('Fruit5') print(res3) # b'Pineapple'# 存1個字符串:Key是Fruit5,Value是Strawberry,name存在時才生效(此時name已存在) conn.set('Fruit5', 'Strawberry', xx=True) res4 = conn.get('Fruit5') print(res4) # b'Strawberry'conn.close()

setnx - name不存在時才生效

  • 語法
setnx(self, name, value) # 相當于:set(key, value, nx=True)
  • 參數
參數釋義
nameRedis中的Key
valueRedis中的Value
  • name不存在時才生效
from redis import Redisconn = Redis(host='127.0.0.1', password='qwer1234', port=6379)# 存1個字符串:Key是Fruit5,Value是Olive ,name不存在時才生效(此時name不存在) conn.setnx('Fruit6', 'Olive') res1 = conn.get('Fruit6') print(res1) # b'Olive'# 存1個字符串:Key是Fruit5,Value是Berry ,name不存在時才生效(此時name已存在) conn.setnx('Fruit6', 'Berry') res2 = conn.get('Fruit6') print(res2) # b'Olive'conn.close() 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的和get redis_Redis练习操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。