Luogu P1908 逆序對
阿新 • • 發佈:2020-07-26
pip3 install redis
1 Python操作Redis之普通連線
from redis import Redis
conn=Redis(host='127.0.0.1', port=6379)
2 Python操作Redis之連線池
注意:pool必須是單例,因為將定義連線池的檔案當模組匯入,只會執行一遍,產生一個連線池,同一個id
包內的py檔案,如果想右鍵執行這個檔案,導包的時候不能帶點
# 連線池 import redis POOL = redis.ConnectionPool(host='127.0.0.1', port=6379,max_connections=100) # 匯入連線池,並從連線池獲得一個連線 import redis from t_redis_pool import POOL conn = redis.Redis(connection_pool=POOL)
3 操作之String操作
conn.set('height',180) conn.set('height','190',nx=True) conn.set('height','190',nx=True) conn.set('height1','190',xx=True) ex,過期時間(秒) px,過期時間(毫秒) nx,如果設定為True,name不存在,執行set操作 xx,如果設定為True,name存在,執行set操作,設定值 setnx(name, value) setex(name, value, time) time 過期時間(秒 或 timedelta物件) psetex(name, time_ms, value) time_ms,過期時間(數字毫秒 或 timedelta物件 conn.mset({'name1':'11','name3':'dasfd'}) ret=conn.mget(['name1','name','name3']) ret=conn.getset('name1', '999') ret=conn.getrange('name1',0,0) # 前閉後閉區間 conn.setrange('name1',1,88888) ret=conn.getbit('name1',9) # incr :統計網站訪問量,頁面訪問量,介面訪問量 conn.incr('name1') # 只要一執行,數字加1 conn.incr('name1',-2) # 設定負數,執行一次減少 # 減少 conn.decr('name1',3) conn.append('name1','oo')
4 操作之Hash操作
conn.hset('hash1','name','lqz') # key不可以重複 ret=conn.hget('hash1','name') # 只能取一個 conn.hmset('hash2',{'key1':'value1','key2':'value2'}) ret=conn.hmget('hash1','name','name2') ret=conn.hmget('hash1',['name','name2']) ret=conn.hgetall('hash1') # 儘量少用,資料全部取出,記憶體,,, 取出hash型別內所有的資料 生成器 ret=conn.hscan_iter('hash1') ret=conn.hlen('hash1') ret=conn.hkeys('hash1') ret=conn.hexists('hash1','name1') ret=conn.hdel('hash1','name') 自增 ret=conn.hincrby('hash1','name')
5 操作之List操作
ret=conn.lpush('list1',1,2,3,4,5)
ret=conn.rpush('list1',999)
ret=conn.lpushx('list2',1)
ret=conn.lpushx('list1',888) # 必須有這個key才能放
ret=conn.rpushx('list1',666)
ret=conn.llen('list1')
ret=conn.linsert('list1','before','3','77777777')
ret=conn.linsert('list1','after','3','66666666')
ret=conn.lset('list1',3,'22222') #從0開始計數
ret=conn.lset('list1',0,'11111')
ret=conn.lrem('list1',2,'5') # 從前往後刪除兩個5
ret=conn.lrem('list1',-1,'5') # 從後往前刪除1個5
ret=conn.lrem('list1',0,'5') # 刪除所有5
ret=conn.lpop('list1')
ret=conn.rpop('list1')
ret=conn.lindex('list1',0)
ret=conn.lrange('list1',0,2) # 前閉後閉
ret=conn.ltrim('list1',1,2)
重點block,阻塞,可以寫一個超時時間
ret=conn.blpop('list1',timeout=10)
conn.flushall()
def scan_list(name,count=2):
index=0
while True:
data_list=conn.lrange(name,index,count+index-1)
if not data_list:
return
index+=count
for item in data_list:
yield item
5 redsi的其他使用
conn.delete('list1')
ret=conn.delete('hash1')
ret=conn.exists('hash2')
ret=conn.keys('cache*') #查詢以cache開頭的所有key
ret=conn.expire('hash2',2) # 設定過期時間
ret=conn.type('name3')
6 管道
# redis支援事務
# 管道:實現事務
import redis
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
conn = redis.Redis(connection_pool=pool)
pipe = r.pipeline(transaction=False)
pipe = conn.pipeline(transaction=True)
pipe.multi()
pipe.set('name', 'alex')
pipe.set('role', 'sb')
pipe.execute() # 這句話,才真正的去執行
7 Django中使用redis
# 方式一:用原來的redis操作,存取
# 方式二:django-redis
pip install django-redis
etting中配置
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100}
# "PASSWORD": "123",
}
}
}
使用cache
from django.core.cache import cache
cache.set('name',user)
8 介面快取
def list(self, request, *args, **kwargs):
# 先去快取拿資料
banner_list=cache.get('banner_list')
if not banner_list:
print('走資料庫了')
# 快取中沒有,去資料庫拿
response = super().list(request, *args, **kwargs)
# 加到快取
cache.set('banner_list',response.data,60*60*24)
return response
return Response(data=banner_list)