Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
af1a4bdc
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
af1a4bdc
编写于
4月 10, 2013
作者:
N
Noemj
提交者:
Olli Rissanen
4月 10, 2013
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added statement cache
上级
45321a69
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
108 addition
and
0 deletion
+108
-0
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+17
-0
activerecord/lib/active_record.rb
activerecord/lib/active_record.rb
+1
-0
activerecord/lib/active_record/statement_cache.rb
activerecord/lib/active_record/statement_cache.rb
+26
-0
activerecord/test/cases/statement_cache_test.rb
activerecord/test/cases/statement_cache_test.rb
+64
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
af1a4bdc
## Rails 4.0.0 (unreleased) ##
*
Added Statement Cache to allow the caching of a single statement. The cache works by
duping the relation returned from yielding a statement which allows skipping the AST
building phase.
Example:
cache = ActiveRecord::StatementCache.new do
Book.where(:name => "my book").limit(100)
end
books = cache.execute
The solution attempts to get closer to the speed of `find_by_sql` but still maintaining
the expressiveness of the AR queries.
*Olli Rissanen*
*
Fixing issue #8345. Now throwing an error when one attempts to touch a
new object that has not yet been persisted. For instance:
...
...
activerecord/lib/active_record.rb
浏览文件 @
af1a4bdc
...
...
@@ -56,6 +56,7 @@ module ActiveRecord
autoload
:SchemaMigration
autoload
:Scoping
autoload
:Serialization
autoload
:StatementCache
autoload
:Store
autoload
:Timestamp
autoload
:Transactions
...
...
activerecord/lib/active_record/statement_cache.rb
0 → 100644
浏览文件 @
af1a4bdc
module
ActiveRecord
# Statement cache is used to cache a single statement in order to avoid creating the AST again.
# Initializing the cache is done by passing the statement in the initialization block:
#
# cache = ActiveRecord::StatementCache.new do
# Book.where(:name => "my book").limit(100)
# end
#
# The cached statement is executed by using the +execute+ method:
#
# cache.execute
#
# The relation returned by yield is cached, and for each +execute+ call the cached relation gets duped.
# Database is queried when +to_a+ is called on the relation.
class
StatementCache
def
initialize
@relation
=
yield
raise
ArgumentError
.
new
(
"Statement cannot be nil"
)
if
@relation
.
nil?
end
def
execute
@relation
.
dup
.
to_a
end
end
end
activerecord/test/cases/statement_cache_test.rb
0 → 100644
浏览文件 @
af1a4bdc
require
'cases/helper'
require
'models/book'
require
'models/liquid'
require
'models/molecule'
require
'models/electron'
module
ActiveRecord
class
StatementCacheTest
<
ActiveRecord
::
TestCase
def
setup
@connection
=
ActiveRecord
::
Base
.
connection
end
def
test_statement_cache_with_simple_statement
cache
=
ActiveRecord
::
StatementCache
.
new
do
Book
.
where
(
name:
"my book"
).
where
(
"author_id > 3"
)
end
Book
.
create
(
name:
"my book"
,
author_id:
4
)
books
=
cache
.
execute
assert_equal
"my book"
,
books
[
0
].
name
end
def
test_statement_cache_with_nil_statement_raises_error
assert_raise
(
ArgumentError
)
do
cache
=
ActiveRecord
::
StatementCache
.
new
do
nil
end
end
end
def
test_statement_cache_with_complex_statement
cache
=
ActiveRecord
::
StatementCache
.
new
do
Liquid
.
joins
(
:molecules
=>
:electrons
).
where
(
'molecules.name'
=>
'dioxane'
,
'electrons.name'
=>
'lepton'
)
end
salty
=
Liquid
.
create
(
name:
'salty'
)
molecule
=
salty
.
molecules
.
create
(
name:
'dioxane'
)
electron
=
molecule
.
electrons
.
create
(
name:
'lepton'
)
liquids
=
cache
.
execute
assert_equal
"salty"
,
liquids
[
0
].
name
end
def
test_statement_cache_values_differ
cache
=
ActiveRecord
::
StatementCache
.
new
do
Book
.
where
(
name:
"my book"
)
end
for
i
in
0
..
2
do
Book
.
create
(
name:
"my book"
)
end
first_books
=
cache
.
execute
for
i
in
0
..
2
do
Book
.
create
(
name:
"my book"
)
end
additional_books
=
cache
.
execute
assert
first_books
!=
additional_books
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录