Skip to main content

A package to support redis session store in django project

Project description

安装
pip install th-redis-session 

修改 django 的 settings.py

# 导入redis的异常包
from redis import exceptions

# 1. 添加如下配置
# session的存储配置
SESSION_ENGINE = 'th_redis_session.RedisSessionStore'
SESSION_CACHE_ALIAS = 'default'

# 设置session失效时间,单位为秒,默认24小时
SESSION_COOKIE_AGE = 24*60*60
SESSION_COOKIE_NAME = 'SESSION'

# redis session key 格式,默认以thsc开头
def redis_key_func(key, key_prefix, version):
    return 'thsc:%s' % (key)

# redis 配置
REDIS_HOST=os.environ.get('REDIS_HOST', 'your_host')
REDIS_PORT=os.environ.get('REDIS_PORT', 'your_port')
REDIS_DB=os.environ.get('REDIS_DB', 'your_db')
REDIS_PASSWORD=os.environ.get('REDIS_PASSWORD', "your_pwd")

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://%s:%s/%s" % (REDIS_HOST, REDIS_PORT, REDIS_DB),
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "SERIALIZER": "django_redis.serializers.json.JSONSerializer",
            "PASSWORD": REDIS_PASSWORD,
            "SOCKET_CONNECT_TIMEOUT": 60,  # seconds
            "SOCKET_TIMEOUT": 60,  # seconds
            "CONNECTION_POOL_KWARGS": {
                "max_connections": 20, 
                "retry_on_timeout": True, 
                "health_check_interval": 30, 
                "socket_keepalive": True, 
                "retry_on_error":[exceptions.ConnectionError]
            }
        },
        "KEY_FUNCTION": redis_key_func
    }
}

# 2. 从 INSTALLED_APPS 中删除 'django.contrib.sessions'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

th-redis-session-0.9.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

th_redis_session-0.9.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file th-redis-session-0.9.0.tar.gz.

File metadata

  • Download URL: th-redis-session-0.9.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.9

File hashes

Hashes for th-redis-session-0.9.0.tar.gz
Algorithm Hash digest
SHA256 8a78f9fb85661aa5a2176a5dc63a1fc6fef29bd1ecb0a6ec32165bf1d3782704
MD5 459023edbce482f162cae8bccd3f48aa
BLAKE2b-256 830f6bc2a221fdd54f4ca944243408dfb8a6420dfc0f0157cb1c9d3d1a3c24c3

See more details on using hashes here.

File details

Details for the file th_redis_session-0.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for th_redis_session-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5c6884572368680637f4169ecc0a2e062fa6bdbb9a18de545ea5c2480ff7d2b
MD5 e53d6bb770fc1ce1556490ef3f0d794b
BLAKE2b-256 c07c0913b23d79bb20d5b69fb14c7e8c3275139ce13baeacb1fbff9dcd3ba305

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page