Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
e531a0c1
Commit
e531a0c1
authored
May 04, 2018
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use the new CacheableAttributes concern in the ApplicationSetting and Appearance models
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
02f17a09
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
11 additions
and
98 deletions
+11
-98
app/models/appearance.rb
app/models/appearance.rb
+4
-11
app/models/application_setting.rb
app/models/application_setting.rb
+1
-35
app/uploaders/object_storage.rb
app/uploaders/object_storage.rb
+1
-0
spec/models/appearance_spec.rb
spec/models/appearance_spec.rb
+2
-25
spec/models/application_setting_spec.rb
spec/models/application_setting_spec.rb
+3
-27
No files found.
app/models/appearance.rb
View file @
e531a0c1
class
Appearance
<
ActiveRecord
::
Base
include
CacheableAttributes
include
CacheMarkdownField
include
AfterCommitQueue
include
ObjectStorage
::
BackgroundMove
include
WithUploads
...
...
@@ -15,16 +15,9 @@ class Appearance < ActiveRecord::Base
mount_uploader
:logo
,
AttachmentUploader
mount_uploader
:header_logo
,
AttachmentUploader
CACHE_KEY
=
"current_appearance:
#{
Gitlab
::
VERSION
}
"
.
freeze
after_commit
:flush_redis_cache
def
self
.
current
Rails
.
cache
.
fetch
(
CACHE_KEY
)
{
first
}
end
def
flush_redis_cache
Rails
.
cache
.
delete
(
CACHE_KEY
)
# Overrides CacheableAttributes.current_without_cache
def
self
.
current_without_cache
first
end
def
single_appearance_row
...
...
app/models/application_setting.rb
View file @
e531a0c1
class
ApplicationSetting
<
ActiveRecord
::
Base
include
CacheableAttributes
include
CacheMarkdownField
include
TokenAuthenticatable
add_authentication_token_field
:runners_registration_token
add_authentication_token_field
:health_check_access_token
CACHE_KEY
=
'application_setting.last'
.
freeze
DOMAIN_LIST_SEPARATOR
=
%r{
\s
*[,;]
\s
* # comma or semicolon, optionally surrounded by whitespace
| # or
\s
# any whitespace character
...
...
@@ -229,40 +229,6 @@ class ApplicationSetting < ActiveRecord::Base
after_commit
do
reset_memoized_terms
Rails
.
cache
.
write
(
CACHE_KEY
,
self
)
end
def
self
.
current
ensure_cache_setup
Rails
.
cache
.
fetch
(
CACHE_KEY
)
do
ApplicationSetting
.
last
.
tap
do
|
settings
|
# do not cache nils
raise
'missing settings'
unless
settings
end
end
rescue
# Fall back to an uncached value if there are any problems (e.g. redis down)
ApplicationSetting
.
last
end
def
self
.
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
def
self
.
cached
value
=
Rails
.
cache
.
read
(
CACHE_KEY
)
ensure_cache_setup
if
value
.
present?
value
end
def
self
.
ensure_cache_setup
# This is a workaround for a Rails bug that causes attribute methods not
# to be loaded when read from cache: https://github.com/rails/rails/issues/27348
ApplicationSetting
.
define_attribute_methods
end
def
self
.
defaults
...
...
app/uploaders/object_storage.rb
View file @
e531a0c1
...
...
@@ -103,6 +103,7 @@ module ObjectStorage
end
included
do
include
AfterCommitQueue
after_save
on:
[
:create
,
:update
]
do
background_upload
(
changed_mounts
)
end
...
...
spec/models/appearance_spec.rb
View file @
e531a0c1
...
...
@@ -3,34 +3,11 @@ require 'rails_helper'
describe
Appearance
do
subject
{
build
(
:appearance
)
}
it
{
is_expected
.
to
be_valid
}
it
{
include
(
CacheableAttributes
)
}
it
{
expect
(
described_class
.
current_without_cache
).
to
eq
(
described_class
.
first
)
}
it
{
is_expected
.
to
have_many
(
:uploads
)
}
describe
'.current'
,
:use_clean_rails_memory_store_caching
do
let!
(
:appearance
)
{
create
(
:appearance
)
}
it
'returns the current appearance row'
do
expect
(
described_class
.
current
).
to
eq
(
appearance
)
end
it
'caches the result'
do
expect
(
described_class
).
to
receive
(
:first
).
once
2
.
times
{
described_class
.
current
}
end
end
describe
'#flush_redis_cache'
do
it
'flushes the cache in Redis'
do
appearance
=
create
(
:appearance
)
expect
(
Rails
.
cache
).
to
receive
(
:delete
).
with
(
described_class
::
CACHE_KEY
)
appearance
.
flush_redis_cache
end
end
describe
'#single_appearance_row'
do
it
'adds an error when more than 1 row exists'
do
create
(
:appearance
)
...
...
spec/models/application_setting_spec.rb
View file @
e531a0c1
...
...
@@ -3,6 +3,9 @@ require 'spec_helper'
describe
ApplicationSetting
do
let
(
:setting
)
{
described_class
.
create_from_defaults
}
it
{
include
(
CacheableAttributes
)
}
it
{
expect
(
described_class
.
current_without_cache
).
to
eq
(
described_class
.
last
)
}
it
{
expect
(
setting
).
to
be_valid
}
it
{
expect
(
setting
.
uuid
).
to
be_present
}
it
{
expect
(
setting
).
to
have_db_column
(
:auto_devops_enabled
)
}
...
...
@@ -318,33 +321,6 @@ describe ApplicationSetting do
end
end
describe
'.current'
do
context
'redis unavailable'
do
it
'returns an ApplicationSetting'
do
allow
(
Rails
.
cache
).
to
receive
(
:fetch
).
and_call_original
allow
(
described_class
).
to
receive
(
:last
).
and_return
(
:last
)
expect
(
Rails
.
cache
).
to
receive
(
:fetch
).
with
(
ApplicationSetting
::
CACHE_KEY
).
and_raise
(
ArgumentError
)
expect
(
described_class
.
current
).
to
eq
(
:last
)
end
end
context
'when an ApplicationSetting is not yet present'
do
it
'does not cache nil object'
do
# when missing settings a nil object is returned, but not cached
allow
(
described_class
).
to
receive
(
:last
).
and_return
(
nil
).
twice
expect
(
described_class
.
current
).
to
be_nil
# when the settings are set the method returns a valid object
allow
(
described_class
).
to
receive
(
:last
).
and_return
(
:last
)
expect
(
described_class
.
current
).
to
eq
(
:last
)
# subsequent calls get everything from cache
expect
(
described_class
.
current
).
to
eq
(
:last
)
end
end
end
context
'restrict creating duplicates'
do
before
do
described_class
.
create_from_defaults
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment