0

hello friends in new project i use Django4 and i set the "django.core.cache.backends.redis.RedisCache" for the cache but i have the error it say "AttributeError: 'RedisCache' object has no attribute 'ttl'" .it refer to the line of my code that i add it below :

from django.core.cache import cache

def validate_token(token, check_time=True):
 if cache.ttl(token) == 0:
     return False
 try:
     data = claim_token(token)
 except Exception:
    return False
 if "type" not in data.keys():
    return False

can anyone tell me how i can resolve it ? (i know ttl mean time to live)

fahime
  • 5
  • 4
  • I think you should use the `django-redis` package. And if you are already using it, you should set `"BACKEND": "django_redis.cache.RedisCache"`, [as per the django-redis doc](https://github.com/jazzband/django-redis). `ttl` is a especial function that is only available through the `django-redis` package – Bao Huynh Lam Feb 08 '22 at 15:53
  • i installed "django-redis" package but i have this. error and i set "BACKEND": "django.core.cache.backends.redis.RedisCache" but i have this error...i dont know what is wrong? – fahime Feb 08 '22 at 16:00
  • if you are using django-redis, you should set "BACKEND": "django_redis.cache.RedisCache", [as per the django-redis doc.](https://github.com/jazzband/django-redis) – Bao Huynh Lam Feb 08 '22 at 16:20
  • i want to know why it is not compatible with "django.core.cache.backends.redis.RedisCache" – fahime Feb 11 '22 at 10:31
  • 1
    because `ttl` is a special function that is exposed through the use of `django-redis`. The original RedisCache just does not have support ttl. That's all – Bao Huynh Lam Feb 11 '22 at 13:52
  • but i need to ttl(time to live )how to have it in djnago4 with "django.core.cache.backends.redis.RedisCache" – fahime Feb 18 '22 at 07:43

0 Answers0