Cache rubygems API response

This commit is contained in:
uu59 2014-05-15 18:11:00 +09:00
parent 176b7771bd
commit 321a310e95
5 changed files with 12 additions and 6 deletions

View File

@ -74,9 +74,12 @@ class Plugin
def latest_version
@latest_version ||=
begin
res = HTTPClient.get("https://rubygems.org/api/v1/versions/#{gem_name}.json")
if res.code == 200
JSON.parse(res.body).map {|ver| Gem::Version.new ver["number"] }.max.to_s
url = "https://rubygems.org/api/v1/versions/#{gem_name}.json"
Rails.cache.fetch(url, expires_in: 10.minutes) do # NOTE: 10.minutes could be changed if it doesn't fit
res = HTTPClient.get(url)
if res.code == 200
JSON.parse(res.body).map {|ver| Gem::Version.new ver["number"] }.max.to_s
end
end
end
end

View File

@ -1,5 +1,5 @@
- @plugins.each do |plugin|
/ TODO: make this page faster, such as do async, cache API response, etc
/ TODO: make this page faster, such as do async, etc
%p
= plugin.gem_name
- if plugin.installed?

View File

@ -22,6 +22,8 @@ Rails.application.configure do
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
config.cache_store = :memory_store, { size: 16 * 1024 * 1024 } # NOTE: `16.megabytes` will be undefined method error at here
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.

View File

@ -36,8 +36,8 @@ Rails.application.configure do
# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production.
# config.cache_store = :mem_cache_store
# cache for rubygems.org response
config.cache_store = :memory_store, { size: 16 * 1024 * 1024 } # NOTE: `16.megabytes` will be undefined method error at here
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = "http://assets.example.com"

View File

@ -19,6 +19,7 @@ Rails.application.configure do
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.cache_store = :null_store
# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false