Blog » Blog Entry

Clean Rubyonrails DB Sessions Filter

January 27, 2007

There's quite a few methods floating around the 'net on how to cleanup old sessions from your database. I think using cron is a good last resort but my prefered way is to let natural server usage do it instead:

class ApplicationController < ActionController::Base

  before_filter :clean

  def clean
    ActiveRecord::Base.connection.execute( "
      DELETE FROM sessions 
      WHERE NOW() - updated_at > 3600
    " ) if rand( 1000 ) % 10 == 0
  end

end

Others setups are probably similar but I'm quite sure this is exactly how PHP session cleaning occurs since there are tunable parameters in the php.ini file:

  session.gc_probability = 1
  session.gc_divisor     = 100

Yup.

Tags: php, ruby, rubyonrails, sessions

« Hating XML is only human Tom and Richard, Two Peas in a Pod »

Add a comment:

Title:

Comment:

Name:

Email: