为了缓解数据库服务器的繁忙状态,准备加入memcache。
"SELECT * FROM table WHERE table.id = value" 这样的简单检索占了全局的30%,把这样的检索进行缓存可以很大程度的减少访问数据库的次数。Place.find(id)以及account.place这样的关联,会产生上边的简单检索。
Place.find(id)可以简单的用Place.get_cache(id)代替。
belongs_to和has_one关联的对应,参考了 http://jack.iteye.com/blog/169736 和 http://groups.google.co.jp/group/acts_as_cached/browse_thread/thread/5fefb2d2355a5048 。
module ActiveRecord
module Associations
class BelongsToAssociation
def find_target_with_cached_association
# is the associated class using it's own id as it's cache id?
if @reflection.klass.respond_to?(:get_cache) &&
(@reflection.klass.cache_config[:cache_id] || :id) == :id
@reflection.klass.get_cache(@owner.send(@reflection.primary_key_name))
else
find_target_without_cached_association
end
end
alias_method_chain :find_target, :cached_association
end
class HasOneAssociation
def find_target_with_cached_association
# is the associated class using this class as it's cache id?
if @reflection.klass.respond_to?(:get_cache)
@reflection.klass.get_cache("#{@reflection.primary_key_name}:#{@owner.id}") {
find_target_without_cached_association
}
else
find_target_without_cached_association
end
end
alias_method_chain :find_target, :cached_association
end
end
end
需要注意的是,采用这种方法,对于从表记录,其实产生了2个缓存。例如,Account:id,Account:name:<name>。在释放缓存的时候需要注意。
has_many关联的也想进行缓存,现在最大的问题不是如何写入缓存,而是修改后如何释放。Place A修改的时候,如何把所有包含A的集合的缓存全部释放。缓存被要求及时更新。修改者本人相关的集合缓存的释放相对来说比较容易,可是他人的集合缓存呢?突然想到mixi的缓存方法,作者本人可以马上看到更新,而其他人是要过几分钟才可以看到更新的。这个是比较容易实现的,只要在集合缓存时设定好时限就可以了。值得考虑一下。
分享到:
- 2008-09-08 23:15
- 浏览 1105
- 评论(0)
- 论坛回复 / 浏览 (0 / 2342)
- 查看更多
相关推荐
这个插件库添加了 ActiveRecord 模型,一种在保存时检查:belongs_to关联是否实际存在的方法。 这是通过向基本验证模块添加validates_existence_of器来实现的。 它还支持:allow_nil => true/false 、 :allow_new => ...
本文整理总结了thinkPHP3.x常用的...BELONGS_TO=2 BELONGS_TO 关联定义 HAS_MANY=3 HAS_MANY 关联定义 MANY_TO_MANY=4 MANY_TO_MANY 关联定义 THINK_VERSION 框架版本号 这些预定义常量不会随着环境的变化而变化。 二、
activemodel-associations, 用于普通 ruby 对象的has_many和belongs_to宏 ActiveModel::Associations 用于普通 ruby 对象的has_many 和 belongs_to 宏。安装将此行添加到你的应用程序的Gemfile中:gem 'activemodel-
通常我们所说的关联关系包括下面三种: ◇ 一对一关联 : ONE_TO_ONE , 包括 HAS_ONE 和 BELONGS_TO ◇ 一对多关联 : ONE_TO_MANY , 包括 HAS_MANY 和 BELONGS_TO ◇ 多对多关联 : MANY_TO_MANY 关联定义 数据表...
支持many_to_one / belongs_to,one_to_many / has_many,many_to_many / has_and_belongs_to_many和has_one / one_to_one关联 以不违反外键约束的方式加载灯具的依赖关系图 有一个非常简单的API...
管理::字段::属于搜索 一个插件,用于搜索 Rails引擎中的belongs_to关联。用法将其添加到您的Gemfile : gem 'administrate-field-belongs_to_search' 运行捆绑程序以安装: bundle install 将其添加到“管理”仪表...
当您具有诸如has_many , has_one或belongs_to has_many的父子关系时,您可以通过一个查询加载父记录,然后为每条记录触发另一个SQL语句以加载相关的孩子。 假设您有以下Ecto模式 defmodule User do use Ecto . ...
使用postgresql hstore列创建ActiveRecord Emirates_to关联。 与多态关联兼容,并支持预先加载。 安装 将此行添加到您的应用程序的Gemfile中: gem 'activerecord-postgres-hstore' # Rails 3 only gem 'belongs_...
Laravel开发-belongs-to-one 属于一个Laravel 5关系类
包含产品的属性,尤其是价格(代表美分的整数),与User的belongs_to关联和与Purchases的has_many关联。 此模型的迁移需要: 用户索引: t.index ["user_id"], name: "index_products_on_user_id"以有效地搜索用户...
该插件使用PostgreSQL数组的功能实现了在Rails中进行has_and_belongs_to_many关联的替代方法。 在很多情况下,你只需要或的功能使用许多一对多与连接表是不必要的传统方法。 我们可以只存储id的整数数组。 它是如何...
缓存 便利包装器 在您的类中使用has_cache提供了一个cached方法,该方法允许自动缓存通常在类或类的实例上可用的方法的... belongs_to :user , inverse_of : :posts end 填充和检索缓存的实体 在类上启用缓存后,您可以
偏好 has_many :through 胜于 has_and_belongs_to_many。 使用 has_many :through 允许在 join 模型有附加的属性及验证 # 使用 has_and_belongs_to_many class User < ActiveRecord::Base has_and_...
has_many和belongs_to其实是Topic类的class method,标准写法是: 代码如下: class Topic < ActiveRecord::Base Topic.has_many(:posts) Topic.belongs_to(:user) end 那么has_many可以给我们带来什么呢?...
您有三个模型,以及一个与所有模型交互的表单: class User has_one :profile has_one :accountendclass Profile belongs_to :userendclass Account belongs_to :userend因此,您创建了一个管理员用户表单演示者类...
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。博客类别在本课程中,我们将设置一个...
ApplicationRecord belongs_to :user has_many :commentsendclass User < ApplicationRecordhas_many :postshas_many :commentsendclass Comment < ApplicationRecord belongs_to :user belongs_to :postend...
外键(hasMany、belongsTo、...) 例子 获取相关文档 var sub = require ( 'level-sublevel' ) ; var level = require ( 'level' ) ; var db = sub ( level ( 'hackerspaces.db' , { valueEncoding : 'json' } ) ) ...
使用有关主要对象的数据以及belongs_to和has_many关联构造嵌套的params哈希。 对关联数据使用常规键名(association_attributes)。 正确输入名称表单,以创建一个嵌套的params散列,该散列具有belongs_to和has_...
创建has_many和belongs_to关联。 通过控制台和db/seeds.rb构建关联的数据。 使用关联提供的方法查询关联数据。 将关联数据嵌入视图中。 遍历显示单个实例的视图内的关联数据。博客类别在本课程中,我们将设置一个...