From bc836e5a1c58bd80ac59ff0a4616cb976e3c16d7 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Fri, 27 Dec 2024 16:45:19 +0800 Subject: [PATCH] fixed user cache --- app/controllers/application_controller.rb | 4 +- app/controllers/concerns/laboratory_helper.rb | 4 +- app/controllers/users_controller.rb | 4 +- app/models/token.rb | 40 ++++++++++--------- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ea050ebe5..3292c3665 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -379,7 +379,9 @@ class ApplicationController < ActionController::Base uid_logger("user setup start: autologin_user is #{autologin_user}") # 多浏览器退出账号时,token不存在处理 if current_domain_session && autologin_user.nil? - autologin_user = (User.active.find(current_domain_session) rescue nil) + autologin_user = Rails.cache.fetch("ApplicationController::find_current_user:#{current_domain_session}", expires_in: 30.minutes) do + (User.active.find(current_domain_session) rescue nil) + end set_autologin_cookie(autologin_user) if autologin_user.present? end autologin_user diff --git a/app/controllers/concerns/laboratory_helper.rb b/app/controllers/concerns/laboratory_helper.rb index ea6e00558..33df4e4cf 100644 --- a/app/controllers/concerns/laboratory_helper.rb +++ b/app/controllers/concerns/laboratory_helper.rb @@ -27,7 +27,9 @@ module LaboratoryHelper end def default_yun_session - laboratory ||= (Laboratory.find_by_subdomain(request.subdomain) || Laboratory.find(1)) + laboratory ||= Rails.cache.fetch("LaboratoryHelper::default_yun_session:#{request.subdomain}:1") do + (Laboratory.find_by_subdomain(request.subdomain) || Laboratory.find(1)) + end @_default_yun_session = "#{laboratory.try(:identifier).split('.').first}_user_id" end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 56002c08c..b3d94e9da 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -725,7 +725,9 @@ class UsersController < ApplicationController private def load_user - @user = User.find_by_login(params[:id]) || User.find_by(id: params[:id]) + @user = Rails.cache.fetch("UsersControllers::load_user:#{params[:id]}") do + User.find_by_login(params[:id]) || User.find_by(id: params[:id]) + end clear_user_cookie unless @user.present? end diff --git a/app/models/token.rb b/app/models/token.rb index 9b9ea5895..38da0ae5a 100644 --- a/app/models/token.rb +++ b/app/models/token.rb @@ -1,19 +1,19 @@ -# == Schema Information -# -# Table name: tokens -# -# id :integer not null, primary key -# user_id :integer default("0"), not null -# action :string(30) default(""), not null -# value :string(40) default(""), not null -# created_on :datetime not null -# -# Indexes -# -# index_tokens_on_user_id (user_id) -# tokens_value (value) UNIQUE -# - +# == Schema Information +# +# Table name: tokens +# +# id :integer not null, primary key +# user_id :integer default("0"), not null +# action :string(30) default(""), not null +# value :string(40) default(""), not null +# created_on :datetime not null +# +# Indexes +# +# index_tokens_on_user_id (user_id) +# tokens_value (value) UNIQUE +# + # @@ -50,13 +50,17 @@ class Token < ActiveRecord::Base token = Token.create(:user => user, :action => type) Rails.logger.info "###### Token.get_token_from_user is nul and agine create token: #{token&.value}" else - token.update_attribute(:created_on, Time.now) + Rails.cache.fetch("user::update::created_on::#{user.id}",:expires_in => 10.minutes) do + token.update_attribute(:created_on, Time.now) + end end token end def self.get_token_from_user(user, action) - token = Token.where(:action => action, :user_id => user).first + token = Rails.cache.fetch("Token::get_token_from_user:#{user.id}:1", :expires_in => 10.minutes) do + Token.where(:action => action, :user_id => user).first + end Rails.logger.info "###### self.get_token_from_user query result: #{token&.value}" unless token token = Token.create!(user_id: user.id, action: action)