0

我知道缓存整个current_ability.

就像是

def current_ability
  cached_ability = Rails.cache.read("#{current_user.cache_key}::ability")
  if cached_ability.present?
    ability = Marshal.load( cached_ability )
  else
    ability = super
    Rails.cache.write("#{current_user.cache_key}::ability", Marshal.dump( ability ))
  end
  @current_ability = ability
end

但是有没有办法缓存单个能力?例如我这样做:

Rails.cache.fetch("#{user.cache_key}::comments::ability") do
  can :manage, Comment, :article_id => articles_ids
end
4

1 回答 1

4

我这样做是为了缓存整个current_ability对象

ApplicationController

def current_ability
  @current_ability = Rails.cache.fetch("#{current_user.cache_key}::ability") do
    super
  end
end unless Rails.env.development?
于 2014-01-15T07:06:44.087 回答