In a nutshell cached_resource automagically caches ActiveResource response objects in a memcached instance. The plugin requires cache_fu for memcached configuration goodness. See also: Kickin’ ass with cache-fu.
The code for cached_resource is also up on GitHub along with a many other Los Angeles Times plugins and other tidbits. You can find it here: http://github.com/latimes/cached_resource/tree/master
So, how do you get it to work? After dropping the plugins in vendor/plugins, set up your cache_fu configuration then drop this single line in any of your ActiveResource models:
class MyClass < ActiveResource::Base cached_resource end
The cached_resource call also takes normal cache_fu options:
class MyClass < ActiveResource::Base cached_resource :ttl => 15.minutes end
So now every time you run a .find() on MyClass the results will be cached for the next time you do the same kind of find. The cache key is the URI of the ActiveRecord generated REST query.
In addition, if you do MyClass.find(:all) or the like to get a list of resources, individual instances in the returned list will be cached separately and the list itself will be distilled down to IDs before it’s stuffed in its own cache bucket. When pulled from the cache the list will be reconstituted by pulling the individual objects from the cache by ID or making additional ActiveResource queries to get objects that have fallen out for one reason or another. The upshot of this is you’ll have less of a chance of stale data between mixed finds.
The cached_resource plugin makes some assumptions about how your REST service is set up. If the REST service you’re pointing to with ActiveResource are wacky, i.e. not conforming to exactly what ActiveResource expects its URLs to be, this plugin will probably not work for you. That said, if you can do normal finds with IDs, like MyClass.find(123), you’ll probably be fine.
This plugin was written before memcached support was baked into Rails 2.1 so if anyone wants to fork and get it working please be my guest! I’ll be happy to pull it back in
Credit where credit is due: Josh Kleinpeter originally came up with the cached_resource concept and Subba Rao helped to lay the groundwork.