2

With django.core.cache.backends.locmem.LocMemCache this worked:

key = cache.make_key('foo')
cache.validate_key(key) 
t = cache._expire_info.get(key)

But it breaks with django.core.cache.backends.memcached.MemcachedCache

I get error: 'MemcachedCache' object has no attribute '_expire_info'

Is there an universal or memcached-specific way to get expiration time for specified key?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
petr0
  • 411
  • 7
  • 10

5 Answers5

2

Unfortunately that is not possible. When you use LocMemCache Django manages the keys and their expiry dates itself, so the info is available. On the other hand when you use memcached, Django does not keep track of the keys.

This info is also not available through mamcached Python API, so if you want to have access to this information you pretty much have to keep track of it yourself (i.e. save an expiration date for every key in an additional dictionary, every time you save something to memcached).

Ludwik Trammer
  • 24,602
  • 6
  • 66
  • 90
2

A little outdated answer but I'll posted anyway for the benefit of the community.

I recently found a python library to retrieve information from Memcache server which in my case did the trick:

https://github.com/ezubillaga/python-memcached-stats

(this is a fork from the original library with Python 3 support)

The library have no dependencies, is just a single python file with a few lines of code, so it turned to be a pretty neat and simple solution for my problem. The method key_details returns a list of all the keys on each server's slab, including size and expiration of each one.

Erasmo
  • 84
  • 5
1

This works for me in localhost (NOT for memcached):

    from django.core.cache import cache    
    print cache._expire_info

I am using python 2.7 and Django 1.7.

jobima
  • 5,790
  • 1
  • 20
  • 18
0

_expire_info is unfortunately not available if you are using Memcached like mentioned by other answers. I ended up doing this and I'm sharing my simple solution here to help future readers:

from django.core.cache import cache
import time

cachekey = 'somerandomkey'
cacheexpkey = '%s-exp' % cachekey

data = {'name': 'John Doe'}
cache.set(cachekey, data, 3600)
cache.set(cacheexpkey, int(time.time()) + 3600)

This way, you can later get the expiry timestamp for a cache key. Maybe any better solution is there but I used this simple approach.

Rehmat
  • 4,681
  • 3
  • 22
  • 38
0

According to my experiments, it is impossible to get the expiration times of cache values with cache._expire_info in LocMemCache. *I asked the question about how to get the expiration times of cache values with cache._expire_info properly.

For example, I set 3 cache values with timeout and get the expiration times with cache._expire_info as shown below:

from django.core.cache import cache

cache.set("name", "John", timeout=60)
cache.set("age", 36, timeout=120)
cache.set("gender", "Male", timeout=180)

print(cache._expire_info)

But, the expiration times are hidden in numbers as shown below:

{':1:name': 1692720916.654848, ':1:age': 1692720976.654848, ':1:gender': 1692721036.654848}
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129