Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
e0af93dd
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 搜索 >>
提交
e0af93dd
编写于
4月 09, 2013
作者:
J
Jeremy Kemper
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #10156 from wangjohn/grouping_thread_locals
Grouping thread locals in ActiveRecord
上级
9039c503
e12901e4
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
92 addition
and
19 deletion
+92
-19
activerecord/lib/active_record.rb
activerecord/lib/active_record.rb
+1
-0
activerecord/lib/active_record/connection_handling.rb
activerecord/lib/active_record/connection_handling.rb
+2
-2
activerecord/lib/active_record/core.rb
activerecord/lib/active_record/core.rb
+2
-2
activerecord/lib/active_record/log_subscriber.rb
activerecord/lib/active_record/log_subscriber.rb
+2
-2
activerecord/lib/active_record/runtime_registry.rb
activerecord/lib/active_record/runtime_registry.rb
+32
-0
activerecord/lib/active_record/scoping.rb
activerecord/lib/active_record/scoping.rb
+6
-6
activerecord/test/cases/base_test.rb
activerecord/test/cases/base_test.rb
+2
-2
activesupport/lib/active_support/notifications.rb
activesupport/lib/active_support/notifications.rb
+4
-5
activesupport/lib/active_support/per_thread_registry.rb
activesupport/lib/active_support/per_thread_registry.rb
+41
-0
未找到文件。
activerecord/lib/active_record.rb
浏览文件 @
e0af93dd
...
...
@@ -50,6 +50,7 @@ module ActiveRecord
autoload
:Querying
autoload
:ReadonlyAttributes
autoload
:Reflection
autoload
:RuntimeRegistry
autoload
:Sanitization
autoload
:Schema
autoload
:SchemaDumper
...
...
activerecord/lib/active_record/connection_handling.rb
浏览文件 @
e0af93dd
...
...
@@ -54,11 +54,11 @@ def connection
end
def
connection_id
Thread
.
current
[
'ActiveRecord::Base.connection_id'
]
ActiveRecord
::
RuntimeRegistry
.
instance
.
connection_id
end
def
connection_id
=
(
connection_id
)
Thread
.
current
[
'ActiveRecord::Base.connection_id'
]
=
connection_id
ActiveRecord
::
RuntimeRegistry
.
instance
.
connection_id
=
connection_id
end
# Returns the configuration of the associated connection as a hash:
...
...
activerecord/lib/active_record/core.rb
浏览文件 @
e0af93dd
...
...
@@ -80,11 +80,11 @@ module Core
class_attribute
:default_connection_handler
,
instance_writer:
false
def
self
.
connection_handler
Thread
.
current
[
:active_record_connection_handler
]
||
self
.
default_connection_handler
ActiveRecord
::
RuntimeRegistry
.
instance
.
connection_handler
||
self
.
default_connection_handler
end
def
self
.
connection_handler
=
(
handler
)
Thread
.
current
[
:active_record_connection_handler
]
=
handler
ActiveRecord
::
RuntimeRegistry
.
instance
.
connection_handler
=
handler
end
self
.
default_connection_handler
=
ConnectionAdapters
::
ConnectionHandler
.
new
...
...
activerecord/lib/active_record/log_subscriber.rb
浏览文件 @
e0af93dd
...
...
@@ -3,11 +3,11 @@ class LogSubscriber < ActiveSupport::LogSubscriber
IGNORE_PAYLOAD_NAMES
=
[
"SCHEMA"
,
"EXPLAIN"
]
def
self
.
runtime
=
(
value
)
Thread
.
current
[
:active_record_sql_runtime
]
=
value
ActiveRecord
::
RuntimeRegistry
.
instance
.
sql_runtime
=
value
end
def
self
.
runtime
Thread
.
current
[
:active_record_sql_runtime
]
||=
0
ActiveRecord
::
RuntimeRegistry
.
instance
.
sql_runtime
||=
0
end
def
self
.
reset_runtime
...
...
activerecord/lib/active_record/runtime_registry.rb
0 → 100644
浏览文件 @
e0af93dd
require
'active_support/per_thread_registry'
module
ActiveRecord
# This is a registry class for storing local variables in active record. The
# class allows you to access variables that are local to the current thread.
# These thread local variables are stored as attributes in the
# +RuntimeRegistry+ class.
#
# You can access the thread local variables by calling a variable's name on
# the +RuntimeRegistry+ class. For example, if you wanted to obtain the
# connection handler for the current thread, you would invoke:
#
# ActiveRecord::RuntimeRegistry.instance.connection_handler
#
# The +PerThreadRegistry+ module will make a new +RuntimeRegistry+ instance
# and store it in +Thread.current+. Whenever you make a call for an attribute
# on the +RuntimeRegistry+ class, the call will be sent to the instance that
# is stored in +Thread.current+.
#
# Note that you can also make the following call which would provide an
# equivalent result as the previous code:
#
# ActiveRecord::RuntimeRegistry.connection_handler
#
# However, this is less performant because it makes a call to +method_missing+
# before it sends the method call to the +instance+.
class
RuntimeRegistry
extend
ActiveSupport
::
PerThreadRegistry
attr_accessor
:connection_handler
,
:sql_runtime
,
:connection_id
end
end
activerecord/lib/active_record/scoping.rb
浏览文件 @
e0af93dd
require
'active_support/per_thread_registry'
module
ActiveRecord
module
Scoping
extend
ActiveSupport
::
Concern
...
...
@@ -34,7 +36,7 @@ def populate_with_current_scope_attributes
# to get the current_scope for the +Board+ model, then you would use the
# following code:
#
# registry = ActiveRecord::Scoping::ScopeRegistry.
current
# registry = ActiveRecord::Scoping::ScopeRegistry.
instance
# registry.set_value_for(:current_scope, "Board", some_new_scope)
#
# Now when you run:
...
...
@@ -48,12 +50,10 @@ def populate_with_current_scope_attributes
# ActiveRecord::Scoping::ScopeRegistry.set_value_for(:current_scope,
# "Board", some_new_scope)
class
ScopeRegistry
# :nodoc:
class
<<
self
delegate
:value_for
,
:set_value_for
,
to: :current
extend
ActiveSupport
::
PerThreadRegistry
def
current
Thread
.
current
[
"scope_registry"
]
||=
new
end
class
<<
self
delegate
:value_for
,
:set_value_for
,
to: :instance
end
VALID_SCOPE_TYPES
=
[
:current_scope
,
:ignore_default_scope
]
...
...
activerecord/test/cases/base_test.rb
浏览文件 @
e0af93dd
...
...
@@ -1377,9 +1377,9 @@ def test_current_scope_is_reset
UnloadablePost
.
send
(
:current_scope
=
,
UnloadablePost
.
all
)
UnloadablePost
.
unloadable
assert_not_nil
ActiveRecord
::
Scoping
::
ScopeRegistry
.
current
.
value_for
(
:current_scope
,
"UnloadablePost"
)
assert_not_nil
ActiveRecord
::
Scoping
::
ScopeRegistry
.
instance
.
value_for
(
:current_scope
,
"UnloadablePost"
)
ActiveSupport
::
Dependencies
.
remove_unloadable_constants!
assert_nil
ActiveRecord
::
Scoping
::
ScopeRegistry
.
current
.
value_for
(
:current_scope
,
"UnloadablePost"
)
assert_nil
ActiveRecord
::
Scoping
::
ScopeRegistry
.
instance
.
value_for
(
:current_scope
,
"UnloadablePost"
)
ensure
Object
.
class_eval
{
remove_const
:UnloadablePost
}
if
defined?
(
UnloadablePost
)
end
...
...
activesupport/lib/active_support/notifications.rb
浏览文件 @
e0af93dd
require
'active_support/notifications/instrumenter'
require
'active_support/notifications/fanout'
require
'active_support/per_thread_registry'
module
ActiveSupport
# = Notifications
...
...
@@ -190,12 +191,10 @@ def instrumenter
# The instrumenters for multiple notifiers are held in a single instance of
# this class.
class
InstrumentationRegistry
# :nodoc:
class
<<
self
delegate
:instrumenter_for
,
to: :current
extend
ActiveSupport
::
PerThreadRegistry
def
current
Thread
.
current
[
:instrumentation_registry
]
||=
new
end
class
<<
self
delegate
:instrumenter_for
,
to: :instance
end
def
initialize
...
...
activesupport/lib/active_support/per_thread_registry.rb
0 → 100644
浏览文件 @
e0af93dd
module
ActiveSupport
# This module creates a local registry class inside each thread. It provides
# basic methods which will store thread locals in a single class. This
# prevents the proliferation of too many thread locals and allows you to
# explicitly keep track of each of the variables you're using as thread
# locals in a class which includes this module.
#
# For example, instead of using a bunch of different thread locals to keep
# track of some variables like so:
#
# Thread.current[:active_record_connection_handler] = connection_handler
# Thread.current[:active_record_sql_runtime] = sql_runtime
#
# You could use the following class which implements the +PerThreadRegistry+
# module:
#
# class NewRegistry
# extend ActiveSupport::PerThreadRegistry
#
# attr_accessor :connection_handler, :sql_runtime
# end
#
# NewRegistry.instance.connection_handler = connection_handler
# NewRegistry.instance.sql_runtime = sql_runtime
#
# The new way of keeping track of the thread locals will create a new local
# inside of +Thread.current+ with a key which is the name of the extended
# class. Now you can easily access per thread variables by just calling the
# variable name on the registry.
module
PerThreadRegistry
def
instance
Thread
.
current
[
self
.
name
]
||=
new
end
protected
def
method_missing
(
*
args
,
&
block
)
instance
.
send
(
*
args
,
&
block
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录