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.將緩存存儲(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)# 取出tokentoken = cache.get(’token’)
以上就是python中操作redis數(shù)據(jù)庫的三種方法的詳細(xì)內(nèi)容,更多關(guān)于python中操作redis的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Android table布局開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器2. IntelliJ IDEA安裝插件的方法步驟3. 理解PHP5中static和const關(guān)鍵字4. php模擬實(shí)現(xiàn)斗地主發(fā)牌5. spring acegi security 1.0.0 發(fā)布6. MyBatis中的JdbcType映射使用詳解7. vue 使用localstorage實(shí)現(xiàn)面包屑的操作8. Python random庫使用方法及異常處理方案9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備