ボクココ

個人開発に関するテックブログ

仕事で学んだイカしたRailsテクニック

毎回同じDBアクセスの場合キャッシュしたくなりますよね。
今日はちょっと感動したコードを紹介。
Rails高速化にはこういうテクニックが必要ですなぁ。


class Area < ActiveRecord::Base

@@cache = nil

class << self
def find_default_area()
unless @@cache
@@cache = find(:all,:conditions....)
end
return @@cache
end
end

end

クラス変数にこういう形で使うとはなるほど、と思いました。