Search for a key in django.core.cache

1.2k views Asked by At

I am writing a simple livechat app using django. I keep data about chat sessions in a static variable on my Chat class. Locally it really works.

I have deployed a test version of an app on heroku, but heroku is a cloud platform. There is no synchronization between class variables in different threads.

So I decided to use memcached. But I can't find if django.core.cache allows search for a key in cache or iterate through entire cache to check values. What is the best way to solve this problem?

1

There are 1 answers

0
alTus On

Memcached only allows you to get/set entries by their keys. You can't iterate these entries to check something. But if your cache keys are sequential (like sess1, sess2, etc.) you can try to check for existence in a loop:

for i in range(1000):
    sess = cache.get('sess%s' % i)
    # some logic

But anyway it seems like a bad design decision. I don't have enough information about what you're doing but I guess that some sort of persistent storage (like database) would work nice. You can also consider http://redis.io/ which has more features than memcached but still very fast.