fixed 查询优化,缓存,竞赛日志

This commit is contained in:
xxq250 2025-10-15 15:10:34 +08:00
parent c029666ffe
commit 798e8810bf
7 changed files with 27 additions and 7 deletions

View File

@ -45,6 +45,8 @@ gem 'redcarpet', '~> 3.4'
gem 'rqrcode', '~> 0.10.1'
gem 'rqrcode_png'
# 动作缓存
gem 'actionpack-action_caching'
gem 'acts-as-taggable-on', '~> 6.0'

View File

@ -3,7 +3,14 @@ class Admins::UserActionsController < Admins::BaseController
def index
@user_actions = UserAction.order(created_at: :desc)
@user_actions = @user_actions.where(action_type: params[:action_type]) if params[:action_type].present?
if params[:action_type].present?
if params[:action_type].include?("CompetitionInfo")
@user_actions = @user_actions.where(action_type: ["CompetitionInfoCreate", "CompetitionInfoUpdate", "CompetitionInfoUp", "CompetitionInfoDown"])
else
@user_actions = @user_actions.where(action_type: params[:action_type])
end
end
keyword = params[:keyword].to_s.strip.presence
if keyword
sql = 'login LIKE :keyword OR phone LIKE :keyword OR email LIKE :keyword'

View File

@ -75,6 +75,7 @@ class CompetitionInfosController < ApplicationController
respond_to do |format|
if @competition_info.save
UserAction.create(:action_id => @competition_info.id, :action_type => "CompetitionInfoCreate", :user_id => current_user.id,:ip => request.remote_ip, memo: "#{current_user.nickname}创建了竞赛")
format.html { redirect_to competition_infos_path, notice: '创建成功.' }
format.json { render_ok(data: @competition_info.as_json) }
else
@ -90,6 +91,7 @@ class CompetitionInfosController < ApplicationController
respond_to do |format|
@competition_info.admin_data = params[:admin_data] if params[:admin_data].present?
if @competition_info.update!(competition_info_params)
UserAction.create(:action_id => @competition_info.id, :action_type => "CompetitionInfoUpdate", :user_id => current_user.id,:ip => request.remote_ip, memo: "#{current_user.nickname}创建了竞赛")
format.html { redirect_to competition_infos_path, notice: '更新成功.' }
format.json { render_ok(data: @competition_info.as_json) }
else
@ -102,8 +104,10 @@ class CompetitionInfosController < ApplicationController
def online_switch
if @competition_info.status
@competition_info.update_attributes!(status: false)
UserAction.create(:action_id => @competition_info.id, :action_type => "CompetitionInfoDown", :user_id => current_user.id,:ip => request.remote_ip, memo: "#{current_user.nickname}下架了竞赛")
else
@competition_info.update_attributes!(status: true)
UserAction.create(:action_id => @competition_info.id, :action_type => "CompetitionInfoUp", :user_id => current_user.id,:ip => request.remote_ip, memo: "#{current_user.nickname}上架了竞赛")
end
render_ok
end

View File

@ -10,6 +10,7 @@ class ProjectsController < ApplicationController
before_action :authorizate_user_can_edit_project!, only: %i[update]
before_action :project_public?, only: %i[fork_users praise_users watch_users]
# before_action :request_limit, only: %i[index]
#caches_action :index, :expires_in => 10.minutes, :cache_path => Proc.new{"/api/projects?limit=#{params[:limit]}&page=#{params[:page]}&search=#{params[:search]}"}
def menu_list
menu = []
@ -37,12 +38,12 @@ class ProjectsController < ApplicationController
recommend_scope = current_user.logged? ? Projects::ListQuery.call(params.merge(recommend: "true"), current_user.id) : Projects::ListQuery.call(params.merge(recommend: "true"))
forked_scope = current_user.logged? ? Projects::ListQuery.call(params.merge(exclude_forked: "false"), current_user.id) : Projects::ListQuery.call(params.merge(exclude_forked: "false"))
scope = Project.from("( select * from ( #{ recommend_scope.reorder("recommend_index desc").to_sql} limit 10000000 ) as t1 UNION select * from ( #{ scope.to_sql} limit 10000000 ) as t2 UNION select * from ( #{ forked_scope.to_sql} limit 10000000 ) as t3 ) AS projects")
@projects = kaminari_paginate(scope.includes(:project_category, :project_language, :repository, :project_educoder, :owner, :project_units))
@projects = kaminari_paginate(scope)
# @projects = paginate scope.includes(:project_category, :project_language, :repository, :project_educoder, :owner, :project_units)
# @rand_projects = params[:page].to_i == 1 ? ([Project.find_by(id: 477)] | @projects) - [nil] : @projects
@rand_projects = @projects
category_id = params[:category_id]
@total_count =
@total_count = Rails.cache.fetch("ProjectsControllers::index::total_count", expires_in: 10.minutes) do
if category_id.blank? && params[:search].blank? && params[:topic_id].blank? && params[:topic_name].blank?
# 默认查询时count性能问题处理
not_category_count = Project.where(project_category_id: nil).count
@ -56,6 +57,7 @@ class ProjectsController < ApplicationController
cate = ProjectCategory.find_by(id: category_id)
cate&.projects_count || 0
end
end
end
def create

View File

@ -50,16 +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
unless Rails.cache.read("token::update::created_on::#{token.id}").present?
Rails.cache.fetch("user::update::created_on::#{user.id}",:expires_in => 10.minutes) do
token.update_attribute(:created_on, Time.now)
Rails.cache.write("token::update::created_on::#{token.id}", true, expires_in: 5.minutes)
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)

View File

@ -38,6 +38,10 @@ class UserAction < ApplicationRecord
when "Login" then "登录"
when "Logout" then "退出登录"
when "DestroyOrganization" then "删除组织"
when "CompetitionInfoCreate" then "创建竞赛"
when "CompetitionInfoUpdate" then "修改竞赛"
when "CompetitionInfoUp" then "上架竞赛"
when "CompetitionInfoDown" then "下架竞赛"
else self.action_type
end
end

View File

@ -5,7 +5,7 @@
<div class="box search-form-container user-list-form">
<%= form_tag(admins_user_actions_path, method: :get, class: 'form-inline search-form flex-1', remote: true) do %>
操作类型:
<% action_type_options = [['自定义',''],['删除用户','DestroyUser'], ['删除项目', 'DestroyProject'], ['删除组织', 'DestroyOrganization']] %>
<% action_type_options = [['自定义',''],['删除用户','DestroyUser'], ['删除项目', 'DestroyProject'], ['删除组织', 'DestroyOrganization'], ['竞赛', 'CompetitionInfo']] %>
<%= select_tag(:action_type_select, options_for_select(action_type_options), class: 'form-control') %>
<%= text_field_tag(:action_type, params[:action_type], class: 'form-control col-sm-2 ml-3',style: 'display:;', placeholder: '自定义操作类型检索') %>
<%= text_field_tag(:keyword, params[:keyword], class: 'form-control col-sm-2 ml-3', placeholder: '操作人用户名/邮箱/手机号检索') %>