0

It seems each thread maintains its separate cache. Should I just use my own Django object to have a Database cache shared by multiple threads? What are the negatives to doing this?

The problem I am facing is this:

  1. I am using the database cache
  2. I save to the cache while processing a request
  3. I retrieve from the cache while processing the next request

I expect it to be in the cache but it is not there. By printing the thread ident, I see it is a different thread. The same thread gets a cache hit but a different thread gets a cache miss.

I don't expect this to happen with the database cache backend.

sureshvv
  • 4,234
  • 1
  • 26
  • 32

1 Answers1

0

This is only true if you use the locmem cache backend. Which you should not do in production; use a proper cache, whether it is the filesystem or db caches, or a dedicated cache backend like memcached or redis.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • From the documentation: "To provide thread-safety, a different instance of the cache backend will be returned for each thread." I verified this to be true in practice during my debugging. – sureshvv Sep 22 '16 at 17:45
  • Yes, but that didn't seem to be what you were asking. Each instance would normally point to the same store, so that they would have access to the same data. – Daniel Roseman Sep 22 '16 at 17:49
  • Do I have to use locks to read and write using the database cache? I am not doing that yet. Could that be my problem? – sureshvv Sep 22 '16 at 18:18
  • I don't even know what your problem is. What issue are you having? And what threads do you have in the first place? – Daniel Roseman Sep 22 '16 at 18:22
  • http://stackoverflow.com/questions/23611737/django-global-data-for-threads seems to agree with you – sureshvv Sep 22 '16 at 18:22
  • I have expanded my question with more background. – sureshvv Sep 22 '16 at 18:25