zoukankan      html  css  js  c++  java
  • python使用Redis

    python使用Redis数据库

    安装依赖

    pip3 install redis
    

    直接脚本文件使用

    import redis
    r = redis.Redis(host='127.0.0.1', port=6379, db=1)
    
    # 基础的脚本文件测试
    r.set('name','xu')
    r.set('age',18)
    

    半连接池使用

    import redis
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=10, max_connections=100)
    r = redis.Redis(connection_pool=pool)
    

    cache缓存使用,要额外安装django-redis

    pip install django-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}
            }
        }
    }
    
    
    #操作cache模块直接操作缓存:views.py
    from django.core.cache import cache  # 结合配置文件实现插拔式
    # 存放token,可以直接设置过期时间
    cache.set('token', 'header.payload.signature', 10)
    # 取出token
    token = cache.get('token')
    
  • 相关阅读:
    10 给予scrapy-redis的分布式爬虫
    pandas 01 序列和数据库昂
    如何控制分布式爬虫结束
    动态导入模块
    docker
    09 scrapy中间件
    scrapy 获取settings中的内容
    session对象的cookies
    文本检测-1-MSER
    CTW1500数据集介绍
  • 原文地址:https://www.cnblogs.com/XuChengNotes/p/12019861.html
Copyright © 2011-2022 走看看