Posts Tagged ‘session’

OMFG Sessions!! Clear them out!!

Wednesday, January 14th, 2009

So since my site, jamzee.com has been in production for over a year now, I have gotten ambitious and decided to do some database maintenance. The first thing I did was log into the server and run a:

mysqlcheck -o database -u username -p

This runs a bunch of mysql tasks; repair, optimize, etc. I notice this was taking a long time on one of the tables which happened to be the sessions table. I got suspicious and logged into the database for a peak.

What did I find, LOTS AND LOTS of rows, 150k+. This seemed relatively unnecessary so after some research found out that I can run the following:

rake db:sessions:clear

After some time of running that, we have a nice clear, and smaller database now! Next task is to throw that on a cron, probably run on a weekly interval.

Moral of the story, CLEAR THOSE SESSIONS!

session[:current_user] = @user => BAD!

Friday, August 22nd, 2008

I’m sure most of you already would know this, or use restful authentication that handles it for you. However, if you have some custom setup where you are loading a user object, and then storing it in session, slap to you!

Basically what I am talking about is doing this in your login method:

session[:current_user] = @user

Instead you should do:

session[:current_user] = @user.id

And then in your application controller, setup a before filter like so:

def set_current_user
  @current_user = User.find(session[:current_user])
end

One main reason not to do that would be if you had to update some user information. If you had it stored in session, then the user would have to log out and log back in for the changes to take effect. This is of course a basic rough draft, but you get the idea.