阿语python4-2 美多商城v5.0第1.2.5节 配置Redis数据库

从今天开始阿语将更新美多商场实战笔记,也就说从今天开始,下面的全部都是实战,喜欢的可以关注一下,一起学习python

配置Redis数据库

美多商城数据缓存服务采用Redis数据库

1. 安装django-redis扩展包

1.安装django-redis扩展包

$ pip install django-redis

2.django-redis使用说明文档

点击进入文档

2. 配置Redis数据库

CACHES = {
    "default": { # 默认
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/0",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
    "session": { # session
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "session"

default:

  • 默认的Redis配置项,采用0号Redis库。

session:

  • 状态保持的Redis配置项,采用1号Redis库。

SESSION_ENGINE

  • 修改session存储机制使用Redis保存。

SESSION_CACHE_ALIAS:

  • 使用名为"session"的Redis配置项存储session数据

配置完成后:运行程序,测试结果。

#!/usr/bin/env python
import os
import sys


if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings.dev")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)


你可能感兴趣的:(redis,缓存,数据库,mysql,hibernate)