add: rank not include deleted project

This commit is contained in:
yystopf 2021-11-01 16:20:52 +08:00
parent 2273b4817e
commit 46405e704a
15 changed files with 163 additions and 133 deletions

View File

@ -22,7 +22,7 @@ class Organizations::OrganizationsController < Organizations::BaseController
@can_create_project = @organization.can_create_project?(current_user.id)
@is_admin = can_edit_org?
@is_member = @organization.is_member?(current_user.id)
Cache::V2::OwnerCommonService.new(@organization.login, @organization.mail).read
Cache::V2::OwnerCommonService.new(@organization.id).read
end
def create

View File

@ -2,9 +2,10 @@ class ProjectRankController < ApplicationController
# 根据时间获取热门项目
def index
$redis_cache.zunionstore("recent-days-project-rank", get_timeable_key_names)
$redis_cache.zrem("recent-days-project-rank", $redis_cache.smembers("v2-project-rank-deleted"))
@project_rank = $redis_cache.zrevrange("recent-days-project-rank", 0, 4, withscores: true)
rescue Exception => e
@project_rack = []
@project_rank = []
end
private

View File

@ -191,7 +191,7 @@ class ProjectsController < ApplicationController
def simple
# 为了缓存活跃项目的基本信息,后续删除
Cache::V2::ProjectCommonService.new(@project.id).reset
Cache::V2::ProjectCommonService.new(@project.id).read
json_response(@project, current_user)
end

View File

@ -52,7 +52,7 @@ class UsersController < ApplicationController
@projects_mirrior_count = user_projects.mirror.size
@projects_sync_mirrior_count = user_projects.sync_mirror.size
# 为了缓存活跃用户的基本信息,后续删除
Cache::V2::OwnerCommonService.new(@user.login, @user.mail).read
Cache::V2::OwnerCommonService.new(@user.id).read
end
def watch_users

View File

@ -0,0 +1,12 @@
class CacheAsyncClearJob < ApplicationJob
queue_as :cache
def perform(type, id=nil)
case type
when "project_common_service"
Cache::V2::ProjectCommonService.new(id).clear
when "owner_common_service"
Cache::V2::OwnnerCommonService.new(id).clear
end
end
end

View File

@ -7,6 +7,8 @@ class CacheAsyncResetJob < ApplicationJob
Cache::V2::PlatformStatisticService.new.reset
when "project_common_service"
Cache::V2::ProjectCommonService.new(id).reset
when "owner_common_service"
Cache::V2::OwnnerCommonService.new(id).reset
when "user_statistic_service"
Cache::V2::UserStatisticService.new(id).reset
end

View File

@ -7,6 +7,8 @@ class CacheAsyncSetJob < ApplicationJob
Cache::V2::PlatformStatisticService.new(params).call
when "project_common_service"
Cache::V2::ProjectCommonService.new(id, params).call
when "owner_common_service"
Cache::V2::OwnnerCommonService.new(id, params).call
when "user_statistic_service"
Cache::V2::UserStatisticService.new(id, params).call
end

View File

@ -84,7 +84,7 @@ class Organization < Owner
after_save :reset_cache_data
def reset_cache_data
Cache::V2::OwnerCommonService.new(self.login, self.mail).reset
Cache::V2::OwnerCommonService.new(self.id).reset
end
def self.build(name, nickname, gitea_token=nil)

View File

@ -128,7 +128,7 @@ class Project < ApplicationRecord
has_many :pinned_projects, dependent: :destroy
has_many :has_pinned_users, through: :pinned_projects, source: :user
has_many :webhooks, class_name: "Gitea::Webhook", primary_key: :gpid, foreign_key: :repo_id
after_create :init_project_common, :incre_user_statistic, :incre_platform_statistic
after_create :incre_user_statistic, :incre_platform_statistic
after_save :check_project_members, :reset_cache_data
before_save :set_invite_code, :reset_unmember_followed, :set_recommend_and_is_pinned
before_destroy :decre_project_common
@ -169,12 +169,8 @@ class Project < ApplicationRecord
end
end
def init_project_common
CacheAsyncResetJob.perform_later("project_common_service", self.id)
end
def decre_project_common
$redis_cache.del("v2-project-common:#{self.id}")
CacheAsyncClearJob.perform_later('project_common_service', self.id)
end
def incre_user_statistic

View File

