Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
8efc87c1
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,发现更多精彩内容 >>
提交
8efc87c1
编写于
4月 16, 2013
作者:
S
Sam Ruby
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
switch (and lazily convert) ivar names to 3.x
上级
0513f6ca
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
51 addition
and
52 deletion
+51
-52
activesupport/lib/active_support/cache.rb
activesupport/lib/active_support/cache.rb
+36
-36
activesupport/test/caching_test.rb
activesupport/test/caching_test.rb
+15
-16
未找到文件。
activesupport/lib/active_support/cache.rb
浏览文件 @
8efc87c1
...
...
@@ -522,7 +522,7 @@ def find_cached_entry(key, name, options)
def
handle_expired_entry
(
entry
,
key
,
options
)
if
entry
&&
entry
.
expired?
race_ttl
=
options
[
:race_condition_ttl
].
to_i
if
race_ttl
&&
(
Time
.
now
-
entry
.
expires_at
<=
race_ttl
)
if
race_ttl
&&
(
Time
.
now
.
to_f
-
entry
.
expires_at
<=
race_ttl
)
# When an entry has :race_condition_ttl defined, put the stale entry back into the cache
# for a brief period while the entry is begin recalculated.
entry
.
expires_at
=
Time
.
now
+
race_ttl
...
...
@@ -562,38 +562,38 @@ class Entry # :nodoc:
# +:compress+, +:compress_threshold+, and +:expires_in+.
def
initialize
(
value
,
options
=
{})
if
should_compress?
(
value
,
options
)
@v
=
compress
(
value
)
@c
=
true
@v
alue
=
compress
(
value
)
@c
ompressed
=
true
else
@v
=
value
end
if
expires_in
=
options
[
:expires_in
]
@x
=
(
Time
.
now
+
expires_in
).
to_i
@value
=
value
end
@created_at
=
Time
.
now
.
to_f
@expires_in
=
options
[
:expires_in
]
@expires_in
=
@expires_in
.
to_f
if
@expires_in
end
def
value
convert_version_
3_entry!
if
defined?
(
@value
)
compressed?
?
uncompress
(
@v
)
:
@v
convert_version_
4beta1_entry!
if
defined?
(
@v
)
compressed?
?
uncompress
(
@v
alue
)
:
@value
end
# Check if the entry is expired. The +expires_in+ parameter can override
# the value set when the entry was created.
def
expired?
convert_version_3_entry!
if
defined?
(
@value
)
if
defined?
(
@x
)
@x
&&
@x
<
Time
.
now
.
to_i
else
false
end
convert_version_4beta1_entry!
if
defined?
(
@value
)
@expires_in
&&
@created_at
+
@expires_in
<=
Time
.
now
.
to_f
end
def
expires_at
Time
.
at
(
@x
)
if
defined?
(
@x
)
@expires_in
?
@created_at
+
@expires_in
:
nil
end
def
expires_at
=
(
value
)
@x
=
value
.
to_i
if
value
@expires_in
=
value
.
to_f
-
@created_at
else
@expires_in
=
nil
end
end
# Returns the size of the cached value. This could be less than
...
...
@@ -606,9 +606,9 @@ def size
when
NilClass
0
when
String
@v
.
bytesize
@v
alue
.
bytesize
else
@s
=
Marshal
.
dump
(
@v
).
bytesize
@s
=
Marshal
.
dump
(
@v
alue
).
bytesize
end
end
end
...
...
@@ -616,12 +616,12 @@ def size
# Duplicate the value in a class. This is used by cache implementations that don't natively
# serialize entries to protect against accidental cache modifications.
def
dup_value!
convert_version_
3_entry!
if
defined?
(
@value
)
if
@v
&&
!
compressed?
&&
!
(
@v
.
is_a?
(
Numeric
)
||
@v
==
true
||
@v
==
false
)
if
@v
.
is_a?
(
String
)
@v
=
@v
.
dup
convert_version_
4beta1_entry!
if
defined?
(
@v
)
if
@v
alue
&&
!
compressed?
&&
!
(
@value
.
is_a?
(
Numeric
)
||
@value
==
true
||
@value
==
false
)
if
@v
alue
.
is_a?
(
String
)
@v
alue
=
@value
.
dup
else
@v
=
Marshal
.
load
(
Marshal
.
dump
(
@v
))
@v
alue
=
Marshal
.
load
(
Marshal
.
dump
(
@value
))
end
end
end
...
...
@@ -637,7 +637,7 @@ def should_compress?(value, options)
end
def
compressed?
defined?
(
@c
)
?
@c
:
false
defined?
(
@c
ompressed
)
?
@compressed
:
false
end
def
compress
(
value
)
...
...
@@ -650,19 +650,19 @@ def uncompress(value)
# The internals of this method changed between Rails 3.x and 4.0. This method provides the glue
# to ensure that cache entries created under the old version still work with the new class definition.
def
convert_version_
3
_entry!
if
defined?
(
@v
alue
)
@v
=
@value
remove_instance_variable
(
:@v
alue
)
def
convert_version_
4beta1
_entry!
if
defined?
(
@v
)
@v
alue
=
@v
remove_instance_variable
(
:@v
)
end
if
defined?
(
@c
ompressed
)
@c
=
@compressed
remove_instance_variable
(
:@c
ompressed
)
if
defined?
(
@c
)
@c
ompressed
=
@c
remove_instance_variable
(
:@c
)
end
if
defined?
(
@
expires_in
)
&&
defined?
(
@created_at
)
&&
@expires_in
&&
@created_at
@
x
=
(
@created_at
+
@expires_in
).
to_i
remove_instance_variable
(
:@created_at
)
remove_instance_variable
(
:@
expires_in
)
if
defined?
(
@
x
)
&&
@x
@
created_at
||=
Time
.
now
.
to_f
@expires_in
=
@x
-
@created_at
remove_instance_variable
(
:@
x
)
end
end
end
...
...
activesupport/test/caching_test.rb
浏览文件 @
8efc87c1
...
...
@@ -943,29 +943,28 @@ def test_non_compress_values
assert_equal
value
.
bytesize
,
entry
.
size
end
def
test_restoring_version_
3
_entries
version_
3
_entry
=
ActiveSupport
::
Cache
::
Entry
.
allocate
version_
3_entry
.
instance_variable_set
(
:@value
,
"hello"
)
version_
3_entry
.
instance_variable_set
(
:@created_at
,
Time
.
now
-
60
)
entry
=
Marshal
.
load
(
Marshal
.
dump
(
version_
3
_entry
))
def
test_restoring_version_
4beta1
_entries
version_
4beta1
_entry
=
ActiveSupport
::
Cache
::
Entry
.
allocate
version_
4beta1_entry
.
instance_variable_set
(
:@v
,
"hello"
)
version_
4beta1_entry
.
instance_variable_set
(
:@x
,
Time
.
now
.
to_i
+
60
)
entry
=
Marshal
.
load
(
Marshal
.
dump
(
version_
4beta1
_entry
))
assert_equal
"hello"
,
entry
.
value
assert_equal
false
,
entry
.
expired?
end
def
test_restoring_compressed_version_
3
_entries
version_
3
_entry
=
ActiveSupport
::
Cache
::
Entry
.
allocate
version_
3_entry
.
instance_variable_set
(
:@value
,
Zlib
::
Deflate
.
deflate
(
Marshal
.
dump
(
"hello"
)))
version_
3_entry
.
instance_variable_set
(
:@compressed
,
true
)
entry
=
Marshal
.
load
(
Marshal
.
dump
(
version_
3
_entry
))
def
test_restoring_compressed_version_
4beta1
_entries
version_
4beta1
_entry
=
ActiveSupport
::
Cache
::
Entry
.
allocate
version_
4beta1_entry
.
instance_variable_set
(
:@v
,
Zlib
::
Deflate
.
deflate
(
Marshal
.
dump
(
"hello"
)))
version_
4beta1_entry
.
instance_variable_set
(
:@c
,
true
)
entry
=
Marshal
.
load
(
Marshal
.
dump
(
version_
4beta1
_entry
))
assert_equal
"hello"
,
entry
.
value
end
def
test_restoring_expired_version_3_entries
version_3_entry
=
ActiveSupport
::
Cache
::
Entry
.
allocate
version_3_entry
.
instance_variable_set
(
:@value
,
"hello"
)
version_3_entry
.
instance_variable_set
(
:@created_at
,
Time
.
now
-
60
)
version_3_entry
.
instance_variable_set
(
:@expires_in
,
58.9
)
entry
=
Marshal
.
load
(
Marshal
.
dump
(
version_3_entry
))
def
test_restoring_expired_version_4beta1_entries
version_4beta1_entry
=
ActiveSupport
::
Cache
::
Entry
.
allocate
version_4beta1_entry
.
instance_variable_set
(
:@v
,
"hello"
)
version_4beta1_entry
.
instance_variable_set
(
:@x
,
Time
.
now
.
to_i
-
1
)
entry
=
Marshal
.
load
(
Marshal
.
dump
(
version_4beta1_entry
))
assert_equal
"hello"
,
entry
.
value
assert_equal
true
,
entry
.
expired?
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录