add: callback cache

This commit is contained in:
2021-10-26 15:22:42 +08:00
parent 91f1f4090f
commit 69c87fd5a4
14 changed files with 343 additions and 51 deletions

View File

@@ -1,5 +1,5 @@
class Cache::V2::ProjectCommonService < ApplicationService
attr_reader :project_id, :owner_id, :name, :identifier, :visits, :watchers, :praises, :forks, :issues, :pullrequests
attr_reader :project_id, :owner_id, :name, :identifier, :description, :visits, :watchers, :praises, :forks, :issues, :pullrequests
attr_accessor :project
def initialize(project_id, params={})
@@ -7,6 +7,7 @@ class Cache::V2::ProjectCommonService < ApplicationService
@owner_id = params[:owner_id]
@name = params[:name]
@identifier = params[:identifier]
@description = params[:description]
@visits = params[:visits]
@watchers = params[:watchers]
@praises = params[:praises]
@@ -48,6 +49,10 @@ class Cache::V2::ProjectCommonService < ApplicationService
"identifier"
end
def description_key
"description"
end
def visits_key
"visits"
end
@@ -102,13 +107,22 @@ class Cache::V2::ProjectCommonService < ApplicationService
$redis_cache.hset(project_common_key, identifier_key, @identifier)
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)
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
$redis_cache.hincrby(project_common_key, visits_key, @visits)
Cache::V2::ProjectRankService.call(@project_id, {visits: @visits})
Cache::V2::ProjectDateRankService.call(@project_id, {visits: @visits})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {visits: @visits})
end
end
if @watchers.present?
@@ -121,37 +135,45 @@ class Cache::V2::ProjectCommonService < ApplicationService
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
$redis_cache.hincrby(project_common_key, praises_key, @praises)
Cache::V2::ProjectRankService.call(@project_id, {praises: @praises})
Cache::V2::ProjectDateRankService.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
$redis_cache.hincrby(project_common_key, forks_key, @forks)
Cache::V2::ProjectRankService.call(@project_id, {forks: @forks})
Cache::V2::ProjectDateRankService.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
$redis_cache.hincrby(project_common_key, issues_key, @issues)
Cache::V2::ProjectRankService.call(@project_id, {issues: @issues})
Cache::V2::ProjectDateRankService.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
$redis_cache.hincrby(project_common_key, pullrequests_key, @pullrequests)
Cache::V2::ProjectRankService.call(@project_id, {pullrequests: @pullrequests})
Cache::V2::ProjectDateRankService.call(@project_id, {pullrequests: @pullrequests})
Cache::V2::ProjectDateRankService.call(@project_id, Date.today, {pullrequests: @pullrequests})
end
end
$redis_cache.hgetall(project_common_key)
@@ -169,6 +191,10 @@ class Cache::V2::ProjectCommonService < ApplicationService
$redis_cache.hset(project_common_key, identifier_key, @project&.identifier)
end
def reset_project_description
$redis_cache.hset(project_common_key, description_key, @project&.description)
end
def reset_project_visits
$redis_cache.hset(project_common_key, visits_key, @project&.visits)
end
@@ -186,7 +212,7 @@ class Cache::V2::ProjectCommonService < ApplicationService
end
def reset_project_issues
$redis_cache.hset(project_common_key, issues_key, Issue.where(project_id: @project_id).count)
$redis_cache.hset(project_common_key, issues_key, Issue.issue_issue.where(project_id: @project_id).count)
end
def reset_project_pullrequests
@@ -199,6 +225,7 @@ class Cache::V2::ProjectCommonService < ApplicationService
reset_project_owner_id
reset_project_name
reset_project_identifier
reset_project_description
reset_project_visits
reset_project_watchers
reset_project_praises

View File

@@ -4,6 +4,7 @@ class Cache::V2::ProjectDateRankService < ApplicationService
def initialize(project_id, rank_date=Date.today, params={})
@project_id = project_id
@rank_date = rank_date
@visits = params[:visits]
@praises = params[:praises]
@forks = params[:forks]
@@ -21,7 +22,7 @@ class Cache::V2::ProjectDateRankService < ApplicationService
private
def project_rank_key
"v2-project-rank-#{rank_date.to_s}"
"v2-project-rank-#{@rank_date.to_s}"
end
def project_rank

