From 383660c778ee1bc08f86e587613583559d4fa829 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 15 Sep 2022 09:38:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=9A=E7=BB=84?= =?UTF-8?q?=E7=BB=87=E7=BC=93=E5=AD=98=E4=BF=A1=E6=81=AF=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=94=B9=E5=9C=A8=E6=8E=A7=E5=88=B6=E5=99=A8=E9=87=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/organizations/organizations_controller.rb | 2 ++ app/models/organization.rb | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) 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/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) From 733f64eaec316717e8bdd26d18ee41ef2748e513 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 15 Sep 2022 09:44:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E6=8E=92=E8=A1=8C=E6=A6=9C=E6=96=B0=E5=A2=9E=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E6=95=B0=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/commit_log.rb | 11 +++++++++++ app/services/cache/v2/project_common_service.rb | 17 ++++++++++++++++- .../cache/v2/project_date_rank_service.rb | 9 +++++++-- app/services/cache/v2/project_rank_service.rb | 10 +++++++--- 4 files changed, 41 insertions(+), 6 deletions(-) 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/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