Create Redisable#redis (#9633)

* Create Redisable

* Use #redis instead of Redis.current
This commit is contained in:
ysksn 2019-02-03 03:11:38 +09:00 committed by Eugen Rochko
parent 6a5e3da6b0
commit bcfff65195
13 changed files with 39 additions and 59 deletions

View File

@ -4,6 +4,8 @@ class ActivityTracker
EXPIRE_AFTER = 90.days.seconds EXPIRE_AFTER = 90.days.seconds
class << self class << self
include Redisable
def increment(prefix) def increment(prefix)
key = [prefix, current_week].join(':') key = [prefix, current_week].join(':')
@ -20,10 +22,6 @@ class ActivityTracker
private private
def redis
Redis.current
end
def current_week def current_week
Time.zone.today.cweek Time.zone.today.cweek
end end

View File

@ -2,6 +2,7 @@
class ActivityPub::Activity class ActivityPub::Activity
include JsonLdHelper include JsonLdHelper
include Redisable
def initialize(json, account, **options) def initialize(json, account, **options)
@json = json @json = json
@ -70,10 +71,6 @@ class ActivityPub::Activity
@object_uri ||= value_or_id(@object) @object_uri ||= value_or_id(@object)
end end
def redis
Redis.current
end
def distribute(status) def distribute(status)
crawl_links(status) crawl_links(status)

View File

@ -4,6 +4,7 @@ require 'singleton'
class FeedManager class FeedManager
include Singleton include Singleton
include Redisable
MAX_ITEMS = 400 MAX_ITEMS = 400
@ -35,7 +36,7 @@ class FeedManager
def unpush_from_home(account, status) def unpush_from_home(account, status)
return false unless remove_from_feed(:home, account.id, status) return false unless remove_from_feed(:home, account.id, status)
Redis.current.publish("timeline:#{account.id}", Oj.dump(event: :delete, payload: status.id.to_s)) redis.publish("timeline:#{account.id}", Oj.dump(event: :delete, payload: status.id.to_s))
true true
end end
@ -53,7 +54,7 @@ class FeedManager
def unpush_from_list(list, status) def unpush_from_list(list, status)
return false unless remove_from_feed(:list, list.id, status) return false unless remove_from_feed(:list, list.id, status)
Redis.current.publish("timeline:list:#{list.id}", Oj.dump(event: :delete, payload: status.id.to_s)) redis.publish("timeline:list:#{list.id}", Oj.dump(event: :delete, payload: status.id.to_s))
true true
end end
@ -142,10 +143,6 @@ class FeedManager
private private
def redis
Redis.current
end
def push_update_required?(timeline_id) def push_update_required?(timeline_id)
redis.exists("subscribed:#{timeline_id}") redis.exists("subscribed:#{timeline_id}")
end end

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class OStatus::Activity::Base class OStatus::Activity::Base
include Redisable
def initialize(xml, account = nil, **options) def initialize(xml, account = nil, **options)
@xml = xml @xml = xml
@account = account @account = account
@ -66,8 +68,4 @@ class OStatus::Activity::Base
Status.find_by(uri: uri) Status.find_by(uri: uri)
end end
end end
def redis
Redis.current
end
end end

View File

@ -11,6 +11,8 @@ class PotentialFriendshipTracker
}.freeze }.freeze
class << self class << self
include Redisable
def record(account_id, target_account_id, action) def record(account_id, target_account_id, action)
return if account_id == target_account_id return if account_id == target_account_id
@ -31,11 +33,5 @@ class PotentialFriendshipTracker
return [] if account_ids.empty? return [] if account_ids.empty?
Account.searchable.where(id: account_ids) Account.searchable.where(id: account_ids)
end end
private
def redis
Redis.current
end
end end
end end

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
module Redisable
extend ActiveSupport::Concern
private
def redis
Redis.current
end
end

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class Feed class Feed
include Redisable
def initialize(type, id) def initialize(type, id)
@type = type @type = type
@id = id @id = id
@ -27,8 +29,4 @@ class Feed
def key def key
FeedManager.instance.key(@type, @id) FeedManager.instance.key(@type, @id)
end end
def redis
Redis.current
end
end end

View File

@ -7,6 +7,8 @@ class TrendingTags
THRESHOLD = 5 THRESHOLD = 5
class << self class << self
include Redisable
def record_use!(tag, account, at_time = Time.now.utc) def record_use!(tag, account, at_time = Time.now.utc)
return if disallowed_hashtags.include?(tag.name) || account.silenced? || account.bot? return if disallowed_hashtags.include?(tag.name) || account.silenced? || account.bot?
@ -59,9 +61,5 @@ class TrendingTags
@disallowed_hashtags = @disallowed_hashtags.split(' ') if @disallowed_hashtags.is_a? String @disallowed_hashtags = @disallowed_hashtags.split(' ') if @disallowed_hashtags.is_a? String
@disallowed_hashtags = @disallowed_hashtags.map(&:downcase) @disallowed_hashtags = @disallowed_hashtags.map(&:downcase)
end end
def redis
Redis.current
end
end end
end end

