新增: 五个接口走缓存

This commit is contained in:
yystopf 2024-12-26 16:09:17 +08:00
parent c014857cf5
commit 2205261661
5 changed files with 121 additions and 69 deletions

View File

@ -311,40 +311,46 @@ class ProjectsController < ApplicationController
end
def simple
if !@project.common? && @project&.repository&.mirror&.waiting?
gitea_result = $gitea_client.get_repos_by_owner_repo(@project&.owner&.login, @project&.identifier) rescue nil
if gitea_result.present? && !gitea_result["empty"]
@project&.update_columns(gpid: gitea_result["id"])
@project&.repository&.mirror&.succeeded!
project_id = @project&.id
user_id = @project&.owner&.id
Rails.logger.info "############ mirror project_id,user_id: #{project_id},#{user_id} ############"
OpenProjectDevOpsJob.set(wait: 5.seconds).perform_later(project_id, user_id) if project_id.present? && user_id.present?
UpdateProjectTopicJob.set(wait: 1.seconds).perform_later(project_id) if project_id.present?
Rails.logger.info "############ mirror status: #{@project&.repository&.mirror&.status} ############"
cached_data = Rails.cache.read("ProjectsController::simple:#{@project&.owner&.login}:#{@project&.identifier}")
unless cached_data
if !@project.common? && @project&.repository&.mirror&.waiting?
gitea_result = $gitea_client.get_repos_by_owner_repo(@project&.owner&.login, @project&.identifier) rescue nil
if gitea_result.present? && !gitea_result["empty"]
@project&.update_columns(gpid: gitea_result["id"])
@project&.repository&.mirror&.succeeded!
project_id = @project&.id
user_id = @project&.owner&.id
Rails.logger.info "############ mirror project_id,user_id: #{project_id},#{user_id} ############"
OpenProjectDevOpsJob.set(wait: 5.seconds).perform_later(project_id, user_id) if project_id.present? && user_id.present?
UpdateProjectTopicJob.set(wait: 1.seconds).perform_later(project_id) if project_id.present?
Rails.logger.info "############ mirror status: #{@project&.repository&.mirror&.status} ############"
end
elsif !@project.common? && @project&.repository&.mirror&.failed?
# 导入失败的项目标记 project.status=0, 在列表中不显示
@project&.update_columns(status: 0) if @project&.status == 1
# Rails.logger.info "############ mirror status: #{@project&.repository&.mirror&.status}"
# Gitea::Repository::DeleteService.new(@project.owner, @project.identifier,current_user.gitea_token).call
# @project.destroy!
# @project.forked_projects.update_all(forked_from_project_id: nil)
# # 如果该项目有所属的项目分类以及为私有项目,需要更新对应数量
# @project.project_category.decrement!(:private_projects_count, 1) if @project.project_category.present? && !@project.is_public
# return render_error("导入失败,请重试!")
end
elsif !@project.common? && @project&.repository&.mirror&.failed?
# 导入失败的项目标记 project.status=0, 在列表中不显示
@project&.update_columns(status: 0) if @project&.status == 1
# Rails.logger.info "############ mirror status: #{@project&.repository&.mirror&.status}"
# Gitea::Repository::DeleteService.new(@project.owner, @project.identifier,current_user.gitea_token).call
# @project.destroy!
# @project.forked_projects.update_all(forked_from_project_id: nil)
# # 如果该项目有所属的项目分类以及为私有项目,需要更新对应数量
# @project.project_category.decrement!(:private_projects_count, 1) if @project.project_category.present? && !@project.is_public
# return render_error("导入失败,请重试!")
end
# 为了缓存活跃项目的基本信息,后续删除
Cache::V2::ProjectCommonService.new(@project.id).read
# 项目名称,标识,所有者变化时重置缓存
project_common = $redis_cache.hgetall("v2-project-common:#{@project.id}")
if project_common.present?
if project_common["name"] != @project.name || project_common["identifier"] != @project.identifier || project_common["owner_id"] != @project.user_id
Cache::V2::ProjectCommonService.new(@project.id).reset
# 为了缓存活跃项目的基本信息,后续删除
Cache::V2::ProjectCommonService.new(@project.id).read
# 项目名称,标识,所有者变化时重置缓存
project_common = $redis_cache.hgetall("v2-project-common:#{@project.id}")
if project_common.present?
if project_common["name"] != @project.name || project_common["identifier"] != @project.identifier || project_common["owner_id"] != @project.user_id
Cache::V2::ProjectCommonService.new(@project.id).reset
end
end
cached_data = json_response(@project, current_user)
Rails.cache.write("ProjectsController::simple:#{@project&.owner&.login}:#{@project&.identifier}", cached_data, expires_in: 10.minutes)
end
json_response(@project, current_user)
cached_data
end
def recommend

