如何在活动管理中实现页面缓存或操作缓存

时间:2014-10-20 12:27:51

标签: ruby-on-rails ruby-on-rails-3 activeadmin page-caching action-caching

我们在应用程序中使用了活动管理员。我有大量的数据等需要管理。我想在活动管理员中实现页面缓存/动作缓存,并希望在我的特定调用中使片段过期。我不介意在索引页面上显示陈旧数据一段时间。有人能为我提供一些如何在主动管理中实现page_caching / action_caching的基本示例吗?

1 个答案:

答案 0 :(得分:1)

以下是此主题的示例解决方案: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