Everyone should be using low level caching in Rails
robotmay.com
robotmay.com
For Memcache users, configure Rails.cache to use Rails' built in :memcache_store:
http://api.rubyonrails.org/classes/ActiveSupport/Cache/MemCa...
For Redis fans, try :redis_store:
https://github.com/jodosha/redis-store
This covers Rails 3 caching pretty well:
http://broadcastingadam.com/2011/05/advanced_caching_in_rail...
Note for Rails 2: If I recall correctly, in Rails 2 fragment caching/action caching still uses the filesystem even if you have a cache_store set.
Most of that info is in the readme, btw.
I think everyone should be caching as the speed of your page loads is fast becoming an even more important factor in people using/enjoying your site. Everyone (including myself) is getting more impatient in regards to page load speed, and every little helps. Also, you never know when you'll hit the frontpage on HN!
http://api.rubyonrails.org/classes/ActiveRecord/IdentityMap....
https://github.com/rails/rails/pull/5261
Though there is now a company volunteering to fund completion of the feature, starting Real Soon Now:
https://github.com/rails/rails/issues/5442
IdentityMap would, obviously, be great. Though I didn't realize it could serve as a replacement for model caching; didn't IdentityMap, like the query cache, only last for the lifetime of a request? Or could it be persisted in the Rails.cache of your choice for an arbitrary lifespan?
IdentityMap will be extremely useful for the majority of sites since they have very low traffic, processor, and memory demands, and therefore it's unlikely you'll have too many processes running using up their own memory containers. Though you could argue these sites really don't need additional caching anyways (they'd still respond slightly faster with IdentityMap).
For high traffic sites it'll still be useful to save some trips, but you'll also want to implement and manage a distributed cache (for models, actions, fragments, etc).
It's available and sort of works now. You just have to turn it on and be mindful of how associations are handled. Still a work in progress.
However its biggest drawback is that its hard-wired to use memcached.
It would be great if the actual cache backend was pluggable and we could develop a redis adapter. As it stands the memcached integration is pretty deeply intertwined into the cache-money library.
Use "if" and "present?" instead of "unless" and "blank?". It's much easier to parse mentally, especially for people who didn't write the code. :)
You don't need to use "self" to access latitude, longitude, updated_at, etc. You only need to use it for assignment.
You don't need to call #to_s on the captured exception. When you interpolate an object into a string, Ruby calls #to_s on the supplied object itself, e.g., "I am an #{Object}".
I'd probably also add a "has_posts" scope to Category and use that instead of the #where call in ApplicationController.
I'll keep that in mind for the future; it's a bad habit of mine :D
I actually didn't realise you only needed that for assignment. That's going to neaten up my code somewhat.
I normally don't put .to_s in strings like that, so I'm not entirely sure why I did in that line.
Aye, I'd normally use a scope but I figured I'd write it out more verbosely just for clarity :)