View File

@ -12,21 +12,42 @@ class PullRequestsController < ApplicationController
def index
# @issues = Gitea::PullRequest::ListService.new(@user,@repository.try(:identifier)).call #通过gitea获取
issues = @project.issues.issue_pull_request.issue_index_includes.includes(pull_request: :user)
issues = issues.where(is_private: false) unless current_user.present? && (current_user.admin? || @project.member?(current_user))
@all_issues = issues.distinct
@filter_issues = @all_issues
@filter_issues = @filter_issues.where("issues.subject LIKE ? OR issues.description LIKE ? ", "%#{params[:search]}%", "%#{params[:search]}%") if params[:search].present?
@open_issues = @filter_issues.joins(:pull_request).where(pull_requests: {status: PullRequest::OPEN})
@close_issues = @filter_issues.joins(:pull_request).where(pull_requests: {status: PullRequest::CLOSED})
@merged_issues = @filter_issues.joins(:pull_request).where(pull_requests: {status: PullRequest::MERGED})
@user_admin_or_member = current_user.present? && (current_user.admin || @project.member?(current_user))
@user_admin_or_developer = current_user.present? && (current_user.admin || @project.all_developers.include?(current_user))
scopes = Issues::ListQueryService.call(issues,params.delete_if{|k,v| v.blank?}, "PullRequest")
@issues_size = scopes.size
@issues = paginate(scopes)
cached_data = Rails.cache.read("PullRequestsController:index:#{@project&.owner&.login}:#{@project.identifier}:#{params.to_unsafe_h}:#{current_user.id}")
unless cached_data
# @issues = Gitea::PullRequest::ListService.new(@user,@repository.try(:identifier)).call #通过gitea获取
issues = @project.issues.issue_pull_request.issue_index_includes.includes(pull_request: :user)
issues = issues.where(is_private: false) unless current_user.present? && (current_user.admin? || @project.member?(current_user))
all_issues = issues.distinct
filter_issues = all_issues
filter_issues = filter_issues.where("issues.subject LIKE ? OR issues.description LIKE ? ", "%#{params[:search]}%", "%#{params[:search]}%") if params[:search].present?
open_issues = filter_issues.joins(:pull_request).where(pull_requests: {status: PullRequest::OPEN})
close_issues = filter_issues.joins(:pull_request).where(pull_requests: {status: PullRequest::CLOSED})
merged_issues = filter_issues.joins(:pull_request).where(pull_requests: {status: PullRequest::MERGED})
user_admin_or_member = current_user.present? && (current_user.admin || @project.member?(current_user))
user_admin_or_developer = current_user.present? && (current_user.admin || @project.all_developers.include?(current_user))
issues = Issues::ListQueryService.call(issues,params.delete_if{|k,v| v.blank?}, "PullRequest")
cached_data = {
all_issues: all_issues,
filter_issues: filter_issues,
open_issues: open_issues,
close_issues: close_issues,
merged_issues: merged_issues,
user_admin_or_member: user_admin_or_member,
user_admin_or_developer: user_admin_or_developer,
issues: issues,
}
Rails.cache.write("PullRequestsController:index:#{@project&.owner&.login}:#{@project.identifier}:#{params.to_unsafe_h}:#{current_user.id}", cached_data, expires_in: 10.minutes)
end
@all_issues = cached_data[:all_issues]
@filter_issues = cached_data[:filter_issues]
@open_issues = cached_data[:open_issues]
@close_issues = cached_data[:close_issues]
@merged_issues = cached_data[:merged_issues]
@user_admin_or_member = cached_data[:user_admin_or_member]
@user_admin_or_developer = cached_data[:user_admin_or_developer]
@issues_size = cached_data[:issues].size
@issues = paginate(cached_data[:issues])
end
def new
@ -344,4 +365,8 @@ class PullRequestsController < ApplicationController
def check_menu_authorize
return render_not_found unless @project.has_menu_permission("pulls")
end
def to_query(hash)
hash.map { |key, value| "#{key}=#{value}" }.join('&')
end
end

View File

