fix: old cache

This commit is contained in:
yystopf 2021-10-25 18:03:56 +08:00
parent fa476732e5
commit 91f1f4090f
10 changed files with 53 additions and 60 deletions

View File

@ -188,30 +188,32 @@ class Users::StatisticsController < Users::BaseController
@project_languages_count = time_filter(Project.where(user_id: observed_user.id), 'created_on').joins(:project_language).group("project_languages.name").count
@platform_project_languages_count = time_filter(Project, 'created_on').joins(:project_language).group("project_languages.name").count
else
@platform_result = Cache::V2::PlatformStatisticService.new.read
@user_result = Cache::V2::UserStatisticService.new(observed_user.id).read
# 用户被follow数量
@follow_count = Cache::UserFollowCountService.call(observed_user)
@platform_follow_count = Cache::PlatformFollowCountService.call
@follow_count = @user_result["follow-count"].to_i
@platform_follow_count = @platform_result["follow-count"].to_i
# 用户pr数量
@pullrequest_count = Cache::UserPullrequestCountService.call(observed_user)
@platform_pullrequest_count = Cache::PlatformPullrequestCountService.call
@pullrequest_count = @user_result["pullrequest-count"].to_i
@platform_pullrequest_count = @platform_result["pullrequest-count"].to_i
# 用户issue数量
@issues_count = Cache::UserIssueCountService.call(observed_user)
@platform_issues_count = Cache::PlatformIssueCountService.call
@issues_count = @user_result["issue-count"].to_i
@platform_issues_count = @platform_result["issue-count"].to_i
# 用户总项目数
@project_count = Cache::UserProjectCountService.call(observed_user)
@platform_project_count = Cache::PlatformProjectCountService.call
@project_count = @user_result["project-count"].to_i
@platform_project_count = @platform_result["project-count"].to_i
# 用户项目被fork数量
@fork_count = Cache::UserProjectForkCountService.call(observed_user)
@platform_fork_count = Cache::PlatformProjectForkCountService.call
@fork_count = @user_result["fork-count"].to_i
@platform_fork_count = @platform_result["fork-count"].to_i
# 用户项目关注数
@project_watchers_count = Cache::UserProjectWatchersCountService.call(observed_user)
@platform_project_watchers_count = Cache::PlatformProjectWatchersCountService.call
@project_watchers_count = @user_result["project-watcher-count"].to_i
@platform_project_watchers_count = @platform_result["project-watcher-count"].to_i
# 用户项目点赞数
@project_praises_count = Cache::UserProjectPraisesCountService.call(observed_user)
@platform_project_praises_count = Cache::PlatformProjectPraisesCountService.call
@project_praises_count = @user_result["project-praise-count"].to_i
@platform_project_praises_count = @platform_result["project-praise-count"].to_i
# 用户不同语言项目数量
@project_languages_count = Cache::UserProjectLanguagesCountService.call(observed_user)
@platform_project_languages_count = Cache::PlatformProjectLanguagesCountService.call
@project_languages_count = JSON.parse(@user_result["project-language"])
@platform_project_languages_count = JSON.parse(@platform_result["project-language"])
end
end
end

View File

@ -1,14 +1,14 @@
class CacheAsyncResetJob < ApplicationJob
queue_as :cache
def perform(type, id)
def perform(type, id=nil)
case type
when "platform_statistic_service"
Cache::V2::PlatformStatisticService.new.reset
when "project_common_service"
Cache::V2::ProjectCommonService.new(id).reset
when "user_statistic_service"
Cache::V2::PlatformStatisticService.new(id).reset
Cache::V2::UserStatisticService.new(id).reset
end
end
end

View File

@ -1,14 +1,14 @@
class CacheAsyncResetJob < ApplicationJob
queue_as :cache
def perform(type, id, params={})
def perform(type, id=nil, params={})
case type
when "platform_statistic_service"
Cache::V2::PlatformStatisticService.new(params).call
when "project_common_service"
Cache::V2::ProjectCommonService.new(id, params).call
when "user_statistic_service"
Cache::V2::PlatformStatisticService.new(id, params).call
Cache::V2::UserStatisticService.new(id, params).call
end
end
end

