Ⅰ : 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)
參數釋義
| name | Redis中的Key |
| value | Redis中的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()
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()
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()
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()
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)
參數釋義
| name | Redis中的Key |
| value | Redis中的Value |
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练习操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。