可以选择性地缓存能力 [英] Can Can selectively cache abilities
本文介绍了可以选择性地缓存能力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道要缓存整个 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
但是,有没有一种方法可以缓存单个能力?例如,
就是这样:
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
推荐答案
我这样做是为了缓存整个 current_ability
对象
I did this to cache the whole current_ability
object
在 ApplicationController
In ApplicationController
def current_ability
@current_ability = Rails.cache.fetch("#{current_user.cache_key}::ability") do
super
end
end unless Rails.env.development?
这篇关于可以选择性地缓存能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文