Django Redis

Heroku Redis

Overview -> Configure Add-ons -> Add-ons -> enter Heroku Redis

Requirement

django-redis

pip3 install django-redis==5.2.0

Setting

{ProjectName}/settings.py -> CACHES add redis config as below

# CACHES
# https://docs.djangoproject.com/en/dev/topics/cache/
# CACHES Base on File
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': '/var/tmp/django_cache',
    },
    "heroku": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": os.environ.get('REDIS_TLS_URL'),
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {
                "ssl_cert_reqs": None,
                "max_connections": 18
            },
        }
    },
    'local': {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

Use Redis

from django_redis import get_redis_connection
from django.views.decorators.cache import never_cache

@never_cache
@ratelimit(key='ip', rate='1/s', block=True)
def Redis(request):
    # Use the name you have defined for Redis in settings.CACHES
    r = get_redis_connection("heroku")
    msg = "Redis Check = %r" % (r.ping())
    print(msg)
    return HttpResponse(msg)
© Kimi Tsai all right reserved.            Updated : 2023-07-12 09:04:53

results matching ""

    No results matching ""

    results matching ""

      No results matching ""