python操作redis數(shù)據(jù)庫的三種方法
更新時(shí)間:2020年09月10日 09:23:07 作者:GH
這篇文章主要介紹了python操作redis數(shù)據(jù)庫的三種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
安裝依賴
pip3 install redis
使用的三種方式
直接使用
import redis r = redis.Redis(host='127.0.0.1', port=6379, db=1, password=None, decode_responses=True)
連接池使用
import redis pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=1, max_connections=100, password=None, decode_responses=True) r = redis.Redis(connection_pool=pool)
緩存使用:要額外安裝 django-redis
安裝django-redis
pip install django-redis
1.將緩存存儲(chǔ)位置配置到redis中:settings.py
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100},
"DECODE_RESPONSES": True,
"PSAAWORD": "",
}
}
}
2.操作cache模塊直接操作緩存:views.py
from django.core.cache import cache # 結(jié)合配置文件實(shí)現(xiàn)插拔式
# 存放token,可以直接設(shè)置過期時(shí)間
cache.set('token', 'header.payload.signature', 300)
# 取出token
token = cache.get('token')
以上就是python中操作redis數(shù)據(jù)庫的三種方法的詳細(xì)內(nèi)容,更多關(guān)于python中操作redis的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python讀寫修改Excel之xlrd&xlwt&xlutils
這篇文章主要介紹了python讀寫修改Excel之xlrd&xlwt&xlutils,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Python實(shí)現(xiàn)簡(jiǎn)繁體轉(zhuǎn)換
很多時(shí)候簡(jiǎn)繁體轉(zhuǎn)換,掌握了簡(jiǎn)體與繁體的轉(zhuǎn)換,往往能夠事半功倍,本文主要介紹了Python實(shí)現(xiàn)簡(jiǎn)繁體轉(zhuǎn)換,感興趣的可以了解一下2021-06-06
基于python批量處理dat文件及科學(xué)計(jì)算方法詳解
今天小編就為大家分享一篇基于python批量處理dat文件及科學(xué)計(jì)算方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05

