Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Compress your ActiveRecord sessions (See related posts)

Using ActiveRecordStore and your sessions are getting too big? Try this!

# in environment.rb or some file you require
require 'zlib'
CGI::Session::ActiveRecordStore::Session.class_eval {
  class << self
    def marshal_with_compression(data)
      Zlib::Deflate.deflate(marshal_without_compression(data))
    end
    def unmarshal_with_compression(data)
      unmarshal_without_compression(Zlib::Inflate.inflate(data))
    end
    alias_method_chain :marshal, :compression
    alias_method_chain :unmarshal, :compression
  end
}

# in migration
def self.up
  change_column :sessions, :data, :binary
end

def self.down
  change_column :sessions, :data, :text
end

You need to create an account or log in to post comments to this site.


Click here to browse all 4834 code snippets

Related Posts