@ -1,25 +1,33 @@
class Users::OrganizationsController < Users::BaseController
def index
if current_user.logged?
logged_organizations_sql = observed_user.organizations.with_visibility(%w(common limited)).to_sql
privacy_organizations_sql = observed_user.organizations.with_visibility("privacy").joins(:organization_users).where(organization_users: {user_id: current_user.id}).to_sql
@organizations = Organization.from("( #{ logged_organizations_sql } UNION #{ privacy_organizations_sql } ) AS users")
else
@organizations = observed_user.organizations.with_visibility("common")
page = params[:page].to_i.zero? ? 1 : params[:page].to_i
limit = params[:limit] || params[:per_page]
limit = (limit.to_i.zero? || limit.to_i > 20) ? 20 : limit.to_i
cached_data = Rails.cache.read("Users::OrganizationsController:index:#{page}:#{limit}:#{current_user.id}:#{sort_by}:#{sort_direction}")
unless cached_data
if current_user.logged?
logged_organizations_sql = observed_user.organizations.with_visibility(%w(common limited)).to_sql
privacy_organizations_sql = observed_user.organizations.with_visibility("privacy").joins(:organization_users).where(organization_users: {user_id: current_user.id}).to_sql
@organizations = Organization.from("( #{ logged_organizations_sql } UNION #{ privacy_organizations_sql } ) AS users")
else
@organizations = observed_user.organizations.with_visibility("common")
end
@organizations = @organizations.ransack(login_cont: params[:search]).result if params[:search].present?
@home_top_ids = @organizations.joins(:home_top_settings).where(home_top_settings: {user_id: observed_user.id}).order("home_top_settings.created_at asc").pluck(:id)
if @home_top_ids.present?
@organizations = @organizations.joins(:organization_extension).order("FIELD(users.id, #{@home_top_ids.join(",")}) desc, organization_extensions.#{sort_by} #{sort_direction}")
else
@organizations = @organizations.joins(:organization_extension).order("organization_extensions.#{sort_by} #{sort_direction}")
end
cached_data = kaminari_paginate(@organizations)
Rails.cache.write("Users::OrganizationsController:index:#{page}:#{limit}:#{current_user.id}:#{sort_by}:#{sort_direction}", cached_data, expires_in: 10.minutes)
end
@organizations = @organizations.ransack(login_cont: params[:search]).result if params[:search].present?
@home_top_ids = @organizations.joins(:home_top_settings).where(home_top_settings: {user_id: observed_user.id}).order("home_top_settings.created_at asc").pluck(:id)
if @home_top_ids.present?
@organizations = @organizations.joins(:organization_extension).order("FIELD(users.id, #{@home_top_ids.join(",")}) desc, organization_extensions.#{sort_by} #{sort_direction}")
else
@organizations = @organizations.joins(:organization_extension).order("organization_extensions.#{sort_by} #{sort_direction}")
end
@organizations = kaminari_paginate(@organizations)
@organizations = cached_data
end
private

View File

@ -2,10 +2,16 @@ class Users::ProjectsController < Users::BaseController
skip_before_action :check_observed_user_exists!, only: [:search]
def index
projects = Users::ProjectService.new(observed_user, query_params).call
@count = projects.count
@projects = paginate(projects.includes(:project_score, owner: { user_extension: :school }), special: observed_user.is_teacher?)
cached_data = Rails.cache.read("Users::ProjectsController:index:#{params[:page]}:#{params[:limit]}:#{query_params[:category]}:#{query_params[:status]}:#{query_params[:sort_direction]}:#{query_params[:topic_name]}")
unless cached_data
projects = Users::ProjectService.new(observed_user, query_params).call
data_to_cache = paginate(projects.includes(:project_score, owner: { user_extension: :school }), special: observed_user.is_teacher?)
Rails.cache.write("Users::ProjectsController:index:#{params[:page]}:#{params[:limit]}:#{query_params[:category]}:#{query_params[:status]}:#{query_params[:sort_direction]}:#{query_params[:topic_name]}", data_to_cache, expires_in: 10.minutes)
# 缓存为空时执行API请求等操作
end
@projects = cached_data
@count = @projects.total_count
end
def search

View File

@ -407,8 +407,15 @@ class UsersController < ApplicationController
end
def projects
page = params[:page].to_i.zero? ? 1 : params[:page].to_i
limit = (limit.to_i.zero? || limit.to_i > 9999) ? 9999 : limit.to_i
is_current_admin_user = User.current.logged? && (current_user&.admin? || current_user.id == @user.id)
scope, @home_top_ids = Projects::ListMyQuery.call(params, @user,is_current_admin_user)
cached_data = Rails.cache.read("UsersControllers::projects:#{page}:#{limit}:#{current_user.id}")
unless cached_data
cached_data = Projects::ListMyQuery.call(params, @user,is_current_admin_user)
Rails.cache.write("UsersControllers::projects:#{page}:#{limit}:#{current_user.id}", cached_data, expires_in: 10.minutes)
end
scope, @home_top_ids = cached_data
@total_count = scope.size
@projects = kaminari_unlimit_paginate(scope)
end