用 Redis 给 Django 加一层缓存,QPS 提升明显
配置 Redis 缓存
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
}
}
视图级缓存
from django.core.cache import cache
posts = cache.get_or_set("home_posts", lambda: list(Post.objects.all()[:3]), 900)
注意设置合理的过期时间,并在数据变更时主动失效,避免脏数据。
广告位占位 · post-inline