1

アプリケーションでアクティブな管理者を使用しています。大量のデータなどを管理しています。アクティブな管理者にページ キャッシング/アクション キャッシングを実装し、特定の呼び出しでフラグメントを期限切れにしたいと考えています。しばらくの間、古いデータがインデックス ページに表示されてもかまいません。アクティブな管理者に page_caching/action_caching を実装する方法の基本的な例を教えてもらえますか?

4

1 に答える 1

3

このスレッドの解決策の例を次に示します: https://github.com/activeadmin/activeadmin/issues/2263#issuecomment-20249617

# application_helper.rb
# Caches Arbre output.
#
# context - ActiveAdmin instance context
# args    - Arguments passed to Rails.cache calls.
#
# Yielding the first time adds to the output buffer regardless of the
# returned value. The missed cache must be handled carefully.
#
# Returns yielded Arbre on cache miss OR an HTML string wrapped in
# an Arbre div on cache hit.
def cache_arbre(context, *args)
  if controller.perform_caching
    if Rails.cache.exist?(*args)
      context.instance_eval do
        div(Rails.cache.read(*args))
      end
    else
      Rails.cache.write(*args, yield.to_s)
    end
  else
    yield
  end
end

# Example Usage would be like the following:
ActiveAdmin.register User do
  show do
    arbre_cache(self, user.cache_key) do
      attributes_table do
        row :name
        row :email
        row :expensive_calculation
      end
    end
  end
end

ソースコードのクレジットは @CMaresh https://stackoverflow.com/users/302824/cmareshにあります

于 2014-10-20T22:22:36.313 に答える