加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

redis数据库的使用

发布时间:2020-12-15 17:10:37 所属栏目:大数据 来源:网络整理
导读:一.安装redis与可视化操作工具 可视化工具:RedisDesktopManager redis载地址:https://github.com/MSOpenTech/redis/releases。 二.在服务中管理redis服务器的开启关闭 redis-server.exe redis.windows.conf redis-cli.exe -h 127.0.0.1 -p 6379 三.命令行简

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

可视化工具:RedisDesktopManager

redis载地址:https://github.com/MSOpenTech/redis/releases。

二.在服务中管理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支持

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

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

五.特点

可持久化、单线程单进程并发

六.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')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读