Refresh object from private counter - Python

In the appengine python application, I have counters that count the number of favs for a photo and the number of views.

I have photos and counters. To order photos by popularity (# from favs), I must have # favs stored in my Photo objects (Photo.all (). Order ('num_favs')). Since I keep track of the number of favs in a closed counter, I wonder what is the most efficient way to update my Yahoo objects with the latest favs number from the Counter model?

Here is a thesis about how my models look:

class Photo(db.Model): photo = db.BlobProperty() favs = db.IntegerProperty() #num of favs class Counter(db.Model): #key_name = key to a model that to be counted - Photo in this case name = db.StringProperty() #counted entity key count = db.IntegerProperty() #count type = db.StringProperty() #type of model to count, "Photo" in this example #Very unefficient way of updating my Photo entities with the latest count from Counter: fav_counters = Counter.all().filter('type =', 'Photo') for fav in fav_counters: photo_fav = db.get(fav.name) #get the photo for which the fav count is for photo_fav.favs = fav.count photo_fav.put() 

I would appreciate it if I could answer: 1) The most efficient way to arrange objects based on their counters 2) If the logic that I follow to sort objects based on their counters is correct - what is the most efficient way to update Photo objects with their counters?

Thanks!

+4
source share
1 answer

If you need to sort or filter based on a value, you probably shouldn't use a closed-line counter, but rather use one of the alternatives. In short, this is:

  • Just use a regular counter. If you do not expect the refresh rate to exceed 1-5QPS for extended periods of time (short bursts are ok) then this should work fine.
  • Put the task in the task queue when the user or favorites are favorites. Ensure that the task queue has a limited execution speed to limit the conflict.
  • Use one of the templates with loss but no conflict, like this one .
+9
source

Source: https://habr.com/ru/post/1338352/


All Articles