forked from treehouse/mastodon
Further abstract caching for includes
parent
356d3874eb
commit
a21bcac9e1
|
@ -59,6 +59,8 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def cache_collection(raw, klass)
|
def cache_collection(raw, klass)
|
||||||
|
return raw unless klass.respond_to?(:with_includes)
|
||||||
|
|
||||||
uncached_ids = []
|
uncached_ids = []
|
||||||
cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key))
|
cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key))
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Cacheable
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
class_methods do
|
||||||
|
def cache_associated(*associations)
|
||||||
|
@cache_associated = associations
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
included do
|
||||||
|
scope :with_includes, -> { includes(@cache_associated) }
|
||||||
|
end
|
||||||
|
end
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
class Notification < ApplicationRecord
|
class Notification < ApplicationRecord
|
||||||
include Paginable
|
include Paginable
|
||||||
|
include Cacheable
|
||||||
|
|
||||||
belongs_to :account
|
belongs_to :account
|
||||||
belongs_to :activity, polymorphic: true
|
belongs_to :activity, polymorphic: true
|
||||||
|
@ -15,7 +16,7 @@ class Notification < ApplicationRecord
|
||||||
|
|
||||||
STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze
|
STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze
|
||||||
|
|
||||||
scope :with_includes, -> { includes(status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account) }
|
cache_associated status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account
|
||||||
|
|
||||||
def activity
|
def activity
|
||||||
send(activity_type.downcase)
|
send(activity_type.downcase)
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
class Status < ApplicationRecord
|
class Status < ApplicationRecord
|
||||||
include Paginable
|
include Paginable
|
||||||
include Streamable
|
include Streamable
|
||||||
|
include Cacheable
|
||||||
|
|
||||||
belongs_to :account, inverse_of: :statuses
|
belongs_to :account, inverse_of: :statuses
|
||||||
|
|
||||||
|
@ -27,7 +28,8 @@ class Status < ApplicationRecord
|
||||||
default_scope { order('id desc') }
|
default_scope { order('id desc') }
|
||||||
|
|
||||||
scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') }
|
scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') }
|
||||||
scope :with_includes, -> { includes(:account, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account) }
|
|
||||||
|
cache_associated :account, :media_attachments, :tags, :stream_entry, mentions: :account, reblog: [:account, :stream_entry, :tags, :media_attachments, mentions: :account], thread: :account
|
||||||
|
|
||||||
def local?
|
def local?
|
||||||
uri.nil?
|
uri.nil?
|
||||||
|
|
Loading…
Reference in New Issue