1. 程式人生 > >python連線redis 以及redis操作

python連線redis 以及redis操作

import redis

r = redis.StrictRedis(host='127.0.0.1',port=6379,db=0,decode_responses=True,password='123456')
#r.set('name','test')
#print(r.get('name'))
#string

pipe = r.pipeline()  #減少對伺服器的請求數  #減少伺服器客戶端之間連線損耗

pipe.set('name','orange')
pipe.set('name1','apple')
pipe.set('name2','pear')
pipe.execute()
print(r.get('name1'))

puthon操作string

r = redis.StrictRedis(host='127.0.0.1',port=6379,db=0,decode_responses=True,password='123456')
'''
set key value 
SETEX key seconds value 
MSET key value [key value ...] 
GET key 
MGET key [key ...] 
INCR key 
INCRBY key increment 
DECR key 
DECRBY key decrement 
STRLEN key
APPEND key value 

python 操作hash

r.hset('namees','kangbazi','ming')

27.0.0.1:6379> hset student name 'feifei'   格式: hset  字典名 key value
(integer) 1
127.0.0.1:6379> hset student age 20   
(integer) 1
127.0.0.1:6379> hset student sex 'girl'
(integer) 1
127.0.0.1:6379> hset student height '1.75m'
(integer) 1
127.0.0.1:6379> hget student age   ——》獲取: hget 字典名 key
"20"
127.0.0.1:6379> type student   —》 判斷student的型別
hash
127.0.0.1:6379> hget student height
"1.75m"
127.0.0.1:6379> hmset book bname 'python' author 'jack' price 50 publish 'beijing'    ——》hmset 字典名 key value key value。。。
OK
127.0.0.1:6379> hget book author
"jack"
127.0.0.1:6379> hmget book author bname   ——》 hmget 字典名 key key key


127.0.0.1:6379> hkeys book   —》 獲取鍵們
1) "bname"
2) "author"
3) "price"
4) "publish"
127.0.0.1:6379> hvals book   ——》 獲取值們
1) "python"
2) "jack"
3) "50"
4) "beijing"
127.0.0.1:6379> hgetall book   —》 獲取鍵值對
1) "bname"
2) "python"
3) "author"
4) "jack"
5) "price"
6) "50"
7) "publish"
8) "beijing"
127.0.0.1:6379> hlen book   ——》獲取hash 字典長度
(integer) 4


127.0.0.1:6379> hexists book author   —》 判斷某個鍵是否存在
(integer) 1
127.0.0.1:6379> hexists book author1
(integer) 0
127.0.0.1:6379> hdel book publish   ——》 刪除某個鍵
(integer) 1
127.0.0.1:6379> hlen book
(integer) 3


python此操作list

127.0.0.1:6379> lpush myqueue 8    ——》列表的左側新增
(integer) 1
127.0.0.1:6379> lpush myqueue 9
(integer) 2
127.0.0.1:6379> lpush myqueue 7
(integer) 3
127.0.0.1:6379> lpush myqueue 3
(integer) 4
127.0.0.1:6379> lpush myqueue 5
(integer) 5
127.0.0.1:6379> lpush myqueue 6
(integer) 6
127.0.0.1:6379> lrange myqueue 0 -1   ——》 檢視列表的資料   lrange 列表名 0 -1     0 表示開頭   -1 表示的是結尾
1) "6"
2) "5"
3) "3"
4) "7"
5) "9"
6) "8"
127.0.0.1:6379> rpush muqueue 'good'    ——》右側新增元素
(integer) 1
127.0.0.1:6379> rpush muqueue 'happy'
(integer) 2
127.0.0.1:6379> rpush muqueue 'hello'
(integer) 3
127.0.0.1:6379> rpush muqueue 'world'
(integer) 4
127.0.0.1:6379> lrange myqueue 0 -1
1) "6"
2) "5"
3) "3"
4) "7"
5) "9"
6) "8"
127.0.0.1:6379> lrange muqueue 0 -1
1) "good"
2) "happy"
3) "hello"
4) "world"
127.0.0.1:6379> lpop myqueue    ——》 左側彈出元素,返回值就是彈出元素
"6"
127.0.0.1:6379> rpop myqueue
"8"
127.0.0.1:6379> lrange myqueue 0 -1
1) "5"
2) "3"
3) "7"
4) "9"
127.0.0.1:6379> linsert myqueue before 7 100   ——》指定位置新增元素   結合before after   7參照元素   100要新增的值
(integer) 5
127.0.0.1:6379> lrange myqueue 0 -1
1) "5"
2) "3"
3) "100"
4) "7"
5) "9"
127.0.0.1:6379> linsert myqueue after 7 88
(integer) 6
127.0.0.1:6379> lrange myqueue 0 -1
1) "5"
2) "3"
3) "100"
4) "7"
5) "88"
6) "9"

127.0.0.1:6379> lindex muqueue 1   ——》1 就是下標位置   根據位置獲取對應的元素
"happy"
127.0.0.1:6379> lrange muqueue 0 -1
1) "good"
2) "happy"
3) "hello"
4) "world"
127.0.0.1:6379> lset muqueue 1 'haha'    ——》 指定位置修改元素   1 位置   ‘haha’ 新的value值
OK
127.0.0.1:6379> lrange muqueue 0 -1
1) "good"
2) "haha"
3) "hello"
4) "world"
127.0.0.1:6379> 


分頁:
127.0.0.1:6379> lrange myqueue 0 -1   
1) "5"
2) "3"
3) "100"
4) "7"
5) "88"
6) "9"
127.0.0.1:6379> lrange myqueue 0 1   指定下標獲取元素  包前start  —0     包後end—1
1) "5"
2) "3"
127.0.0.1:6379> lrange myqueue 2 3
1) "100"
2) "7"
127.0.0.1:6379> lrange myqueue 4 6
1) "88"
2) "9"
127.0.0.1:6379> lrange myqueue 1 5
1) "3"
2) "100"
3) "7"
4) "88"
5) "9"

127.0.0.1:6379> ltrim myqueue 0 3    ——》 截斷list列表    
OK
127.0.0.1:6379> lrange myqueue 0 -1
1) "5"
2) "3"
3) "100"
4) "7"     ——》 只剩下  5 3  100  7  



python 操作set

set = {1,’1345688’}

127.0.0.1:6379> sadd set1 1,4,7,8,1,4,8,8,8,8,1    —>set1 表示集合名字   後面的就是元素(整體元素)
(integer) 1
127.0.0.1:6379> sadd set1 1 4 7 8 8   —》 一次新增多個元素
(integer) 3
127.0.0.1:6379> smembers set1   —》檢視元素
1) "7"
2) "1"
3) "1,4,7,8,1,4,8,8,8,8,1"
4) "4"
5) "8"
127.0.0.1:6379> smembers set1
1) "1,4,7,8,1,4,8,8,8,8,1"
127.0.0.1:6379> sadd set1 1
(integer) 1
127.0.0.1:6379> smembers set1
1) "1"
2) "1,4,7,8,1,4,8,8,8,8,1"
127.0.0.1:6379> sadd set1 1
(integer) 0
127.0.0.1:6379> smembers set1
1) "1"
2) "1,4,7,8,1,4,8,8,8,8,1"
127.0.0.1:6379> sadd set1 1,4,7,8,1,4,8,8,8,8,1
(integer) 0
127.0.0.1:6379> sismember set1 1    —》 判斷元素1 是否在set1集合中  返回值1  在   返回0 不在
(integer) 1
127.0.0.1:6379> sismember set1 6
(integer) 0
127.0.0.1:6379> 

127.0.0.1:6379> scard set1    —》獲取集合長度
(integer) 5

127.0.0.1:6379> sinter set1 set2   —》交集
1) "1"
2) "4"
127.0.0.1:6379> sdiff set1 set2   —》差集
1) "8"
2) "7"
3) "1,4,7,8,1,4,8,8,8,8,1"
127.0.0.1:6379> sunion set1 set2  —》並集
1) "4"
2) "8"
3) "1,4,7,8,1,4,8,8,8,8,1"
4) "1"
5) "5"
6) "7"


python 操作zset

127.0.0.1:6379> zadd zset8 1 ‘a’ 2 ‘b’ …    —> 向zset8中新增權重和對應的元素
1) "a"
2) "c"
3) "b"
4) "y"
5) "f"
127.0.0.1:6379> zadd zset8 1 'k'   ——》 新增權重值是1 的元素‘k’  到zset8中
(integer) 1
127.0.0.1:6379> zrange zset8 0 -1
1) "a"
2) "c"
3) "k"
4) "b"
5) "y"
6) "f"
127.0.0.1:6379> zcount zset8 1 2   ——》 檢視權重值在1,2之間的元素有多少
(integer) 5
127.0.0.1:6379> zscore zset8 k   找‘k’對應的權重
"1"


127.0.0.1:6379> zadd zset3 1 'ee'  1 'uu'    
(integer) 2
127.0.0.1:6379> expire zset3 3   —》設定zset3的過期時間時3秒
(integer) 1
127.0.0.1:6379> zcard zest
(integer) 0
127.0.0.1:6379> ttl zset8   ——》 檢視zset8的過期時間   如果是-1 沒有限制
(integer) -1