我知道缓存整个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