BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
853fa726
Commit
853fa726
authored
Aug 22, 2017
by
Sean McGivern
Committed by
Jarka Kadlecova
Aug 22, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge branch 'fix-broadcast-message-caching' into 'master'
Fix caching of future broadcast messages Closes #36661 See merge request !13667
parent
cc370bc8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
5 deletions
+55
-5
broadcast_message.rb
app/models/broadcast_message.rb
+27
-5
fix-broadcast-message-caching.yml
changelogs/unreleased/fix-broadcast-message-caching.yml
+5
-0
broadcast_message_spec.rb
spec/models/broadcast_message_spec.rb
+23
-0
No files found.
app/models/broadcast_message.rb
View file @
853fa726
...
...
@@ -19,11 +19,21 @@ class BroadcastMessage < ActiveRecord::Base
after_commit
:flush_redis_cache
def
self
.
current
Rails
.
cache
.
fetch
(
CACHE_KEY
)
do
where
(
'ends_at > :now AND starts_at <= :now'
,
now:
Time
.
zone
.
now
)
.
reorder
(
id: :asc
)
.
to_a
end
messages
=
Rails
.
cache
.
fetch
(
CACHE_KEY
)
{
current_and_future_messages
.
to_a
}
return
messages
if
messages
.
empty?
now_or_future
=
messages
.
select
(
&
:now_or_future?
)
# If there are cached entries but none are to be displayed we'll purge the
# cache so we don't keep running this code all the time.
Rails
.
cache
.
delete
(
CACHE_KEY
)
if
now_or_future
.
empty?
now_or_future
.
select
(
&
:now?
)
end
def
self
.
current_and_future_messages
where
(
'ends_at > :now'
,
now:
Time
.
zone
.
now
).
reorder
(
id: :asc
)
end
def
active?
...
...
@@ -38,6 +48,18 @@ class BroadcastMessage < ActiveRecord::Base
ends_at
<
Time
.
zone
.
now
end
def
now?
(
starts_at
..
ends_at
).
cover?
(
Time
.
zone
.
now
)
end
def
future?
starts_at
>
Time
.
zone
.
now
end
def
now_or_future?
now?
||
future?
end
def
flush_redis_cache
Rails
.
cache
.
delete
(
CACHE_KEY
)
end
...
...
changelogs/unreleased/fix-broadcast-message-caching.yml
0 → 100644
View file @
853fa726
---
title
:
Fix caching of future broadcast messages
merge_request
:
author
:
type
:
fixed
spec/models/broadcast_message_spec.rb
View file @
853fa726
...
...
@@ -53,6 +53,29 @@ describe BroadcastMessage do
2
.
times
{
described_class
.
current
}
end
it
'includes messages that need to be displayed in the future'
do
create
(
:broadcast_message
)
future
=
create
(
:broadcast_message
,
starts_at:
Time
.
now
+
10
.
minutes
,
ends_at:
Time
.
now
+
20
.
minutes
)
expect
(
described_class
.
current
.
length
).
to
eq
(
1
)
Timecop
.
travel
(
future
.
starts_at
)
do
expect
(
described_class
.
current
.
length
).
to
eq
(
2
)
end
end
it
'does not clear the cache if only a future message should be displayed'
do
create
(
:broadcast_message
,
:future
)
expect
(
Rails
.
cache
).
not_to
receive
(
:delete
)
expect
(
described_class
.
current
.
length
).
to
eq
(
0
)
end
end
describe
'#active?'
do
...
...
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