zoukankan      html  css  js  c++  java
  • 在python当中使用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  
    # "LOCATION": "redis://127.0.0.1:6379/3", 后面这个3表示指定Redis那个数据库
    CACHES = {
        "default": {
            "BACKEND": "django_redis.cache.RedisCache",
            "LOCATION": "redis://127.0.0.1:6379/3",
            "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')
    
  • 相关阅读:
    hibernate hql
    数据库锁机制
    Spring 事物管理
    spring自动代理
    spring 其它增强类型
    spring
    mybatis动态sql
    SSH注解整合
    ssh整合
    错题解析
  • 原文地址:https://www.cnblogs.com/raynduan/p/11844992.html
Copyright © 2011-2022 走看看