Em 07-02-2013 16:43, David MacMahon escreveu:
>> ...An extra parse operation on a big list could easily add 100ms to the request timing.
> So use explicit initialization instead of lazy initialization.

Sorry, didn't get. Could you please show some sample code?

>> Also, it doesn't happen only the first time but on each request.
> In the original example, JSON.parse was used on every call except the first one.  The modified example uses JSON.parse on every call including the first one.  That's why I said the modified example has only one extra JSON.parse call (i.e. the one extra one on the first call).  If that's too much overhead, either don't use lazy initialization or explicitly invoke the method once at startup to force lazy initialization so it doesn't impact the (un?)lucky first user.

I see the confusion. I simplified the code in that example. Here is how 
it would look in a real Rails controller:

class MyController

   def my_action
      @users = if cached = CacheStore.fetch('users')
        JSON.parse cached
      else
        DB[:users].select(:id, :name).all.tap{|u| CacheStore.store 
'users', JSON.unparse u}
      end
   end
end

Of course I don't cache the users list, this is just a small example. 
The real query is much more complex and could take up to a second when 
lots (20) of fields are searched using the user interface. Usually the 
query would take about 100ms or less (up to 5 fields usually) but then 
the user may want to print the results or export to Excel or changing to 
another page and caching would help a lot in that case.