From 34dc1032d83ed071d695dc27903efb10b2132207 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Sat, 22 Jun 2019 10:45:54 +0800 Subject: [PATCH] add cache to edusetting get --- app/controllers/application_controller.rb | 4 ++-- app/helpers/application_helper.rb | 2 +- app/libs/base64_image_converter.rb | 2 +- app/models/edu_setting.rb | 22 +++++++++++++++++++++- app/services/git_service.rb | 2 +- app/services/review_service.rb | 2 +- lib/educoder/sms.rb | 2 +- 7 files changed, 28 insertions(+), 8 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d1aac6f3a..89b3ed5d8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -19,8 +19,8 @@ class ApplicationController < ActionController::Base # 全局配置参数 # 返回name对应的value - def edu_setting name - EduSetting.find_by_name(name).try(:value) + def edu_setting(name) + EduSetting.get(name) end def user_course_identity diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 42bc16343..b4e3baf51 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -11,7 +11,7 @@ module ApplicationHelper # 全局参数配置 def edu_setting name - EduSetting.find_by_name(name).try(:value) + EduSetting.get(name) end def graduation_navigation graduation diff --git a/app/libs/base64_image_converter.rb b/app/libs/base64_image_converter.rb index 2a8155283..2dc9aa41e 100644 --- a/app/libs/base64_image_converter.rb +++ b/app/libs/base64_image_converter.rb @@ -35,7 +35,7 @@ class Base64ImageConverter end def size_limit - EduSetting.find_by_name('upload_avatar_max_size')&.value + EduSetting.get('upload_avatar_max_size') end class Image diff --git a/app/models/edu_setting.rb b/app/models/edu_setting.rb index db6d545f0..e6e4d900e 100644 --- a/app/models/edu_setting.rb +++ b/app/models/edu_setting.rb @@ -1,5 +1,25 @@ class EduSetting < ApplicationRecord + after_commit :expire_cache + + def cache_key + self.class.cache_key(name) + end + def self.get(key) - find_by_name(key.to_s)&.value + Rails.cache.fetch(cache_key(key), expires_in: 1.days) do + find_by_name(key.to_s)&.value + end + end + + def self.cache_key(name) + raise ArgumentError if name.blank? + + "educoder/edu-settings/#{name.to_s}" + end + + private + + def expire_cache + Rails.cache.clear(cache_key) end end diff --git a/app/services/git_service.rb b/app/services/git_service.rb index 4b6bf17ef..3298185bf 100644 --- a/app/services/git_service.rb +++ b/app/services/git_service.rb @@ -15,7 +15,7 @@ class GitService private def root_url - new_git_address = EduSetting.find_by_name('git_address_domain').try(:value) + new_git_address = EduSetting.get('git_address_domain') raise 'error: new_git_address not configuration' unless new_git_address.present? new_git_address end diff --git a/app/services/review_service.rb b/app/services/review_service.rb index 7602aa51b..df004fd19 100644 --- a/app/services/review_service.rb +++ b/app/services/review_service.rb @@ -4,7 +4,7 @@ require 'net/http' require 'uri' class ReviewService - @review_server_url = EduSetting.find_by_name('review_server_url').try(:value) + @review_server_url = EduSetting.get('review_server_url') def self.logger diff --git a/lib/educoder/sms.rb b/lib/educoder/sms.rb index 9d54a6df5..e79678ad3 100644 --- a/lib/educoder/sms.rb +++ b/lib/educoder/sms.rb @@ -27,7 +27,7 @@ module Educoder def self.sendYunpian(mobile, code, send_type, name, user_name, result) #修改为您的apikey.可在官网(http://www.yunpian.com)登录后用户中心首页看到 - apikey = EduSetting.find_by_name('sms_apikey').try(:value) + apikey = EduSetting.get('sms_apikey') #指定模板发送接口HTTP地址 send_tpl_sms_uri = URI.parse('https://sms.yunpian.com/v2/sms/single_send.json')