Can I get expiration time of specified key in django cache?

cache._expire_info.get('foo') 

to get the unix timestamp


RedisCache has ttl:

cache.ttl('foo:bar:2020-09-01')

to get the unix timestamp:

 cache_expire_time = datetime.datetime.fromtimestamp(
                    [value for key, value in cache._expire_info.items() if 'devices' in key.lower()][0]
                ) - datetime.datetime.now()

to get the remaining time in seconds:

cache_expire_time.seconds

Note that it looks like this only works for locmem, not memcached, if someone knows how to do this in memcached please comment

Tags:

Django

Caching