forked from erp-dev/erp
fix: redis url conflict
This commit is contained in:
@@ -83,6 +83,8 @@ services:
|
|||||||
DB_NAME: flower
|
DB_NAME: flower
|
||||||
DB_USER: postgres
|
DB_USER: postgres
|
||||||
DB_PASSWORD: postgres
|
DB_PASSWORD: postgres
|
||||||
|
CACHE_HOST: redis
|
||||||
|
CACHE_PORT: "6379"
|
||||||
depends_on:
|
depends_on:
|
||||||
- pgbouncer
|
- pgbouncer
|
||||||
- redis
|
- redis
|
||||||
|
|||||||
@@ -70,6 +70,8 @@ services:
|
|||||||
DB_PASSWORD: "${DB_PASSWORD:-postgres}"
|
DB_PASSWORD: "${DB_PASSWORD:-postgres}"
|
||||||
CELERY_BROKER_URL: "${CELERY_BROKER_URL:-amqp://guest:guest@rabbitmq:5672//}"
|
CELERY_BROKER_URL: "${CELERY_BROKER_URL:-amqp://guest:guest@rabbitmq:5672//}"
|
||||||
CELERY_RESULT_BACKEND: "${CELERY_RESULT_BACKEND:-redis://redis:6379/0}"
|
CELERY_RESULT_BACKEND: "${CELERY_RESULT_BACKEND:-redis://redis:6379/0}"
|
||||||
|
CACHE_HOST: ${CACHE_HOST:-redis}
|
||||||
|
CACHE_PORT: "${CACHE_PORT:-6379}"
|
||||||
depends_on:
|
depends_on:
|
||||||
- pgbouncer
|
- pgbouncer
|
||||||
- redis
|
- redis
|
||||||
|
|||||||
@@ -204,7 +204,8 @@ DATABASES = {
|
|||||||
# Cache configuration
|
# Cache configuration
|
||||||
# https://docs.djangoproject.com/en/5.2/topics/cache/
|
# https://docs.djangoproject.com/en/5.2/topics/cache/
|
||||||
# Django 5.x 内置 Redis 缓存后端,使用 redis 包(已安装)
|
# Django 5.x 内置 Redis 缓存后端,使用 redis 包(已安装)
|
||||||
# 容器内默认使用 redis://redis:6379/0,本地开发可使用 redis://localhost:6379/0
|
# Redis 连接配置:优先使用 CACHE_URL,否则从 CACHE_HOST/CACHE_PORT/CACHE_DB 构建
|
||||||
|
# 默认 localhost:6379(适合宿主机运行),Docker Compose 可通过环境变量指定 CACHE_HOST=redis
|
||||||
|
|
||||||
# 检测是否在测试环境中
|
# 检测是否在测试环境中
|
||||||
import sys
|
import sys
|
||||||
@@ -220,13 +221,18 @@ if TESTING:
|
|||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
# 生产/开发环境使用 Redis
|
# 生产/开发环境使用 Redis
|
||||||
|
# 优先使用 CACHE_URL(完整 URL),否则从 host/port/db 构建
|
||||||
|
cache_url = env('CACHE_URL', default=None)
|
||||||
|
if cache_url is None:
|
||||||
|
cache_host = env('CACHE_HOST', default='localhost')
|
||||||
|
cache_port = env.int('CACHE_PORT', default=6379)
|
||||||
|
cache_db = env.int('CACHE_DB', default=0)
|
||||||
|
cache_url = f'redis://{cache_host}:{cache_port}/{cache_db}'
|
||||||
|
|
||||||
CACHES = {
|
CACHES = {
|
||||||
'default': {
|
'default': {
|
||||||
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
|
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
|
||||||
'LOCATION': env(
|
'LOCATION': cache_url,
|
||||||
'CACHE_URL',
|
|
||||||
default='redis://redis:6379/0', # 容器内默认地址
|
|
||||||
),
|
|
||||||
'OPTIONS': {
|
'OPTIONS': {
|
||||||
# Redis 连接选项
|
# Redis 连接选项
|
||||||
'socket_connect_timeout': 5, # 连接超时(秒)
|
'socket_connect_timeout': 5, # 连接超时(秒)
|
||||||
|
|||||||
Reference in New Issue
Block a user