View File

@@ -0,0 +1,118 @@
class Cache::V2::UserDateRankService < ApplicationService
attr_reader :user_id, :rank_date, :follow_count, :fork_count, :issue_count, :project_count, :project_language_count_key, :project_language_count, :project_praise_count, :project_watcher_count, :pullrequest_count
def initialize(user_id, rank_date=Date.today, params={})
@user_id = user_id
@rank_date = rank_date
@follow_count = params[:follow_count]
@fork_count = params[:fork_count]
@issue_count = params[:issue_count]
@project_count = params[:project_count]
@project_language_count_key = params[:project_language_count_key]
@project_language_count = params[:project_language_count]
@project_praise_count = params[:project_praise_count]
@project_watcher_count = params[:project_watcher_count]
@pullrequest_count = params[:pullrequest_count]
end
def read_rank
user_rank
end
def read_statistic
user_statistic
end
def call
set_user_rank
end
private
def user_rank_key
"v2-user-rank-#{@rank_date.to_s}"
end
def user_date_statistic_key
"v2-user-statistic:#{@user_id}-#{@rank_date.to_s}"
end
def user_rank
$redis_cache.zscore(user_rank_key, @user_id)
end
def user_statistic
$redis_cache.hgetall(user_date_statistic_key)
end
def set_user_statistic
if @follow_count.present?
$redis_cache.hincrby(user_date_statistic_key, "follow-count", @follow_count.to_i)
end
if @fork_count.present?
$redis_cache.hincrby(user_date_statistic_key, "fork-count", @fork_count.to_i)
end
if @issue_count.present?
$redis_cache.hincrby(user_date_statistic_key, "issue-count", @issue_count.to_i)
end
if @project_count.present?
$redis_cache.hincrby(user_date_statistic_key, "project-count", @project_count.to_i)
end
if project_language_count_key.present? && project_language_count.present?
if $redis_cache.hget(user_date_statistic_key, "project-language").nil?
result = {}
result[@project_language_count_key] = project_language_count.to_i
result.delete(@project_language_count_key) if result[@project_language_count_key] == 0
$redis_cache.hset(user_date_statistic_key, "project-language", result.to_json)
else
result = JSON.parse($redis_cache.hget(user_date_statistic_key, "project-language"))
result[@project_language_count_key] ||= 0
result[@project_language_count_key] += project_language_count.to_i
result.delete(@project_language_count_key) if result[@project_language_count_key] == 0
$redis_cache.hset(user_date_statistic_key, "project-language", result.to_json)
end
end
if @project_praise_count.present?
$redis_cache.hincrby(user_date_statistic_key, "project-praise-count", @project_praise_count.to_i)
end
if @project_watcher_count.present?
$redis_cache.hincrby(user_date_statistic_key, "project-watcher-count", @project_watcher_count.to_i)
end
if @pullrequest_count.present?
$redis_cache.hincrby(user_date_statistic_key, "pullrequest-count", @pullrequest_count.to_i)
end
$redis_cache.hgetall(user_date_statistic_key)
end
def set_user_rank
set_user_statistic
follow_count = $redis_cache.hget(user_date_statistic_key, "follow-count") || 0
pullrequest_count = $redis_cache.hget(user_date_statistic_key, "pullrequest-count") || 0
issues_count = $redis_cache.hget(user_date_statistic_key, "issue-count") || 0
project_count = $redis_cache.hget(user_date_statistic_key, "project-count") || 0
fork_count = $redis_cache.hget(user_date_statistic_key, "fork-count") || 0
project_watchers_count = $redis_cache.hget(user_date_statistic_key, "project-watcher-count") || 0
project_praises_count = $redis_cache.hget(user_date_statistic_key, "project-praise-count") || 0
project_languages_count = $redis_cache.hget(user_date_statistic_key, "project-language").nil? ? 0 : $redis_cache.hget(user_date_statistic_key, "project-language").length
# 影响力
influence = (60.0 + follow_count.to_i / (follow_count.to_i + 20.0) * 40.0).to_i
# 贡献度
contribution = (60.0 + pullrequest_count.to_i / (pullrequest_count.to_i + 20.0) * 40.0).to_i
# 活跃度
activity = (60.0 + issues_count.to_i / (issues_count.to_i + 80.0) * 40.0).to_i
# 项目经验
experience = 10 * project_count.to_i + 5 * fork_count.to_i + project_watchers_count.to_i + project_praises_count.to_i
experience = (60.0 + experience / (experience + 100.0) * 40.0).to_i
# 语言能力
language = (60.0 + project_languages_count.to_i / (project_languages_count.to_i + 5.0) * 40.0).to_i
score = influence+ contribution + activity + experience + language
$redis_cache.zadd(user_rank_key, score, @user_id) if score.to_i > 300
$redis_cache.zscore(user_rank_key, @user_id)
end
end

