| 
									
										
										
										
											2018-04-27 01:38:10 +02:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require 'singleton' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EntityCache | 
					
						
							|  |  |  |   include Singleton | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   MAX_EXPIRATION = 7.days.freeze | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 12:51:22 +02:00
										 |  |  |   def status(url) | 
					
						
							|  |  |  |     Rails.cache.fetch(to_key(:status, url), expires_in: MAX_EXPIRATION) { FetchRemoteStatusService.new.call(url) } | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 01:38:10 +02:00
										 |  |  |   def mention(username, domain) | 
					
						
							| 
									
										
										
										
											2020-01-23 22:00:13 +01: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-27 01:38:10 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def emoji(shortcodes, domain) | 
					
						
							| 
									
										
										
										
											2021-03-18 00:41:32 +01:00
										 |  |  |     shortcodes = Array(shortcodes) | 
					
						
							|  |  |  |     return [] if shortcodes.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 01:38:10 +02: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 09:27:38 +01:00
										 |  |  |       uncached = CustomEmoji.where(shortcode: shortcodes, domain: domain, disabled: false).index_by(&:shortcode) | 
					
						
							| 
									
										
										
										
											2018-04-27 01:38:10 +02: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-27 01:38:10 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def to_key(type, *ids) | 
					
						
							|  |  |  |     "#{type}:#{ids.compact.map(&:downcase).join(':')}" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |