Thursday, 29 February 2024

django cache, redis, python

 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  # 7 days
    }
}
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

#/bin/redis/Dockerfile
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

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.*
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

 // and all configurations that can be specified
 // what you put here overwrites the default settings that have the
 container





No comments:

Post a Comment