View File

@ -2,6 +2,7 @@
class BatchedRemoveStatusService < BaseService class BatchedRemoveStatusService < BaseService
include StreamEntryRenderer include StreamEntryRenderer
include Redisable
# Delete given statuses and reblogs of them # Delete given statuses and reblogs of them
# Dispatch PuSH updates of the deleted statuses, but only local ones # Dispatch PuSH updates of the deleted statuses, but only local ones
@ -109,10 +110,6 @@ class BatchedRemoveStatusService < BaseService
end end
end end
def redis
Redis.current
end
def build_xml(stream_entry) def build_xml(stream_entry)
return @activity_xml[stream_entry.id] if @activity_xml.key?(stream_entry.id) return @activity_xml[stream_entry.id] if @activity_xml.key?(stream_entry.id)

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class FollowService < BaseService class FollowService < BaseService
include Redisable
# Follow a remote user, notify remote user about the follow # Follow a remote user, notify remote user about the follow
# @param [Account] source_account From which to follow # @param [Account] source_account From which to follow
# @param [String, Account] uri User URI to follow in the form of username@domain (or account record) # @param [String, Account] uri User URI to follow in the form of username@domain (or account record)
@ -67,10 +69,6 @@ class FollowService < BaseService
follow follow
end end
def redis
Redis.current
end
def build_follow_request_xml(follow_request) def build_follow_request_xml(follow_request)
OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_request_salmon(follow_request)) OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_request_salmon(follow_request))
end end

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class PostStatusService < BaseService class PostStatusService < BaseService
include Redisable
MIN_SCHEDULE_OFFSET = 5.minutes.freeze MIN_SCHEDULE_OFFSET = 5.minutes.freeze
# Post a text status update, fetch and notify remote users mentioned # Post a text status update, fetch and notify remote users mentioned
@ -110,10 +112,6 @@ class PostStatusService < BaseService
ProcessHashtagsService.new ProcessHashtagsService.new
end end
def redis
Redis.current
end
def scheduled? def scheduled?
@scheduled_at.present? @scheduled_at.present?
end end

View File

@ -2,6 +2,7 @@
class RemoveStatusService < BaseService class RemoveStatusService < BaseService
include StreamEntryRenderer include StreamEntryRenderer
include Redisable
def call(status, **options) def call(status, **options)
@payload = Oj.dump(event: :delete, payload: status.id.to_s) @payload = Oj.dump(event: :delete, payload: status.id.to_s)
@ -55,7 +56,7 @@ class RemoveStatusService < BaseService
def remove_from_affected def remove_from_affected
@mentions.map(&:account).select(&:local?).each do |account| @mentions.map(&:account).select(&:local?).each do |account|
Redis.current.publish("timeline:#{account.id}", @payload) redis.publish("timeline:#{account.id}", @payload)
end end
end end
@ -133,26 +134,22 @@ class RemoveStatusService < BaseService
return unless @status.public_visibility? return unless @status.public_visibility?
@tags.each do |hashtag| @tags.each do |hashtag|
Redis.current.publish("timeline:hashtag:#{hashtag}", @payload) redis.publish("timeline:hashtag:#{hashtag}", @payload)
Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if @status.local? redis.publish("timeline:hashtag:#{hashtag}:local", @payload) if @status.local?
end end
end end
def remove_from_public def remove_from_public
return unless @status.public_visibility? return unless @status.public_visibility?
Redis.current.publish('timeline:public', @payload) redis.publish('timeline:public', @payload)
Redis.current.publish('timeline:public:local', @payload) if @status.local? redis.publish('timeline:public:local', @payload) if @status.local?
end end
def remove_from_media def remove_from_media
return unless @status.public_visibility? return unless @status.public_visibility?
Redis.current.publish('timeline:public:media', @payload) redis.publish('timeline:public:media', @payload)
Redis.current.publish('timeline:public:local:media', @payload) if @status.local? redis.publish('timeline:public:local:media', @payload) if @status.local?
end
def redis
Redis.current
end end
end end

View File

@ -2,6 +2,7 @@
class Scheduler::FeedCleanupScheduler class Scheduler::FeedCleanupScheduler
include Sidekiq::Worker include Sidekiq::Worker
include Redisable
sidekiq_options unique: :until_executed, retry: 0 sidekiq_options unique: :until_executed, retry: 0
@ -57,8 +58,4 @@ class Scheduler::FeedCleanupScheduler
def feed_manager def feed_manager
FeedManager.instance FeedManager.instance
end end
def redis
Redis.current
end
end end