Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Pickling and caching the current request.user, good or bad idea?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Kevin  
View profile  
 More options Oct 29 2011, 6:45 am
From: Kevin <kveron...@gmail.com>
Date: Sat, 29 Oct 2011 03:45:41 -0700 (PDT)
Local: Sat, Oct 29 2011 6:45 am
Subject: Pickling and caching the current request.user, good or bad idea?
In my attempts to lower database hits, I found that on most of my
pages, auth_user is hit every time.  I had an idea about caching this
object in the current session.  Is this a good idea, or will it cause
more problems than it helps?  If so, what type of problems will I be
looking at?

My app rarely modifies the auth_user table, if at all.  Here is a
quick middleware I wrote to go aloneside the current
AuthenticationMiddleware, and it works perfectly!  Only 2 database
hits when viewing an account page.  One hit to the session table, and
other hit to the table which holds various account settings.

class UserPickleMiddleware(object):
  def process_request(self, request):
    if 'cached_user' not in request.session:
      from django.contrib.auth import get_user
      request.session['cached_user'] = get_user(request)
    request._cached_user = request.session['cached_user']
    return None

I place this just before the standard AuthenticationMiddleware.  I
will continue to run tests and see how it functions, if say the user
is added or removed from a group.

My sessions are stored in a database, which makes this interesting.
As it's only hitting the database once to grab both the session and
current user information.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kevin  
View profile  
 More options Oct 29 2011, 7:23 am
From: Kevin <kveron...@gmail.com>
Date: Sat, 29 Oct 2011 04:23:09 -0700 (PDT)
Local: Sat, Oct 29 2011 7:23 am
Subject: Re: Pickling and caching the current request.user, good or bad idea?
Okay, so the last one there had a hickup, users could not longer
login, once they have logged out.  It kept the AnonymousUser cached...

Here is an update which works with logging back in:

class UserPickleMiddleware(object):
  def process_request(self, request):
    if 'cached_user' not in request.session:
      from django.contrib.auth import get_user
      user = get_user(request)
      if isinstance(user, User):
        request.session['cached_user'] = user
    if 'cached_user' in request.session:
      request._cached_user = request.session['cached_user']
    return None

This one makes sure that the user instance is indeed a regular User
and not some Anonymous person before setting the cached data.  The
code could be more optimized.  I didn't use an Else, because it's
needs to set the request variables.  I'd rather write an extra "if"
instead of copying the same line to set the request variable twice.

On Oct 29, 5:45 am, Kevin <kveron...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »