Recently by Andrew Avenoso

There are many ways to store sessions in Ruby on Rails, but by using JRuby, we get another option that I think supersedes the common solutions... Using the Java Server's built in session store.

First... The implementation

This is assuming that you are running JRuby and using a tool like warbler to deploy to a Java server like Tomcat.

config/initializers/session_store.rb
# check if we're running in a java container
if defined?($servlet_context)
    require 'action_controller/session/java_servlet_store'
    # tell rails to use the java container's session store
    ActionController::Base.session_store = :java_servlet_store
else
    # other session store for development in webrick/mongrel
# setup cookie based session
ActionController::Base.session = {
:key => '_dd_session',
:secret => '<Your really long random secret key>'
}
end
This will store your rails session in Tomcat's session store exactly as if you were running a Java web framework.

Second... Why?

There are a few common solutions starting with the rails default.

Cookie Store: This is not very secure, limited in size, and sends a lot of data back and forth to the client. It is strongly recommended to not use this for production systems.
File Store: Stores sessions on the file system. Every cookie access involves file system IO. Does not support clustering by default but can be achieved through a SAN or shared storage.
ActiveRecord: Stores sessions in the database. Slow due to database IO. Natively supports clustering.
Memcached: Stores sessions in Memcached instance. Very popular and works well with clusters. Can run into RAM issues on systems with lots of users. Requires an instance of Memcached running.
Java Servlet Store: Works just like your other Java applications. Java Servers can be configured to cluster sessions. Uses the JSESSIONID cookie or url param (in Tomcat)

Conclusion

This won't be the solution for everyone, but if you're deploying a JRuby on Rails app into an infrastructure based on Java, then it provides a simple and efficient solution.
I just learned about a feature in Rails that I've been wanting for a long time. In the past, if you were to run two identical database queries in a row, it would hit the database twice
p = Person.find 1 p = Person.find 1

produces the following logging
Person Load (0.010000)?[0m ?[0;1mSELECT * FROM people WHERE (people.`id` = 1) Person Load (0.010000)?[0m ?[0;1mSELECT * FROM people WHERE (people.`id` = 1)

If you freeze to the Rails Edge the previous code will now produce this
Person Load (0.010000)?[0m ?[0;1mSELECT * FROM people WHERE (people.`id` = 1) CACHE (0.000000)?[0m ?[0mSELECT * FROM people WHERE (people.`id` = 1)

So the second call is being pulled from the cache and takes 0.000000 seconds to retrieve By default this is available when running in a controller or .rhtml

If you want to see this work on the console you can use the cache method

Person.cache do   p = Person.find 1   p = Person.find 1 end

This is by no means a replacement for memcache, but I like it because it increases performance without having to do any additional coding or configuration. Another thing to remember is that the cache only lasts as long as the request, so you don't need to worry about stale data or cache expiration. Now... Why would any programmer do a find for the same id multiple times in one request? You probably won't, but ActiveRecord will. If you have a highly denormalized database with many lookup tables, this can be a huge performance improvement. Let's say you have 5 stores, 200 products and 5 product_types

s = Store.find 1, :include=>:products s.products.each { |p| puts "#{p.name} is of type #{p.product_type.name}" }

Doing a puts is obviously a contrived example, but I often traverse model relationships like this in my views. Before this feature, these two lines would have produced 201 database queries Now with Query Caching this will be between 2 and 6 queries. You might think that you could just add a :include => :product_type in your product model. This works while you're loading products, but not if you've already included products in store. In other words, it appears that ActiveRecord can only do an :include one level deep This is a great way to keep from having to roll your own solution or monkey patch ActiveRecord, I look forward to this feature moving from edge to production.
One of the problems that I have with my computers is keeping the settings the same across all of them. I have my desktop, laptop, and work computer and would like to keep things like my bookmarks the same across all of them. I have been using del.icio.us to keep all my bookmarks and the firefox live bookmark feature to access them on each computer. While is is better than nothing, it leaves much to be desired. I just ran into a tool from Google that should make the whole process much better. It's called Google Browser Sync and it seems to solve many of the issues. This is a firefox plugin that you install on each of your computers and it uses your google account to keep certain things synchronized between each of your computers. At this time they support
  • bookmarks - This is the main thing I wanted
  • history - Some people might find this useful
  • cookies - For all the bad rap that cookies get, this is a great feature for web sites that recognize you like Amazon
  • tabs - This is really cool. I could have tabs open on my home desktop, take my laptop to a internet cafe and open firefox to the same exact web site I was working on at home.
  • saved passwords - This one really scares me, Do we really trust google so blindly? Sure, don't be evil, but...
This is a great start, what would be an incredible enhancement would be the ability to synchronize the installation of other plugins as well as the plugin configurations. All firefox users know that it's power comes from having lots of plugins installed. It is difficult to have the same plugins installed and configured on all my instances of firefox, so this would be a wonderful addition.