<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
    <channel>
        <title>Andrew Avenoso - Blogs at Near Infinity</title>


        <link>http://www.nearinfinity.com/blogs/</link>
        <description>Employee Blogs</description>
        <language>en</language>
        <copyright>Copyright 2011</copyright>
        <lastBuildDate>Mon, 09 Aug 2010 12:30:00 -0500</lastBuildDate>
        <generator>http://www.sixapart.com/movabletype/</generator>
        <docs>http://www.rssboard.org/rss-specification</docs>
        
        <item>
            <title>Session Storage in JRuby on Rails</title>
            <description><![CDATA[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.<br /><br /><h2>First... The implementation</h2>
This is assuming that you are running JRuby and using a tool like warbler to deploy to a Java server like Tomcat.<br /><br />config/initializers/session_store.rb<br />
<pre class="prettyprint"># 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<br />    # setup cookie based session<br />    ActionController::Base.session = {<br />        :key    =&gt; '_dd_session',<br />        :secret =&gt; '&lt;Your really long random secret key&gt;'<br />    }<br />end
</pre>This will store your rails session in Tomcat's session store exactly as if you were running a Java web framework.<br />
<br /><h2>Second... Why?</h2>There are a few common solutions starting with the rails default.<br /><br /><b>Cookie Store:</b> 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.<br /><b>File Store:</b> 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.<br /><b>ActiveRecord:</b> Stores sessions in the database. Slow due to database IO. Natively supports clustering.<br /><b>Memcached:</b> 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.<br /><b>Java Servlet Store:</b> Works just like your other Java applications. Java Servers can be configured to cluster sessions. Uses the JSESSIONID cookie or url param (in Tomcat)<br /><br /><h2>Conclusion</h2>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.<br />]]></description>
            <link>http://www.nearinfinity.com/blogs/andrew_avenoso/session_storage_in_jruby_on_ra.html</link>
            <guid>http://www.nearinfinity.com/blogs/andrew_avenoso/session_storage_in_jruby_on_ra.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">JRuby</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Java</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Ruby</category>
            
                <category domain="http://www.sixapart.com/ns/types#category">Web Development</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">jruby</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">ruby</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">session</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">tomcat</category>
            
            <pubDate>Mon, 09 Aug 2010 12:30:00 -0500</pubDate>
        </item>
        
        <item>
            <title>Rails edge finally supports Query Caching</title>
            <description><![CDATA[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<br/>
<code class="prettyprint">
p = Person.find 1
p = Person.find 1
</code><br/><br/>
produces the following logging<br/>
<code class="prettyprint">
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)
</code><br/><br/>

If you freeze to the Rails Edge the previous code will now produce this<br/>
<code class="prettyprint">
Person Load (0.010000)?[0m   ?[0;1mSELECT * FROM people WHERE (people.`id` = 1)
CACHE (0.000000)?[0m   ?[0mSELECT * FROM people WHERE (people.`id` = 1)
</code><br/><br/>
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
<p>If you want to see this work on the console you can use the cache method </p>
<code class="prettyprint">
Person.cache do
&nbsp;&nbsp;p = Person.find 1
&nbsp;&nbsp;p = Person.find 1
end
</code><br/><br/>

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
<code class="prettyprint">
<br /><br />s = Store.find 1, :include=&gt;:products
s.products.each { |p| puts "#{p.name} is of type #{p.product_type.name}" }<br /><br /></code>
Doing a <code class="prettyprint">puts</code> 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 <code class="prettyprint">:include =&gt; :product_type</code> 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 <code class="prettyprint">:include</code> 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.]]></description>
            <link>http://www.nearinfinity.com/blogs/andrew_avenoso/rails_edge_finally_supports_query.html</link>
            <guid>http://www.nearinfinity.com/blogs/andrew_avenoso/rails_edge_finally_supports_query.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">Ruby</category>
            
            
                <category domain="http://www.sixapart.com/ns/types#tag">ActiveRecord</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">caching</category>
            
                <category domain="http://www.sixapart.com/ns/types#tag">database</category>
            
            <pubDate>Fri, 20 Apr 2007 13:26:00 -0500</pubDate>
        </item>
        
        <item>
            <title>Browser settings across multiple computers</title>
            <description><![CDATA[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 <a href="http://del.icio.us">del.icio.us</a> to keep all my bookmarks and the <a href="http://www.mozilla.com/firefox/">firefox</a> 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 <a href="http://www.google.com">Google</a> that should make the whole process much better.  It's called <a href="http://www.google.com/tools/firefox/browsersync/">Google Browser Sync</a> and it seems to solve many of the issues.

This is a <a href="http://www.mozilla.com/firefox/">firefox</a> 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<ul><li>bookmarks - This is the main thing I wanted</li>
<li>history - Some people might find this useful</li>
<li>cookies - For all the bad rap that cookies get, this is a great feature for web sites that recognize you like <a href="http://www.amazon.com">Amazon</a></li>
<li>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.</li>
<li>saved passwords - This one really scares me, Do we really trust google so blindly?  Sure, don't be evil, but...</li>
</ul>

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 <a href="http://www.mozilla.com/firefox/">firefox</a> 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 <a href="http://www.mozilla.com/firefox/">firefox</a>, so this would be a wonderful addition.]]></description>
            <link>http://www.nearinfinity.com/blogs/andrew_avenoso/browser_settings_across_multiple_computers.html</link>
            <guid>http://www.nearinfinity.com/blogs/andrew_avenoso/browser_settings_across_multiple_computers.html</guid>
            
                <category domain="http://www.sixapart.com/ns/types#category">General</category>
            
            
            <pubDate>Thu, 22 Jun 2006 12:07:52 -0500</pubDate>
        </item>
        
    </channel>
</rss>

