Prefetcher
This gem provides a simple-to-use interface to work with frequently requested http requests from your api. It gets request response from memory, if possible. But also this means you have to update this cache from time to time (using whenever, for example). Any kind of non-200 responses will not be memoized, so you can be always sure that you don't use broken data. Redis is used to store data. RDoc
Installation
Add this line to your application's Gemfile:
gem 'prefetcher'
And then execute:
$ bundle
Or install it yourself as:
$ gem install prefetcher
You can also override redis connection details (if not using default localhost:6379 ):
Prefetcher.redis_connection = Redis.new(:host => "10.0.1.1", :port => 6380, :db => 15)
See redis gem documentation for more options when creating redis connection.
Usage
Using cached requests
After installing project you can request any URL:
Prefetcher::HttpPrefetcher.new('http://www.reddit.com/r/ruby').get
Calling #get any number of times will return data from cache.
Force get
If you want to force request (and save the response), you can call #fetch:
Prefetcher::HttpPrefetcher.new('http://www.reddit.com/r/ruby').fetch
This will cause actual http request.
Updating cache
Calling manualy. You can call Prefetcher.update_all to fetch all URLs right now.
You can also automate this call using whenever. Just add this code to your schedule.rb .
every 30.minutes do
runner "Prefetcher.update_all"
end%
Contributing
- Fork it ( https://github.com/brain-geek/prefetcher/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request