@ -207,7 +207,7 @@ class User < Owner
validate :validate_password_length
def reset_cache_data
Cache::V2::OwnerCommonService.new(self.login, self.mail).reset
Cache::V2::OwnerCommonService.new(self.id).reset
end
# 用户参与的所有项目

View File

@ -1,12 +1,13 @@
class Cache::V2::OwnerCommonService < ApplicationService
include AvatarHelper
attr_reader :owner_id, :login, :name, :avatar_url, :email
attr_accessor :owner
attr_reader :owner_id, :name
attr_accessor :owner, :login, :email
def initialize(login, email, params={})
@login = login
@email = email
def initialize(owner_id, params={})
@owner_id = owner_id
@email = params[:email]
@name = params[:name]
@avatar_url = params[:avatar_url]
end
def read
@ -14,7 +15,6 @@ class Cache::V2::OwnerCommonService < ApplicationService
end
def call
load_owner
set_owner_common
end
@ -22,9 +22,15 @@ class Cache::V2::OwnerCommonService < ApplicationService
reset_owner_common
end
def clear
clear_owner_common
end
private
def load_owner
@owner = Owner.find_by(login: @login)
@owner = Owner.find_by_id @owner_id
@login = @owner&.login
@email ||= @owner&.mail
end
def owner_common_key
@ -32,35 +38,47 @@ class Cache::V2::OwnerCommonService < ApplicationService
end
def owner_common_key_by_id
"v2-owner-common:#{@owner.id}"
"v2-owner-common:#{@owner&.id}"
end
def owner_common
$redis_cache.hgetall(owner_common_key).blank? ? reset_owner_common : $redis_cache.hgetall(owner_common_key)
result = $redis_cache.hgetall(owner_common_key_by_id)
result.blank? ? reset_owner_common : result
end
def set_owner_common
if $redis_cache.hgetall(owner_common_key).blank?
if $redis_cache.hgetall(owner_common_key_by_id).blank?
reset_owner_common
return
end
if @name.present?
if $redis_cache.hget(owner_common_key, "name").nil?
reset_owner_name
else
$redis_cache.hset(owner_common_key, "name", @name)
$redis_cache.hset(owner_common_key, "avatar_url", url_to_avatar(owner))
$redis_cache.hset(owner_common_key_by_id, "name", @name)
$redis_cache.hset(owner_common_key_by_id, "avatar_url", url_to_avatar(owner))
else
load_owner
return if @owner.nil?
if @name.present?
if $redis_cache.hget(owner_common_key, "name").nil?
reset_owner_name
else
$redis_cache.hset(owner_common_key, "name", @name)
$redis_cache.hset(owner_common_key_by_id, "name", @name)
end
end
end
if @email.present?
if $redis_cache.hget(owner_common_key, "email").nil?
reset_owner_email
else
$redis_cache.hset(owner_common_key, "email", @email)
$redis_cache.hset(owner_common_key_by_id, "email", @email)
if @email.present?
if $redis_cache.hget(owner_common_key, "email").nil?
reset_owner_email
else
# 更改邮箱这里把旧数据删除
$redis_cache.del("v2-owner-common:#{@login}-*")
$redis_cache.hset(owner_common_key, "email", @email)
$redis_cache.hset(owner_common_key_by_id, "email", @email)
end
end
if @avatar_url.present?
if $redis_cache.hget(owner_common_key, "avatar_url").nil?
reset_owner_avatar_url
else
$redis_cache.hset(owner_common_key, "avatar_url", @avatar_url)
$redis_cache.hset(owner_common_key_by_id, "avatar_url", @avatar_url)
end
end
end
@ -88,20 +106,30 @@ class Cache::V2::OwnerCommonService < ApplicationService
def reset_owner_name
$redis_cache.hset(owner_common_key, "name", owner&.real_name)
$redis_cache.hset(owner_common_key, "avatar_url", url_to_avatar(owner))
$redis_cache.hset(owner_common_key_by_id, "name", owner&.real_name)
end
def reset_owner_avatar_url
$redis_cache.hset(owner_common_key, "avatar_url", url_to_avatar(owner))
$redis_cache.hset(owner_common_key_by_id, "avatar_url", url_to_avatar(owner))
end
def reset_owner_common
load_owner
$redis_cache.del(owner_common_key)
clear_owner_common
reset_owner_id
reset_owner_type
reset_owner_login
reset_owner_email
reset_owner_name
reset_owner_avatar_url
$redis_cache.hgetall(owner_common_key)
end
def clear_owner_common
load_owner
return if @owner.nil?
$redis_cache.del(owner_common_key)
$redis_cache.del(owner_common_key_by_id)
end
end

