1
0
forked from erp-dev/erp

fix: redis url conflict

This commit is contained in:
2025-12-30 14:20:14 +08:00
parent 138983fa4e
commit d96373980d
3 changed files with 15 additions and 5 deletions

View File

@@ -83,6 +83,8 @@ services:
DB_NAME: flower
DB_USER: postgres
DB_PASSWORD: postgres
CACHE_HOST: redis
CACHE_PORT: "6379"
depends_on:
- pgbouncer
- redis

View File

@@ -70,6 +70,8 @@ services:
DB_PASSWORD: "${DB_PASSWORD:-postgres}"
CELERY_BROKER_URL: "${CELERY_BROKER_URL:-amqp://guest:guest@rabbitmq:5672//}"
CELERY_RESULT_BACKEND: "${CELERY_RESULT_BACKEND:-redis://redis:6379/0}"
CACHE_HOST: ${CACHE_HOST:-redis}
CACHE_PORT: "${CACHE_PORT:-6379}"
depends_on:
- pgbouncer
- redis

View File

@@ -204,7 +204,8 @@ DATABASES = {
# Cache configuration
# https://docs.djangoproject.com/en/5.2/topics/cache/
# 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
@@ -220,13 +221,18 @@ if TESTING:
}
else:
# 生产/开发环境使用 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 = {
'default': {
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
'LOCATION': env(
'CACHE_URL',
default='redis://redis:6379/0', # 容器内默认地址
),
'LOCATION': cache_url,
'OPTIONS': {
# Redis 连接选项
'socket_connect_timeout': 5, # 连接超时(秒)