add: user common info cache service

This commit is contained in:
yystopf 2021-10-25 17:23:17 +08:00
parent f9bb74aba4
commit fa476732e5
5 changed files with 28 additions and 10 deletions

View File

@ -51,6 +51,8 @@ class UsersController < ApplicationController
@projects_common_count = user_projects.common.size
@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
end
def watch_users

View File

@ -1,10 +1,8 @@
class CacheAsyncResetJob < ApplicationJob
queue_as :cache
def perform(type, id, params={})
def perform(type, id)
case type
when "owner_common_service"
Cache::V2::OwnerCommonService.new(id).reset
when "platform_statistic_service"
Cache::V2::PlatformStatisticService.new.reset
when "project_common_service"

View File

@ -3,8 +3,6 @@ class CacheAsyncResetJob < ApplicationJob
def perform(type, id, params={})
case type
when "owner_common_service"
Cache::V2::OwnerCommonService.new(id, params).call
when "platform_statistic_service"
Cache::V2::PlatformStatisticService.new(params).call
when "project_common_service"

View File

@ -3,10 +3,10 @@ class Cache::V2::OwnerCommonService < ApplicationService
attr_reader :owner_id, :login, :name, :avatar_url, :email
attr_accessor :owner
def initialize(owner_id, params={})
@owner_id = owner_id
def initialize(login, email, params={})
@login = login
@email = email
@name = params[:name]
@email = params[:email]
end
def read
@ -24,11 +24,11 @@ class Cache::V2::OwnerCommonService < ApplicationService
private
def load_owner
@owner = User.find_by_id(@owner_id)
@owner = User.find_by(login: @login)
end
def owner_common_key
"v2-owner-common:#{@owner_id}"
"v2-owner-common:#{@login}-#{@email.to_s}"
end
def owner_common
@ -58,6 +58,9 @@ class Cache::V2::OwnerCommonService < ApplicationService
$redis_cache.hgetall(owner_common_key)
end
def reset_owner_id
$redis_cache.hset(owner_common_key, "id", owner&.id)
end
def reset_owner_type
$redis_cache.hset(owner_common_key, "type", owner&.type)
@ -79,6 +82,7 @@ class Cache::V2::OwnerCommonService < ApplicationService
def reset_owner_common
load_owner
$redis_cache.del(owner_common_key)
reset_owner_id
reset_owner_type
reset_owner_login
reset_owner_email

View File

@ -0,0 +1,16 @@
user = $redis_cache.hgetall("v2-owner-common:#{name}-#{email}")
if user.blank?
json.id nil
json.type nil
json.login name
json.name name
json.email email
json.image_url User::Avatar.get_letter_avatar_url(name)
else
json.id user["id"]
json.type user["type"]
json.login user["login"]
json.name user["name"]
json.email user["email"]
json.image_url user["avatar_url"]
end