No description, website, or topics provided.
Switch branches/tags
Pull request Compare This branch is 129 commits behind mhgbrown:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
Gemfile
MIT-LICENSE
README.md
Rakefile
cached_resource.gemspec

README.md

CachedResource Build Status

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.

Installation

gem install cached_resource

Configuration

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

Options

CachedResource accepts the following options:

  • :cache The cache store that CacheResource should use. Default: The Rails.cache if available, or an ActiveSupport::Cache::MemoryStore
  • :ttl The time in seconds until the cache should expire. Default: 604800
  • :logger The logger to which CachedResource messages should be written. Default: The Rails.logger if available, or an ActiveSupport::BufferedLogger
  • :enabled Default: true

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).

MyActiveResource.cached_resource.off!

Turn CachedResource on.

MyActiveResource.cached_resource.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

Usage

Sit back and relax! If you need to reload a particular request you can do something like this:

MyActiveResource.find(:all, :reload => true)

Testing

rake

Credit/Inspiration

Future Work

  • Cached collection lookups