Adjust cache.

This commit is contained in:
BoHung Chiu 2022-11-24 23:43:17 +08:00
parent 3ac3d4c7ef
commit 0495a4af16
2 changed files with 18 additions and 5 deletions

View File

@ -50,11 +50,17 @@ class RulingWeatherSetting
"locationName" => custom_location} "locationName" => custom_location}
timeout_flag = false timeout_flag = false
begin begin
cahche_model = self.ruling_weather_cache cache_model = self.ruling_weather_cache
if cahche_model.nil? if cache_model.nil?
cahche_model = RulingWeatherCache.create(:cache=>{},:ruling_weather_setting=>self) cache_model = RulingWeatherCache.create(:cache=>{},:ruling_weather_setting=>self)
elsif (cache_model.cache_time.to_i / 3600) == (time_now.to_i / 3600) #The same hour
result = cache_model.cache
if result["uvi_text"].blank?
result["uvi_text"] = I18n.t("ruling_weather.none")
end end
cache = cahche_model.cache rescue {} return result
end
cache = cache_model.cache rescue {}
res = net_http_get_response(URI.parse("#{url}?#{data.to_query}")) res = net_http_get_response(URI.parse("#{url}?#{data.to_query}"))
content = JSON.parse(res.body) content = JSON.parse(res.body)
weather_data = get_weather_data(content) rescue {} weather_data = get_weather_data(content) rescue {}
@ -132,7 +138,7 @@ class RulingWeatherSetting
"wx_svg" => wx_svg, "wx_svg" => wx_svg,
"ws" => ws, "ws" => ws,
"rain" => rain} "rain" => rain}
cahche_model.update(:cache=>result,:cache_time=>time_now) cache_model.update(:cache=>result,:cache_time=>time_now)
if uvi_text.blank? if uvi_text.blank?
result["uvi_text"] = I18n.t("ruling_weather.none") result["uvi_text"] = I18n.t("ruling_weather.none")
end end

View File

@ -7,6 +7,13 @@ module RulingWeather
authorizable authorizable
widget_methods ["widget"] widget_methods ["widget"]
widget_settings [{"override_category_with"=>"ruling_weather_setting","multiselect"=>false,"display_field"=>"location"}] widget_settings [{"override_category_with"=>"ruling_weather_setting","multiselect"=>false,"display_field"=>"location"}]
if File.basename($0) != 'rake'
begin
avoid_page_cache RulingWeatherCache
rescue => e
puts ["avoid_page_cache", e.to_s]
end
end
side_bar do side_bar do
head_label_i18n 'ruling_weather.ruling_weather', icon_class: "fa fa-sun-o" head_label_i18n 'ruling_weather.ruling_weather', icon_class: "fa fa-sun-o"
available_for "users" available_for "users"