zoukankan      html  css  js  c++  java
  • 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)
    #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')
    
  • 相关阅读:
    当Django模型迁移时,报No migrations to apply 问题时
    django--各个文件的含义
    django--创建项目
    1013. Battle Over Cities (25)
    1011. World Cup Betting (20)
    1009. Product of Polynomials (25)
    1007. Maximum Subsequence Sum (25)
    1006. Sign In and Sign Out (25)
    1008. Elevator (20)
    1004. Counting Leaves (30)
  • 原文地址:https://www.cnblogs.com/luowenConnor/p/11426713.html
Copyright © 2011-2022 走看看