python操作redis數(shù)據(jù)庫的三種方法
更新時間: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.將緩存存儲位置配置到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é)合配置文件實現(xiàn)插拔式
# 存放token,可以直接設(shè)置過期時間
cache.set('token', 'header.payload.signature', 300)
# 取出token
token = cache.get('token')
以上就是python中操作redis數(shù)據(jù)庫的三種方法的詳細內(nèi)容,更多關(guān)于python中操作redis的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python讀寫修改Excel之xlrd&xlwt&xlutils
這篇文章主要介紹了python讀寫修改Excel之xlrd&xlwt&xlutils,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03

