X
X
Xawer2015-05-16 22:13:22
Django
Xawer, 2015-05-16 22:13:22

Django. Can't figure out the cache?

Good day!
Sorry for the stupid question. Faced with the behavior of the cache that is incomprehensible to me. Couldn't google anything.
The point is this.
There is a code that extracts photos from the database:

cacheName = 'photosPage%s' %page
photos = cache.get(cacheName)
if photos == None:
    photos = (Thumbs.objects
                                .filter(pubDate__lte=datetime.now()) 
                                .all[:100])
    cache.set(cacheName, photos 60 * 10)

And then comes the code that should increase the number of views for photos:
for photo in photos:
    photo.views += 1
    photo.save()

Which, accordingly, does not work.
More precisely, it works only at the time of rebuilding the cache, or if the cache is disabled.
Please help and kick in the right direction)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2015-05-16
@Xawer

You have stored in photos what is received from the cache. Those. you work not handed over from a DB. To increase the counter, you need to access the database.
The operation of the counter during rebuilding is due precisely to the fact that at this moment information is stored there for working with the database.
cache.get(cacheName) != Thumbs.objects.filter(pubDate__lte=datetime.now()).all[:100]
It's better to increment the counter in memory and drop it periodically in the database.

S
sim3x, 2015-05-16
@sim3x

It’s worth caching after it’s clear what has become a bottleneck.
It’s still not clear why cache the pictures of the current day if they change

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question