View File

@ -64,7 +64,9 @@ class Cache::V2::PlatformStatisticService < ApplicationService
end
def platform_statistic
$redis_cache.hgetall(platform_statistic_key).blank? ? reset_platform_statistic : $redis_cache.hgetall(platform_statistic_key)
result = $redis_cache.hgetall(platform_statistic_key)
result.blank? ? reset_platform_statistic : result
end
def set_platform_statistic

View File

@ -28,6 +28,10 @@ class Cache::V2::ProjectCommonService < ApplicationService
reset_project_common
end
def clear
clear_project_common
end
private
def load_project
@project = Project.find_by_id(project_id)
@ -78,109 +82,75 @@ class Cache::V2::ProjectCommonService < ApplicationService
end
def project_common
$redis_cache.hgetall(project_common_key).blank? ? reset_project_common : $redis_cache.hgetall(project_common_key)
result = $redis_cache.hgetall(project_common_key)
result.blank? ? reset_project_common : result
end
def set_project_common
if $redis_cache.hgetall(project_common_key).blank?
reset_project_common
return
end
load_project
return unless @project.is_full_public
if @owner_id.present?
if $redis_cache.hget(project_common_key, owner_id_key).nil?
reset_project_owner_id
else
$redis_cache.hset(project_common_key, owner_id_key, @owner_id)
else
load_project
return unless @project.is_full_public
if @owner_id.present?
if $redis_cache.hget(project_common_key, owner_id_key).nil?
reset_project_owner_id
else
$redis_cache.hset(project_common_key, owner_id_key, @owner_id)
end
end
end
if @name.present?
if $redis_cache.hget(project_common_key, name_key).nil?
reset_project_name
else
$redis_cache.hset(project_common_key, name_key, @name)
if @name.present?
if $redis_cache.hget(project_common_key, name_key).nil?
reset_project_name
else
$redis_cache.hset(project_common_key, name_key, @name)
end
end
end
if @identifier.present?
if $redis_cache.hget(project_common_key, identifier_key).nil?
reset_project_identifier
else
$redis_cache.hset(project_common_key, identifier_key, @identifier)
if @identifier.present?
if $redis_cache.hget(project_common_key, identifier_key).nil?
reset_project_identifier
else
$redis_cache.hset(project_common_key, identifier_key, @identifier)
end
end
end
if @description.present?
if $redis_cache.hget(project_common_key, description_key).nil?
reset_project_description
else
$redis_cache.hset(project_common_key, description_key, @description)
if @description.present?
if $redis_cache.hget(project_common_key, description_key).nil?
reset_project_description
else
$redis_cache.hset(project_common_key, description_key, @description)
end
end
end
if @visits.present?
if $redis_cache.hget(project_common_key, visits_key).nil?
reset_project_visits
Cache::V2::ProjectRankService.call(@project_id, {visits: @visits})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {visits: @visits})
else
puts project_common_key
puts visits_key
puts @visits
if @visits.present?
$redis_cache.hincrby(project_common_key, visits_key, @visits.to_s)
Cache::V2::ProjectRankService.call(@project_id, {visits: @visits})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {visits: @visits})
end
end
if @watchers.present?
if $redis_cache.hget(project_common_key, watchers_key).nil?
reset_project_watchers
else
if @watchers.present?
$redis_cache.hincrby(project_common_key, watchers_key, @watchers)
end
end
if @praises.present?
if $redis_cache.hget(project_common_key, praises_key).nil?
reset_project_praises
Cache::V2::ProjectRankService.call(@project_id, {praises: @praises})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {praises: @praises})
else
if @praises.present?
$redis_cache.hincrby(project_common_key, praises_key, @praises)
Cache::V2::ProjectRankService.call(@project_id, {praises: @praises})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {praises: @praises})
Cache::V2::ProjectRankService.call(@project_id, {praises: @praises})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {praises: @praises})
end
end
if @forks.present?
if $redis_cache.hget(project_common_key, forks_key).nil?
reset_project_forks
Cache::V2::ProjectRankService.call(@project_id, {forks: @forks})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {forks: @forks})
else
if @forks.present?
$redis_cache.hincrby(project_common_key, forks_key, @forks)
Cache::V2::ProjectRankService.call(@project_id, {forks: @forks})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {forks: @forks})
end
end
if @issues.present?
if $redis_cache.hget(project_common_key, issues_key).nil?
reset_project_issues
Cache::V2::ProjectRankService.call(@project_id, {issues: @issues})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {issues: @issues})
else
if @issues.present?
$redis_cache.hincrby(project_common_key, issues_key, @issues)
Cache::V2::ProjectRankService.call(@project_id, {issues: @issues})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {issues: @issues})
end
end
if @pullrequests.present?
if $redis_cache.hget(project_common_key, pullrequests_key).nil?
reset_project_pullrequests
Cache::V2::ProjectRankService.call(@project_id, {pullrequests: @pullrequests})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {pullrequests: @pullrequests})
else
if @pullrequests.present?
$redis_cache.hincrby(project_common_key, pullrequests_key, @pullrequests)
Cache::V2::ProjectRankService.call(@project_id, {pullrequests: @pullrequests})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {pullrequests: @pullrequests})
end
end
$redis_cache.hgetall(project_common_key)
end
@ -241,4 +211,9 @@ class Cache::V2::ProjectCommonService < ApplicationService
$redis_cache.hgetall(project_common_key)
end
def clear_project_common
$redis_cache.del(project_common_key)
Cache::V2::ProjectRankService.new(@project_id).clear
end
end

