diff --git a/app/models/plugin.rb b/app/models/plugin.rb index 7101539..238d3fa 100644 --- a/app/models/plugin.rb +++ b/app/models/plugin.rb @@ -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 diff --git a/app/views/plugins/index.html.haml b/app/views/plugins/index.html.haml index 398cee2..c4fd547 100644 --- a/app/views/plugins/index.html.haml +++ b/app/views/plugins/index.html.haml @@ -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? diff --git a/config/environments/development.rb b/config/environments/development.rb index ddf0e90..1b493da 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -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. diff --git a/config/environments/production.rb b/config/environments/production.rb index 7099fd9..fdf36ca 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -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" diff --git a/config/environments/test.rb b/config/environments/test.rb index 053f5b6..24515b7 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -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