add: rank not include deleted project

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

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