View File

@ -23,6 +23,10 @@ class Cache::V2::ProjectRankService < ApplicationService
reset_project_rank
end
def clear
clear_project_rank
end
private
def load_project_common
@project_common = Cache::V2::ProjectCommonService.new(@project_id).read
@ -33,7 +37,8 @@ class Cache::V2::ProjectRankService < ApplicationService
end
def project_rank
$redis_cache.zscore(project_rank_key, @project_id).blank? ? reset_project_rank : $redis_cache.zscore(project_rank_key, @project_id)
result = $redis_cache.zscore(project_rank_key, @project_id)
result.blank? ? reset_project_rank : result
end
def set_project_rank
@ -41,23 +46,24 @@ class Cache::V2::ProjectRankService < ApplicationService
if $redis_cache.zscore(project_rank_key, @project_id).blank?
reset_project_rank
return
else
if @visits.present?
$redis_cache.zincrby(project_rank_key, @visits.to_i * 1, @project_id)
end
if @praises.present?
$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)
end
if @issues.present?
$redis_cache.zincrby(project_rank_key, @issues.to_i * 10, @project_id)
end
if @pullrequests.present?
$redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id)
end
reset_user_project_rank
end
if @visits.present?
$redis_cache.zincrby(project_rank_key, @visits.to_i * 1, @project_id)
end
if @praises.present?
$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)
end
if @issues.present?
$redis_cache.zincrby(project_rank_key, @issues.to_i * 10, @project_id)
end
if @pullrequests.present?
$redis_cache.zincrby(project_rank_key, @pullrequests.to_i * 10, @project_id)
end
reset_user_project_rank
$redis_cache.zscore(project_rank_key, @project_id)
end
@ -74,4 +80,8 @@ class Cache::V2::ProjectRankService < ApplicationService
def reset_user_project_rank
$redis_cache.zadd("v2-user-project-rank:#{@project_common["owner_id"]}", $redis_cache.zscore(project_rank_key, @project_id), @project_id)
end
def clear_project_rank
$redis_cache.sadd('v2-project-rank-deleted', @project_id)
end
end

View File

@ -12,6 +12,7 @@ class Cache::V2::UserStatisticService < ApplicationService
@project_praise_count = params[:project_praise_count]
@project_watcher_count = params[:project_watcher_count]
@pullrequest_count = params[:pullrequest_count]
Cache::V2::OwnerCommonService.new(user_id).read
end
def read
@ -65,7 +66,8 @@ class Cache::V2::UserStatisticService < ApplicationService
end
def user_statistic
$redis_cache.hgetall(user_statistic_key).blank? ? reset_user_statistic : $redis_cache.hgetall(user_statistic_key)
result = $redis_cache.hgetall(user_statistic_key)
result.blank? ? reset_user_statistic : result
end
def set_user_statistic