# 1.安装redis与可视化操作工具

# 2.在服务中管理redis服务器的开启关闭

# 3.命令行简单使用redis:
	-- redis-cli  # 启动客户端
    -- set key value  # 设置值
    -- get key  # 取出值
    
# 4.redis支持:字符串、字典、列表、集合、有序集合
# https://www.runoob.com/redis/redis-tutorial.html

# 5.特点:可持久化、单线程单进程并发

python使用redis

依赖

>: 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)

缓存使用

# 1.将缓存存储位置配置到redis中:settings.py
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')

相关文章:

  • 2022-01-14
  • 2022-01-03
  • 2021-11-28
  • 2021-11-16
  • 2022-01-18
  • 2021-06-30
  • 2021-08-28
  • 2021-09-04
猜你喜欢
  • 2022-01-20
  • 2021-08-24
  • 2022-02-08
  • 2022-02-24
  • 2022-01-02
  • 2021-10-12
  • 2021-06-29
相关资源
相似解决方案