View File

@@ -76,60 +76,77 @@ class Cache::V2::UserStatisticService < ApplicationService
if @follow_count.present?
if $redis_cache.hget(user_statistic_key, follow_count_key).nil?
reset_user_follow_count
Cache::V2::UserDateRankService.call(@user_id, Date.today, {follow_count: @follow_count})
else
$redis_cache.hincrby(user_statistic_key, follow_count_key, @follow_count)
Cache::V2::UserDateRankService.call(@user_id, Date.today, {follow_count: @follow_count})
end
end
if @fork_count.present?
if $redis_cache.hget(user_statistic_key, fork_count_key).nil?
reset_user_fork_count
Cache::V2::UserDateRankService.call(@user_id, Date.today, {fork_count: @fork_count})
else
$redis_cache.hincrby(user_statistic_key, fork_count_key, @fork_count)
Cache::V2::UserDateRankService.call(@user_id, Date.today, {fork_count: @fork_count})
end
end
if @issue_count.present?
if $redis_cache.hget(user_statistic_key, issue_count_key).nil?
reset_user_issue_count
Cache::V2::UserDateRankService.call(@user_id, Date.today, {issue_count: @issue_count})
else
$redis_cache.hincrby(user_statistic_key, issue_count_key, @issue_count)
Cache::V2::UserDateRankService.call(@user_id, Date.today, {issue_count: @issue_count})
end
end
if @project_count.present?
if $redis_cache.hget(user_statistic_key, project_count_key).nil?
reset_user_project_count
Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_count: @project_count})
else
$redis_cache.hincrby(user_statistic_key, project_count_key, @project_count)
Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_count: @project_count})
end
end
if @project_language_count_key.present? && project_language_count.present?
if $redis_cache.hget(user_statistic_key, project_language_key).nil?
reset_user_project_language
Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_language_count_key: @project_language_count_key, project_language_count: @project_language_count})
else
result = JSON.parse($redis_cache.hget(user_statistic_key, project_language_key))
result[@project_language_count_key] ||= 0
result[@project_language_count_key] += project_language_count.to_i
result.delete(@project_language_count_key) if result[@project_language_count_key] == 0
$redis_cache.hset(user_statistic_key, project_language_key, result.to_json)
Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_language_count_key: @project_language_count_key, project_language_count: @project_language_count})
end
end
if @project_praise_count.present?
if $redis_cache.hget(user_statistic_key, project_praise_count_key).nil?
reset_user_project_praise_count
Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_praise_count: @project_praise_count})
else
$redis_cache.hincrby(user_statistic_key, project_praise_count_key, @project_praise_count)
Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_praise_count: @project_praise_count})
end
end
if @project_watcher_count.present?
if $redis_cache.hget(user_statistic_key, project_watcher_count_key).nil?
reset_user_project_watcher_count
Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_watcher_count: @project_watcher_count})
else
$redis_cache.hincrby(user_statistic_key, project_watcher_count_key, @project_watcher_count)
Cache::V2::UserDateRankService.call(@user_id, Date.today, {project_watcher_count: @project_watcher_count})
end
end
if @pullrequest_count.present?
if $redis_cache.hget(user_statistic_key, pullrequest_count_key).nil?
reset_user_pullrequest_count
Cache::V2::UserDateRankService.call(@user_id, Date.today, {pullrequest_count: @pullrequest_count})
else
$redis_cache.hincrby(user_statistic_key, pullrequest_count_key, @pullrequest_count)
Cache::V2::UserDateRankService.call(@user_id, Date.today, {pullrequest_count: @pullrequest_count})
end
end
$redis_cache.hgetall(user_statistic_key)