View File

@ -17,14 +17,6 @@ class ApplicationRecord < ActiveRecord::Base
Rails.env.production? && EduSetting.get('host_name') == 'https://www.educoder.net'
end
def reset_user_cache_async_job(user)
ResetUserCacheJob.perform_later(user)
end
def reset_platform_cache_async_job
ResetPlatformCacheJob.perform_later
end
def self.strip_param(key)
key.to_s.strip.presence
end

View File

@ -24,8 +24,8 @@ class ForkUser < ApplicationRecord
after_destroy :reset_cache_data
def reset_cache_data
self.reset_platform_cache_async_job
self.reset_user_cache_async_job(self.project.owner)
CacheAsyncResetJob.perform_later("platform_statistic_service")
CacheAsyncResetJob.perform_later("user_statistic_service", self.project&.user_id)
end
end

View File

@ -79,8 +79,8 @@ class Issue < ApplicationRecord
after_destroy :update_closed_issues_count_in_project!, :reset_cache_data
def reset_cache_data
self.reset_platform_cache_async_job
self.reset_user_cache_async_job(self.user)
CacheAsyncResetJob.perform_later("platform_statistic_service")
CacheAsyncResetJob.perform_later("user_statistic_service", self.author_id)
end
def get_assign_user

View File

@ -1,20 +1,20 @@
# == Schema Information
#
# Table name: praise_treads
#
# id :integer not null, primary key
# user_id :integer not null
# praise_tread_object_id :integer
# praise_tread_object_type :string(255)
# praise_or_tread :integer default("1")
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# praise_tread (praise_tread_object_id,praise_tread_object_type)
#
# == Schema Information
#
# Table name: praise_treads
#
# id :integer not null, primary key
# user_id :integer not null
# praise_tread_object_id :integer
# praise_tread_object_type :string(255)
# praise_or_tread :integer default("1")
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# praise_tread (praise_tread_object_id,praise_tread_object_type)
#
class PraiseTread < ApplicationRecord
belongs_to :user
@ -26,9 +26,9 @@ class PraiseTread < ApplicationRecord
after_destroy :reset_cache_data
def reset_cache_data
self.reset_platform_cache_async_job
CacheAsyncResetJob.perform_later("platform_statistic_service")
if self.praise_tread_object.is_a?(Project)
self.reset_user_cache_async_job(self.praise_tread_object&.owner)
CacheAsyncResetJob.perform_later("user_statistic_service", self.praise_tread_object&.user_id)
end
end

View File

@ -151,11 +151,10 @@ class Project < ApplicationRecord
def reset_cache_data
if changes[:user_id].present?
first_owner = Owner.find_by_id(changes[:user_id].first)
self.reset_user_cache_async_job(first_owner)
CacheAsyncResetJob.perform_later("user_statistic_service", changes[:user_id].first)
end
self.reset_platform_cache_async_job
self.reset_user_cache_async_job(self.owner)
CacheAsyncResetJob.perform_later("platform_statistic_service")
CacheAsyncResetJob.perform_later("user_statistic_service", self.user_id)
end
def reset_unmember_followed

View File

@ -46,8 +46,8 @@ class PullRequest < ApplicationRecord
after_destroy :reset_cache_data
def reset_cache_data
self.reset_platform_cache_async_job
self.reset_user_cache_async_job(self.user)
CacheAsyncResetJob.perform_later("platform_statistic_service")
CacheAsyncResetJob.perform_later("user_statistic_service", self.user_id)
end
def fork_project

View File

@ -28,12 +28,12 @@ class Watcher < ApplicationRecord
def reset_cache_data
if self.watchable.is_a?(User)
self.reset_user_cache_async_job(self.watchable)
CacheAsyncResetJob.perform_later("user_statistic_service", self.watchable_id)
end
if self.watchable.is_a?(Project)
self.reset_user_cache_async_job(self.watchable&.owner)
CacheAsyncResetJob.perform_later("user_statistic_service", self.watchable&.user_id)
end
self.reset_platform_cache_async_job
CacheAsyncResetJob.perform_later("platform_statistic_service")
end
def send_create_message_to_notice_system