django-redis和redis连接

redis连接

简单连接

import redis
 
r = redis.Redis(host='10.211.55.4', port=6379)
r.set('foo', 'Bar')
print r.get('foo')

连接池

import redis
 
pool = redis.ConnectionPool(host='10.211.55.4', port=6379)
 
r = redis.Redis(connection_pool=pool)
r.set('foo', 'Bar')
print r.get('foo')

django-redis

安装

pip3 install django-redis

配置

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}
            # "PASSWORD": "密码",
        }
    }
}

视图中操作连接

from django_redis import get_redis_connection
conn
= get_redis_connection() conn.set(phone, random_code, ex=30)

猜你喜欢

转载自www.cnblogs.com/a438842265/p/12359286.html