lib/app_manager/model.rb in app_manager-1.2.2 vs lib/app_manager/model.rb in app_manager-1.2.3

- old
+ new

@@ -1,10 +1,11 @@ module AppManager module Model extend ActiveSupport::Concern - + @@remaining_cache_key = "get_remaining_days_key" + @@get_charge_key = "get_charge_key" def has_plan if !self[AppManager.configuration.plan_id_or_name_field] return false; end @@ -14,18 +15,19 @@ plan_id = self[AppManager.configuration.plan_id_or_name_field] if !plan_id Rails.logger.info "Plan id found nil or not set" return false; end - remaining_days = self.get_remaining_days + remaining_days = fetch_static_remaining_days if remaining_days > 0 return true end - plan_obj = AppManager::Client.new - shop_domain = self[AppManager.configuration.shopify_domain_field] - active_charge = plan_obj.get_charge(shop_domain) rescue nil - return active_charge['active_charge'].present? && !active_charge['active_charge'].nil? ? true : false + # plan_obj = AppManager::Client.new + # shop_domain = self[AppManager.configuration.shopify_domain_field] + # active_charge = plan_obj.get_charge(shop_domain) rescue nil + active_charge = fetch_static_get_charge + return active_charge && active_charge['active_charge'].present? && !active_charge['active_charge'].nil? ? true : false end def plan_features @@ -191,9 +193,43 @@ else return [] end end + + + def fetch_static_remaining_days + @remaining_cache_key = "app-manager-cache/#{self.shopify_domain}-remaining_days" + if fetch_cached_static_remaining_days(@remaining_cache_key).present? + return fetch_cached_static_remaining_days(@remaining_cache_key) + end + remaining_days = self.get_remaining_days + Rails.cache.write(@remaining_cache_key, remaining_days, expires_in: AppManager.configuration.expires_in) + return remaining_days + end + + def fetch_cached_static_remaining_days(cache_key) + if Rails.cache.read(cache_key).present? + return Rails.cache.read(cache_key) + end + end + + + def fetch_static_get_charge + @get_charge_key = "app-manager-cache/#{self.shopify_domain}-get_charge" + if fetch_cached_static_get_charge(@get_charge_key).present? + return fetch_cached_static_get_charge(@get_charge_key) + end + charge = self.get_charge + Rails.cache.write(@get_charge_key, charge, expires_in: AppManager.configuration.expires_in) + return charge + end + + def fetch_cached_static_get_charge(cache_key) + if Rails.cache.read(cache_key).present? + return Rails.cache.read(cache_key) + end + end private def casted_value(value,value_type) \ No newline at end of file