python操作redis數(shù)據(jù)庫的三種方法
安裝依賴
pip3 install redis
使用的三種方式
直接使用
import redisr = redis.Redis(host=’127.0.0.1’, port=6379, db=1, password=None, decode_responses=True)
連接池使用
import redispool = 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)# 取出tokentoken = cache.get(’token’)
以上就是python中操作redis數(shù)據(jù)庫的三種方法的詳細內(nèi)容,更多關(guān)于python中操作redis的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python裝飾器三種裝飾模式的簡單分析2. 詳解java中static關(guān)鍵詞的作用3. Python如何進行時間處理4. 詳解Python模塊化編程與裝飾器5. Java14發(fā)布了,再也不怕NullPointerException了6. PHP擴展之字符編碼相關(guān)函數(shù)1——iconv7. python使用ctypes庫調(diào)用DLL動態(tài)鏈接庫8. 關(guān)于Java下奇怪的Base64詳解9. Python實現(xiàn)迪杰斯特拉算法過程解析10. PHP swoole的process模塊創(chuàng)建和使用子進程操作示例
