zoukankan      html  css  js  c++  java
  • Python 连接Redis两中方式

    一.通过django里设置settings文件连接redis

    #1.settings添加
    
    CACHES = {
        "default": {
            "BACKEND": "django_redis.cache.RedisCache",
            "LOCATION": "redis://192.168.8.102:6379/0",
            "OPTIONS": {
                "CLIENT_CLASS": "django_redis.client.DefaultClient",
           "CONNECTION_POOL_KWARGS": {"max_connections": 100}
           # "PASSWORD": "密码",

    } },
    "session": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://192.168.8.102:6379/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } }, "sms_code": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://192.168.8.102:6379/15", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } } } # 保存 session数据到 Redis中 SESSION_ENGINE = "django.contrib.sessions.backends.cache" SESSION_CACHE_ALIAS = "session"


    2.导入get_redis_connection连接
    from django_redis import get_redis_connection
    conn = get_redis_connection("default")

    二.通过redis模块

    import redis
    
    pool = redis.ConnectionPool(host='192.168.8.102', port=6379,password='密码',db="库",)
    con = redis.Redis(connection_pool=pool)
  • 相关阅读:
    [转]优秀的程序员不会觉得累成狗是一种荣耀
    .NET读写Excel工具Spire.XlS使用(DataExport )
    WPF之Binding深入探讨
    第一个WPF应用程序
    Visio作图
    唯一的重复元素
    Strange Problem O(∩_∩)O~
    数据库知识点①
    HDU 2825 Wireless Password
    POJ 1625 Censored!
  • 原文地址:https://www.cnblogs.com/icemonkey/p/10536536.html
Copyright © 2011-2022 走看看