Can Can selectively cache abilities

限于喜欢 提交于 2019-12-11 20:22:38

问题


I know about caching the whole current_ability.

Something like

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

But is there is a way to cache a single ability ? for example I do this :

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

回答1:


I did this to cache the whole current_ability object

In ApplicationController

def current_ability
  @current_ability = Rails.cache.fetch("#{current_user.cache_key}::ability") do
    super
  end
end unless Rails.env.development?


来源:https://stackoverflow.com/questions/18849482/can-can-selectively-cache-abilities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!