CachedResource is a Ruby gem whose goal is to increase the performance of interacting with web services via ActiveResource by caching responses based on request parameters. It can help reduce the lag created by making repeated requests across a network.
gem install cached_resource
Enable CachedResource across all ActiveResources. You could put this code in config/initializers.
class ActiveResource::Base cached_resource end
Enable CachedResource for a single class.
class MyActiveResource < ActiveResource::Base cached_resource end
CachedResource accepts the following options:
:cacheThe cache store that CacheResource should use. Default: The
Rails.cacheif available, or an
:ttlThe time in seconds until the cache should expire. Default:
:loggerThe logger to which CachedResource messages should be written. Default: The
Rails.loggerif available, or an
You can set them like this:
cached_resource :cache => MyCacheStore.new, :ttl => 60, :logger => MyLogger.new, :enabled => false
You can also change these options on the fly.
Turn CachedResource off. This will cause all responses to be retrieved normally (i.e. via the network).
Turn CachedResource on.
Set the cache expiry time to 60 seconds.
MyActiveResource.cached_resource.ttl = 60
Set a different logger.
MyActiveResource.cached_resource.logger = MyLogger.new
Set a different cache store.
MyActiveResource.cached_resource.cache = MyCacheStore.new
Sit back and relax! If you need to reload a particular request you can do something like this:
MyActiveResource.find(:all, :reload => true)
- Cached collection lookups