diff --git a/app/controllers/organizations/organizations_controller.rb b/app/controllers/organizations/organizations_controller.rb index 58aa0d29d..0ae113590 100644 --- a/app/controllers/organizations/organizations_controller.rb +++ b/app/controllers/organizations/organizations_controller.rb @@ -31,6 +31,7 @@ class Organizations::OrganizationsController < Organizations::BaseController Organizations::CreateForm.new(organization_params.merge(original_name: "")).validate! @organization = Organizations::CreateService.call(current_user, organization_params) Util.write_file(@image, avatar_path(@organization)) if params[:image].present? + Cache::V2::OwnerCommonService.new(@organization.id).reset end rescue Exception => e uid_logger_error(e.message) @@ -48,6 +49,7 @@ class Organizations::OrganizationsController < Organizations::BaseController Gitea::Organization::UpdateService.call(current_user.gitea_token, login, @organization.reload) Util.write_file(@image, avatar_path(@organization)) if params[:image].present? + Cache::V2::OwnerCommonService.new(@organization.id).reset end rescue Exception => e uid_logger_error(e.message) diff --git a/app/models/commit_log.rb b/app/models/commit_log.rb index 9b51b0631..326872b36 100644 --- a/app/models/commit_log.rb +++ b/app/models/commit_log.rb @@ -3,4 +3,15 @@ class CommitLog < ApplicationRecord belongs_to :project belongs_to :repository + after_create :incre_project_common + after_destroy :decre_project_common + + def incre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {commits: 1}, self.project_id) + end + + def decre_project_common + CacheAsyncSetJob.perform_later("project_common_service", {commits: -1}, self.project_id) + end + end diff --git a/app/models/organization.rb b/app/models/organization.rb index 56351a415..adf43a2cd 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -81,15 +81,15 @@ class Organization < Owner scope :with_visibility, ->(visibility) { joins(:organization_extension).where(organization_extensions: {visibility: visibility}) if visibility.present? } - after_save :reset_cache_data + # after_save :reset_cache_data def gitea_token team_users.joins(:team).where(teams: {authorize: "owner"}).take&.user&.gitea_token end - def reset_cache_data - Cache::V2::OwnerCommonService.new(self.id).reset - end + # def reset_cache_data + # Cache::V2::OwnerCommonService.new(self.id).reset + # end def self.build(name, nickname, gitea_token=nil) self.create!(login: name, nickname: nickname, gitea_token: gitea_token) diff --git a/app/services/cache/v2/project_common_service.rb b/app/services/cache/v2/project_common_service.rb index 8ed0a0636..97023ae29 100644 --- a/app/services/cache/v2/project_common_service.rb +++ b/app/services/cache/v2/project_common_service.rb @@ -1,5 +1,5 @@ class Cache::V2::ProjectCommonService < ApplicationService - attr_reader :project_id, :owner_id, :name, :identifier, :description, :visits, :watchers, :praises, :forks, :issues, :pullrequests + attr_reader :project_id, :owner_id, :name, :identifier, :description, :visits, :watchers, :praises, :forks, :issues, :pullrequests, :commits attr_accessor :project def initialize(project_id, params={}) @@ -14,6 +14,7 @@ class Cache::V2::ProjectCommonService < ApplicationService @forks = params[:forks] @issues = params[:issues] @pullrequests = params[:pullrequests] + @commits = params[:commits] end def read @@ -81,6 +82,10 @@ class Cache::V2::ProjectCommonService < ApplicationService "pullrequests" end + def commits_key + "commits" + end + def project_common result = $redis_cache.hgetall(project_common_key) result.blank? ? reset_project_common : result @@ -149,6 +154,11 @@ class Cache::V2::ProjectCommonService < ApplicationService Cache::V2::ProjectRankService.call(@project_id, {pullrequests: @pullrequests}) Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {pullrequests: @pullrequests}) end + if @commits.present? + $redis_cache.hincrby(project_common_key, commits_key, @commits) + Cache::V2::ProjectRankService.call(@project_id, {commits: @commits}) + Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {commits: @commits}) + end end $redis_cache.hgetall(project_common_key) @@ -194,6 +204,10 @@ class Cache::V2::ProjectCommonService < ApplicationService $redis_cache.hset(project_common_key, pullrequests_key, PullRequest.where(project_id: @project_id).count) end + def reset_project_commits + $redis_cache.hset(project_common_key, commits_key, CommitLog.where(project_id: @project_id).count) + end + def reset_project_common load_project return unless @project.present? @@ -209,6 +223,7 @@ class Cache::V2::ProjectCommonService < ApplicationService reset_project_forks reset_project_issues reset_project_pullrequests + reset_project_commits $redis_cache.hgetall(project_common_key) end diff --git a/app/services/cache/v2/project_date_rank_service.rb b/app/services/cache/v2/project_date_rank_service.rb index 092aff796..08aae6f77 100644 --- a/app/services/cache/v2/project_date_rank_service.rb +++ b/app/services/cache/v2/project_date_rank_service.rb @@ -1,5 +1,6 @@ +# 项目日活跃度计算存储 class Cache::V2::ProjectDateRankService < ApplicationService - attr_reader :project_id, :rank_date, :visits, :praises, :forks, :issues, :pullrequests + attr_reader :project_id, :rank_date, :visits, :praises, :forks, :issues, :pullrequests, :commits attr_accessor :project_common def initialize(project_id, rank_date=Date.today, params={}) @@ -10,6 +11,7 @@ class Cache::V2::ProjectDateRankService < ApplicationService @forks = params[:forks] @issues = params[:issues] @pullrequests = params[:pullrequests] + @commits = params[:commits] end def read @@ -37,7 +39,7 @@ class Cache::V2::ProjectDateRankService < ApplicationService $redis_cache.zincrby(project_rank_key, @praises.to_i * 5, @project_id) end if @forks.present? - $redis_cache.zincrby(project_rank_key, @forks.to_i * 5, @project_id) + $redis_cache.zincrby(project_rank_key, @forks.to_i * 10, @project_id) end if @issues.present? $redis_cache.zincrby(project_rank_key, @issues.to_i * 10, @project_id) @@ -45,6 +47,9 @@ class Cache::V2::ProjectDateRankService < ApplicationService if @pullrequests.present? $redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id) end + if @commits.present? + $redis_cache.zincrby(project_rank_key, @commits.to_i * 1, @project_id) + end $redis_cache.zscore(project_rank_key, @project_id) end diff --git a/app/services/cache/v2/project_rank_service.rb b/app/services/cache/v2/project_rank_service.rb index 7e5d323bf..3265c0a6c 100644 --- a/app/services/cache/v2/project_rank_service.rb +++ b/app/services/cache/v2/project_rank_service.rb @@ -1,5 +1,5 @@ class Cache::V2::ProjectRankService < ApplicationService - attr_reader :project_id, :visits, :praises, :forks, :issues, :pullrequests + attr_reader :project_id, :visits, :praises, :forks, :issues, :pullrequests, :commits attr_accessor :project_common def initialize(project_id, params={}) @@ -9,6 +9,7 @@ class Cache::V2::ProjectRankService < ApplicationService @forks = params[:forks] @issues = params[:issues] @pullrequests = params[:pullrequests] + @commits = params[:commits] end def read @@ -54,7 +55,7 @@ class Cache::V2::ProjectRankService < ApplicationService $redis_cache.zincrby(project_rank_key, @praises.to_i * 5, @project_id) end if @forks.present? - $redis_cache.zincrby(project_rank_key, @forks.to_i * 5, @project_id) + $redis_cache.zincrby(project_rank_key, @forks.to_i * 10, @project_id) end if @issues.present? $redis_cache.zincrby(project_rank_key, @issues.to_i * 10, @project_id) @@ -62,6 +63,9 @@ class Cache::V2::ProjectRankService < ApplicationService if @pullrequests.present? $redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id) end + if @commits.present? + $redis_cache.zincrby(project_rank_key, @commits.to_i * 1, @project_id) + end reset_user_project_rank end @@ -70,7 +74,7 @@ class Cache::V2::ProjectRankService < ApplicationService def reset_project_rank load_project_common - score = @project_common["visits"].to_i * 1 + @project_common["praises"].to_i * 5 + @project_common["forks"].to_i * 5 + @project_common["issues"].to_i * 10 + @project_common["pullrequests"].to_i * 10 + score = @project_common["visits"].to_i * 1 + @project_common["praises"].to_i * 5 + @project_common["forks"].to_i * 10 + @project_common["issues"].to_i * 10 + @project_common["pullrequests"].to_i * 10 + @project_common["commits"].to_i * 1 $redis_cache.zadd(project_rank_key, score, @project_id) reset_user_project_rank