【发布时间】:2011-11-26 11:05:27
【问题描述】:
在 Rails 中向模型添加缓存时,存在重复性,如下所示:
class Team < ActiveRecord::Base
attr_accessible :name
end
Before caching, to retrieve a name, everything was trivial,
team = Team.new(:name => "The Awesome Team")
team.save
team.name # "The Awesome Team"
使用 memcached 或 redis 引入缓存后,我发现自己在模型中添加了方法,而且非常重复:
def get_name
if name_is_in_cache
return cached_name
else
name
end
end
def set_name(name)
# set name in cache
self.name = name
end
我是否缺少一些明显的方法来清理它?我以不同的方式缓存了很多字段,目前看来attr_accessible 几乎是多余的。如何清理?
【问题讨论】:
-
您能否添加一些强制您使用缓存的复杂/性能密集型方法的示例?
标签: ruby-on-rails ruby caching activerecord redis