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
d4d0740e
Commit
d4d0740e
authored
Feb 23, 2018
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cache project/user combinations.
parent
ebcf3c73
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
6 deletions
+23
-6
app/models/user_contributed_projects.rb
app/models/user_contributed_projects.rb
+19
-3
spec/models/user_contributed_projects_spec.rb
spec/models/user_contributed_projects_spec.rb
+4
-3
No files found.
app/models/user_contributed_projects.rb
View file @
d4d0740e
...
...
@@ -5,9 +5,25 @@ class UserContributedProjects < ActiveRecord::Base
validates
:project
,
presence:
true
validates
:user
,
presence:
true
CACHE_EXPIRY_TIME
=
1
.
day
def
self
.
track
(
event
)
find_or_create_by!
(
project:
event
.
project
,
user:
event
.
author
)
rescue
ActiveRecord
::
RecordNotUnique
retry
attributes
=
{
project_id:
event
.
project_id
,
user_id:
event
.
author_id
}
cached_exists?
(
attributes
)
do
begin
find_or_create_by!
(
attributes
)
true
# not caching the whole record here for now
rescue
ActiveRecord
::
RecordNotUnique
retry
end
end
end
private
def
self
.
cached_exists?
(
project_id
:,
user_id
:,
&
block
)
cache_key
=
"user_contributed_projects:
#{
project_id
}
:
#{
user_id
}
"
Rails
.
cache
.
fetch
(
cache_key
,
expires_in:
CACHE_EXPIRY_TIME
,
&
block
)
end
end
spec/models/user_contributed_projects_spec.rb
View file @
d4d0740e
...
...
@@ -12,16 +12,17 @@ describe UserContributedProjects do
it
'creates a record'
do
expect
{
subject
}.
to
change
{
UserContributedProjects
.
count
}.
from
(
0
).
to
(
1
)
end
end
end
it
'sets project accordingly'
do
expect
(
subject
.
project
).
to
eq
(
event
.
project
)
subject
expect
(
UserContributedProjects
.
first
.
project
).
to
eq
(
event
.
project
)
end
it
'sets user accordingly'
do
expect
(
subject
.
user
).
to
eq
(
event
.
author
)
subject
expect
(
UserContributedProjects
.
first
.
user
).
to
eq
(
event
.
author
)
end
it
'only creates a record once per user/project'
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