zoukankan      html  css  js  c++  java
  • Django 大神手把手带你上路系列 ~ redis数据库

    redis数据库

    # 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')
    
  • 相关阅读:
    如何区分JS中的this?!
    JavaScript----函数的封装、继承和多态
    正则知识点解读及常用表达式(判断有效数字、手机号邮箱等)
    Java-集合练习5
    输入输出练习
    集合练习5
    集合练习4
    集合练习题2
    Java-集合练习题1
    Java-小练习简单银行程序
  • 原文地址:https://www.cnblogs.com/bladecheng/p/11593839.html
Copyright © 2011-2022 走看看