Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
02f17a09
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
02f17a09
编写于
5月 04, 2018
作者:
R
Rémy Coutable
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce a new CacheableAttributes concern
Signed-off-by:
N
Rémy Coutable
<
remy@rymai.me
>
上级
a2dbca4a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
211 addition
and
0 deletion
+211
-0
app/models/concerns/cacheable_attributes.rb
app/models/concerns/cacheable_attributes.rb
+54
-0
lib/gitlab.rb
lib/gitlab.rb
+4
-0
spec/models/concerns/cacheable_attributes_spec.rb
spec/models/concerns/cacheable_attributes_spec.rb
+153
-0
未找到文件。
app/models/concerns/cacheable_attributes.rb
0 → 100644
浏览文件 @
02f17a09
module
CacheableAttributes
extend
ActiveSupport
::
Concern
included
do
after_commit
{
self
.
class
.
expire
}
end
class_methods
do
# Can be overriden
def
current_without_cache
last
end
def
cache_key
"
#{
name
}
:
#{
Gitlab
::
VERSION
}
:
#{
Gitlab
.
migrations_hash
}
:json"
.
freeze
end
def
defaults
{}
end
def
build_from_defaults
(
attributes
=
{})
new
(
defaults
.
merge
(
attributes
))
end
def
cached
json_attributes
=
Rails
.
cache
.
read
(
cache_key
)
return
nil
unless
json_attributes
.
present?
build_from_defaults
(
JSON
.
parse
(
json_attributes
))
end
def
current
cached_record
=
cached
return
cached_record
if
cached_record
.
present?
current_without_cache
.
tap
{
|
current_record
|
current_record
&
.
cache!
}
rescue
# Fall back to an uncached value if there are any problems (e.g. Redis down)
current_without_cache
end
def
expire
Rails
.
cache
.
delete
(
cache_key
)
rescue
# Gracefully handle when Redis is not available. For example,
# omnibus may fail here during gitlab:assets:compile.
end
end
def
cache!
Rails
.
cache
.
write
(
self
.
class
.
cache_key
,
attributes
.
to_json
)
end
end
lib/gitlab.rb
浏览文件 @
02f17a09
...
...
@@ -9,6 +9,10 @@ module Gitlab
Settings
end
def
self
.
migrations_hash
@_migrations_hash
||=
Digest
::
MD5
.
hexdigest
(
ActiveRecord
::
Migrator
.
get_all_versions
.
to_s
)
end
COM_URL
=
'https://gitlab.com'
.
freeze
APP_DIRS_PATTERN
=
%r{^/?(app|config|ee|lib|spec|
\(\w
*
\)
)}
SUBDOMAIN_REGEX
=
%r{
\A
https://[a-z0-9]+
\.
gitlab
\.
com
\z
}
...
...
spec/models/concerns/cacheable_attributes_spec.rb
0 → 100644
浏览文件 @
02f17a09
require
'spec_helper'
describe
CacheableAttributes
do
let
(
:minimal_test_class
)
do
Class
.
new
do
include
ActiveModel
::
Model
extend
ActiveModel
::
Callbacks
define_model_callbacks
:commit
include
CacheableAttributes
def
self
.
name
'TestClass'
end
def
self
.
first
@_first
||=
new
(
'foo'
=>
'a'
)
end
def
self
.
last
@_last
||=
new
(
'foo'
=>
'a'
,
'bar'
=>
'b'
)
end
attr_accessor
:attributes
def
initialize
(
attrs
=
{})
@attributes
=
attrs
end
end
end
shared_context
'with defaults'
do
before
do
minimal_test_class
.
define_singleton_method
(
:defaults
)
do
{
foo:
'a'
,
bar:
'b'
,
baz:
'c'
}
end
end
end
describe
'.current_without_cache'
do
it
'defaults to last'
do
expect
(
minimal_test_class
.
current_without_cache
).
to
eq
(
minimal_test_class
.
last
)
end
it
'can be overriden'
do
minimal_test_class
.
define_singleton_method
(
:current_without_cache
)
do
first
end
expect
(
minimal_test_class
.
current_without_cache
).
to
eq
(
minimal_test_class
.
first
)
end
end
describe
'.cache_key'
do
it
'excludes cache attributes'
do
expect
(
minimal_test_class
.
cache_key
).
to
eq
(
"TestClass:
#{
Gitlab
::
VERSION
}
:
#{
Gitlab
.
migrations_hash
}
:json"
)
end
end
describe
'.defaults'
do
it
'defaults to {}'
do
expect
(
minimal_test_class
.
defaults
).
to
eq
({})
end
context
'with defaults defined'
do
include_context
'with defaults'
it
'can be overriden'
do
expect
(
minimal_test_class
.
defaults
).
to
eq
({
foo:
'a'
,
bar:
'b'
,
baz:
'c'
})
end
end
end
describe
'.build_from_defaults'
do
include_context
'with defaults'
context
'without any attributes given'
do
it
'intializes a new object with the defaults'
do
expect
(
minimal_test_class
.
build_from_defaults
).
not_to
be_persisted
end
end
context
'without attributes given'
do
it
'intializes a new object with the given attributes merged into the defaults'
do
expect
(
minimal_test_class
.
build_from_defaults
(
foo:
'd'
).
attributes
[
:foo
]).
to
eq
(
'd'
)
end
end
end
describe
'.current'
,
:use_clean_rails_memory_store_caching
do
context
'redis unavailable'
do
it
'returns an uncached record'
do
allow
(
minimal_test_class
).
to
receive
(
:last
).
and_return
(
:last
)
expect
(
Rails
.
cache
).
to
receive
(
:read
).
and_raise
(
Redis
::
BaseError
)
expect
(
minimal_test_class
.
current
).
to
eq
(
:last
)
end
end
context
'when a record is not yet present'
do
it
'does not cache nil object'
do
# when missing settings a nil object is returned, but not cached
allow
(
minimal_test_class
).
to
receive
(
:last
).
twice
.
and_return
(
nil
)
expect
(
minimal_test_class
.
current
).
to
be_nil
expect
(
Rails
.
cache
.
exist?
(
minimal_test_class
.
cache_key
)).
to
be
(
false
)
end
it
'cache non-nil object'
do
# when the settings are set the method returns a valid object
allow
(
minimal_test_class
).
to
receive
(
:last
).
and_call_original
expect
(
minimal_test_class
.
current
).
to
eq
(
minimal_test_class
.
last
)
expect
(
Rails
.
cache
.
exist?
(
minimal_test_class
.
cache_key
)).
to
be
(
true
)
# subsequent calls retrieve the record from the cache
last_record
=
minimal_test_class
.
last
expect
(
minimal_test_class
).
not_to
receive
(
:last
)
expect
(
minimal_test_class
.
current
.
attributes
).
to
eq
(
last_record
.
attributes
)
end
end
end
describe
'.cached'
,
:use_clean_rails_memory_store_caching
do
context
'when cache is cold'
do
it
'returns nil'
do
expect
(
minimal_test_class
.
cached
).
to
be_nil
end
end
context
'when cached settings do not include the latest defaults'
do
before
do
Rails
.
cache
.
write
(
minimal_test_class
.
cache_key
,
{
bar:
'b'
,
baz:
'c'
}.
to_json
)
minimal_test_class
.
define_singleton_method
(
:defaults
)
do
{
foo:
'a'
,
bar:
'b'
,
baz:
'c'
}
end
end
it
'includes attributes from defaults'
do
expect
(
minimal_test_class
.
cached
.
attributes
[
:foo
]).
to
eq
(
minimal_test_class
.
defaults
[
:foo
])
end
end
end
describe
'#cache!'
,
:use_clean_rails_memory_store_caching
do
let
(
:appearance_record
)
{
create
(
:appearance
)
}
it
'caches the attributes'
do
appearance_record
.
cache!
expect
(
Rails
.
cache
.
read
(
Appearance
.
cache_key
)).
to
eq
(
appearance_record
.
attributes
.
to_json
)
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录