Mohit Jain
Mohit Jain

Reputation: 43939

How to implement page caching or action caching in active admin

We are using active admin in our application. I have massive data etc to be managed. I want to implement page caching/ action caching in active admin and wants to expire fragments on my specific calls. I don't mind showing stale data for some time on index pages. Can some one provide me some basic example how to implement page_caching/action_caching in active admin?

Upvotes: 1

Views: 1625

Answers (1)

scarver2
scarver2

Reputation: 7995

Here is an example solution from this thread: 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

Credit for the source code goes to @CMaresh https://stackoverflow.com/users/302824/cmaresh

Upvotes: 3

Related Questions