add cache to edusetting get

dev_course
p31729568 6 years ago
parent 09d8e6e5da
commit 34dc1032d8

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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')

Loading…
Cancel
Save