Saturday, January 31, 2015

Re: How to get hold of a session after getting signaled?

You still could store the do_something_after_badge_awarded result in a
database table, but cache the lookups from the view for something
short, like 5 minutes. How quickly after a badge is awarded do people
need to be notified?

Also, you could use a memcached library to store/fetch the awards by
user ID. Should be pretty quick. That assumes of course that it's OK
to lose award notifications if memcached restarts. Would look
something like:

# store
mc.append(key_for_user, "{0} ".format(award_obj.pk))

# ...

# get. use a lockless cas() algo
award_objs = None
while award_objs is None:
award_ids, cas_token = mc.gets(key_for_user)
if not award_ids:
# No awards waiting for us
award_objs = []
elif mc.cas(key_for_user, "", cas_token):
# Awards didn't change on us, continue
award_objs = Awards.objects.filter(pk__in=award_ids.split())

Or you could do something with the Django cache framework. Or you
could use local files and serialization.

But I do think you should test your assumption that the DB check for
awarded awards will be too expensive. Especially if cached. Most apps
make a ton of DB calls anyway, and this one will mostly be empty.

On Sat, Jan 31, 2015 at 2:02 AM, Tobias Dacoir <falc410@gmail.com> wrote:
> I can't just call badge.award_to(user) because I need to wait for the check
> logic to verify if the user should receive a badge or not.
> My workaround is that I created a new table that inserts badge and user
> foreign keys and before the view is rendered I will check this table for any
> entries for that user. If there are, I will copy the data to a message and
> delete the object from the database again.
>
> It's rather ugly due to the many database hits which I would like to avoid
> but I can't think of anything else at the moment. Apparently (using google)
> there is no way to get hold of the request in a post_save() signal - which I
> need to check all achievements.
>
> The only real solution would be to not use this post_save() signal but
> manually do the checking for achievements before my view is rendered, which
> would require some work on the original source code. I will look into it
> though next week.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/ee9653c8-2c8e-479e-871b-34774862888b%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD4ANxWOT1Mg%3D47H89s0Xoi9m7kVA-y1Ag5qoh9vrdO9Nuc7tg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment