redis數據庫的使用

一.安裝redis與可視化操做工具

可視化工具:RedisDesktopManagerhtml

redis載地址:https://github.com/MSOpenTech/redis/releasespython

二.在服務中管理redis服務器的開啓關閉

redis-server.exe redis.windows.conf
redis-cli.exe -h 127.0.0.1 -p 6379

三.命令行簡單使用

redis-cli  # 啓動客戶端
set key value  # 設置值
get key  # 取出值

四.redis支持

字符串、字典、列表、集合、有序集合git

https://www.runoob.com/redis/redis-tutorial.htmlgithub

五.特色

可持久化、單線程單進程併發redis

六.python中使用

依賴

pip3 install redis

直接使用

import redis
r = redis.Redis(host='127.0.0.1', port=6379)

鏈接池使用

import redis
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
r = redis.Redis(connection_pool=pool)

庫的選擇

import redis
r = redis.Redis(db=0) #第幾個庫總共有15個庫

七.django中配置與CACHES聯用

緩存使用

# 1.將緩存存儲位置配置到redis中:settings.py
#首先要安裝依賴pip install django-redis。
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}
        }
    }
}

# 2.操做cache模塊直接操做緩存:views.py
from django.core.cache import cache  # 結合配置文件實現插拔式
# 存放token,能夠直接設置過時時間
cache.set('token', 'header.payload.signature', 10)
# 取出token
token = cache.get('token')
相關文章
相關標籤/搜索