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
Boxiang Sun
gitlab-ce
Commits
6cc3a07d
Commit
6cc3a07d
authored
May 15, 2018
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dynamically cast value from cache
parent
2c29e80a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
49 deletions
+15
-49
app/models/ci/runner.rb
app/models/ci/runner.rb
+1
-2
app/models/concerns/redis_cacheable.rb
app/models/concerns/redis_cacheable.rb
+10
-9
spec/models/concerns/redis_cacheable_spec.rb
spec/models/concerns/redis_cacheable_spec.rb
+4
-38
No files found.
app/models/ci/runner.rb
View file @
6cc3a07d
...
@@ -75,8 +75,7 @@ module Ci
...
@@ -75,8 +75,7 @@ module Ci
project_type:
3
project_type:
3
}
}
cached_attr_reader
:version
,
:revision
,
:platform
,
:architecture
,
:ip_address
cached_attr_reader
:version
,
:revision
,
:platform
,
:architecture
,
:ip_address
,
:contacted_at
cached_attr_time_reader
:contacted_at
chronic_duration_attr
:maximum_timeout_human_readable
,
:maximum_timeout
chronic_duration_attr
:maximum_timeout_human_readable
,
:maximum_timeout
...
...
app/models/concerns/redis_cacheable.rb
View file @
6cc3a07d
...
@@ -6,18 +6,11 @@ module RedisCacheable
...
@@ -6,18 +6,11 @@ module RedisCacheable
class_methods
do
class_methods
do
def
cached_attr_reader
(
*
attributes
)
def
cached_attr_reader
(
*
attributes
)
attributes
.
each
do
|
attribute
|
define_method
(
attribute
)
do
cached_attribute
(
attribute
)
||
read_attribute
(
attribute
)
end
end
end
def
cached_attr_time_reader
(
*
attributes
)
attributes
.
each
do
|
attribute
|
attributes
.
each
do
|
attribute
|
define_method
(
attribute
)
do
define_method
(
attribute
)
do
cached_value
=
cached_attribute
(
attribute
)
cached_value
=
cached_attribute
(
attribute
)
cached_value
?
Time
.
zone
.
parse
(
cached_value
)
:
read_attribute
(
attribute
)
cached_value
=
cast_value_from_cache
(
attribute
,
cached_value
)
if
cached_value
cached_value
||
read_attribute
(
attribute
)
end
end
end
end
end
end
...
@@ -49,4 +42,12 @@ module RedisCacheable
...
@@ -49,4 +42,12 @@ module RedisCacheable
end
end
end
end
end
end
def
cast_value_from_cache
(
attribute
,
value
)
if
self
.
class
.
column_for_attribute
(
attribute
).
respond_to?
(
:type_cast_from_database
)
self
.
class
.
column_for_attribute
(
attribute
).
type_cast_from_database
(
value
)
else
self
.
class
.
type_for_attribute
(
attribute
).
cast
(
value
)
end
end
end
end
spec/models/concerns/redis_cacheable_spec.rb
View file @
6cc3a07d
...
@@ -6,6 +6,10 @@ describe RedisCacheable do
...
@@ -6,6 +6,10 @@ describe RedisCacheable do
def
read_attribute
(
attribute
)
def
read_attribute
(
attribute
)
attributes
[
attribute
]
attributes
[
attribute
]
end
end
def
cast_value_from_cache
(
attribute
,
cached_value
)
cached_value
end
end
end
end
end
...
@@ -75,42 +79,4 @@ describe RedisCacheable do
...
@@ -75,42 +79,4 @@ describe RedisCacheable do
expect
(
instance
.
name
).
to
eq
(
'new_value'
)
expect
(
instance
.
name
).
to
eq
(
'new_value'
)
end
end
end
end
describe
'#cached_attr_time_reader'
,
:clean_gitlab_redis_shared_state
do
subject
{
instance
.
time
}
let
(
:other_time
)
{
Time
.
zone
.
parse
(
'May 14 2018'
)
}
before
do
model
.
cached_attr_time_reader
(
:time
)
end
context
'when there is no cached value'
do
it
'reads the attribute'
do
expect
(
instance
).
to
receive
(
:read_attribute
).
and_call_original
expect
(
subject
).
to
be_instance_of
(
ActiveSupport
::
TimeWithZone
)
expect
(
subject
).
to
eq
(
payload
[
:time
])
end
end
context
'when there is a cached value'
do
it
'reads the cached value'
do
expect
(
instance
).
not_to
receive
(
:read_attribute
)
instance
.
cache_attributes
(
time:
other_time
)
expect
(
subject
).
to
be_instance_of
(
ActiveSupport
::
TimeWithZone
)
expect
(
subject
).
to
eq
(
other_time
)
end
end
it
'always returns the latest values'
do
expect
(
instance
.
time
).
to
eq
(
payload
[
:time
])
instance
.
cache_attributes
(
time:
other_time
)
expect
(
instance
.
time
).
to
eq
(
other_time
)
end
end
end
end
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