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

It must be saved somewhere. I can change it with set() / incr() , but I could not find a way to read it.

+10
django caching
source share
2 answers
 cache._expire_info.get('foo') 

to get unix timestamp

+4
source share

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 find out the remaining time in seconds:

 cache_expire_time.seconds 

Please note that this seems to work only for locmem and not memcached, if anyone knows how to do this in memcached, please comment

+3
source share

All Articles