Showing posts with label Redis. Show all posts
Showing posts with label Redis. Show all posts

Wednesday, 16 July 2025

redis data presistence

 https://redis.io/docs/latest/operate/oss_and_stack/management/persistence/


  • RDB (Redis Database): RDB persistence performs point-in-time snapshots of your dataset at specified intervals.
  • AOF (Append Only File): AOF persistence logs every write operation received by the server. These operations can then be replayed again at server startup, reconstructing the original dataset. Commands are logged using the same format as the Redis protocol itself.
  • No persistence: You can disable persistence completely. This is sometimes used when caching.
  • RDB + AOF: You can also combine both AOF and RDB in the same instance.

AOF needs more memory (30% free memory)
but can save on every write 
disk usage can also be big

https://stackoverflow.com/questions/25328317/does-redis-persist-data

Friday, 8 March 2024

redis clear cache on docker restart

 https://stackoverflow.com/questions/54533308/disable-redis-persistence-in-docker

  redis:
    image: "redis:5.0.3-alpine"
    command: [sh, -c, "rm -f /data/dump.rdb && redis-server"]  # disable persisten

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