django cache:(for django >4.0.0)
https://docs.djangoproject.com/en/5.0/topics/cache/
can use redis :
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
}
}
from django.core.cache import cache
>>> cache.set("my_key", "hello, world!", 30)
cache.get(key, default=None, version=None)¶
>>> cache.get("my_key")
'hello, world!'
django cache timeout:
https://stackoverflow.com/questions/51948687/how-can-i-expire-entries-to-django-database-cache
I think you solve the problem at the wrong level: the CACHES settings have a setting for automatic expiration: the 'TIMEOUT' key:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'exchange_rate_cache',
'TIMEOUT': 604800
}
}
install redis, and redis in python
redis docker:
redis:
image: redis:7.2.4-alpine
restart: unless-stopped
ports:
- "6379:6379"
redis python module:
redis>=5.0.0
https://hub.docker.com/_/redis
django to connect redis with docker
use redis's app name
CACHES = {
'default': {
'BACKEND': "django.core.cache.backends.redis.RedisCache",
'LOCATION': 'redis://redis/',
'TIMEOUT': None,
}
}
docker-compose redis specify config file and pwd:
https://stackoverflow.com/questions/30547274/redis-in-docker-compose-any-way-to-specify-a-redis-conf-file
It is an old question but I have a solution that seems elegant and I don't have to execute commands every time ;).
1 Create your dockerfile like this
FROM redis
CMD ["redis-server", "--include /usr/local/etc/redis/redis.conf"]
What we are doing is telling the server to include that file in the Redis configuration. The settings you type there will override the default Redis have.
2 Create your docker-compose
redisall:
build:
context: ./bin/redis
container_name: 'redisAll'
restart: unless-stopped
ports:
- "6379:6379"
volumes:
- ./config/redis:/usr/local/etc/redis
3 Create your configuration file it has to be called the same as Dockerfile
//config/redis/redis.conf
requirepass some-long-password
appendonly yes
bind 127.0.0.1
// and all configurations that can be specified
// what you put here overwrites the default settings that have the
container