2018-04-26 19:38:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'singleton'
|
|
|
|
|
|
|
|
class EntityCache
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
MAX_EXPIRATION = 7.days.freeze
|
|
|
|
|
2020-04-05 06:51:22 -04:00
|
|
|
def status(url)
|
|
|
|
Rails.cache.fetch(to_key(:status, url), expires_in: MAX_EXPIRATION) { FetchRemoteStatusService.new.call(url) }
|
|
|
|
end
|
|
|
|
|
2018-04-26 19:38:10 -04:00
|
|
|
def mention(username, domain)
|
2020-01-23 16:00:13 -05:00
|
|
|
Rails.cache.fetch(to_key(:mention, username, domain), expires_in: MAX_EXPIRATION) { Account.select(:id, :username, :domain, :url).find_remote(username, domain) }
|
2018-04-26 19:38:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def emoji(shortcodes, domain)
|
2020-08-31 21:04:00 -04:00
|
|
|
shortcodes = Array(shortcodes)
|
2018-04-26 19:38:10 -04:00
|
|
|
cached = Rails.cache.read_multi(*shortcodes.map { |shortcode| to_key(:emoji, shortcode, domain) })
|
|
|
|
uncached_ids = []
|
|
|
|
|
|
|
|
shortcodes.each do |shortcode|
|
|
|
|
uncached_ids << shortcode unless cached.key?(to_key(:emoji, shortcode, domain))
|
|
|
|
end
|
|
|
|
|
|
|
|
unless uncached_ids.empty?
|
2021-01-12 03:27:38 -05:00
|
|
|
uncached = CustomEmoji.where(shortcode: shortcodes, domain: domain, disabled: false).index_by(&:shortcode)
|
2018-04-26 19:38:10 -04:00
|
|
|
uncached.each_value { |item| Rails.cache.write(to_key(:emoji, item.shortcode, domain), item, expires_in: MAX_EXPIRATION) }
|
|
|
|
end
|
|
|
|
|
2021-01-09 18:32:01 -05:00
|
|
|
shortcodes.filter_map { |shortcode| cached[to_key(:emoji, shortcode, domain)] || uncached[shortcode] }
|
2018-04-26 19:38:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_key(type, *ids)
|
|
|
|
"#{type}:#{ids.compact.map(&:downcase).join(':')}"
|
|
|
|
end
|
|
|
|
end
|