Commit adf559581cf5ac61445088158072820522337e9f

Authored by Dmitriy Zaporozhets
2 parents 2934e7a9 c26d392b

Merge pull request #5759 from bke-drewb/issue-5496

Add cache expiry on avatar creation/removal
@@ -9,6 +9,7 @@ v 6.4.0 @@ -9,6 +9,7 @@ v 6.4.0
9 - Allow removal of avatar (Drew Blessing) 9 - Allow removal of avatar (Drew Blessing)
10 - Project web hooks now support issues and merge request events 10 - Project web hooks now support issues and merge request events
11 - Visiting project page while not logged in will redirect to sign-in instead of 404 (Jason Hollingsworth) 11 - Visiting project page while not logged in will redirect to sign-in instead of 404 (Jason Hollingsworth)
  12 + - Expire event cache on avatar creation/removal (Drew Blessing)
12 13
13 v 6.3.0 14 v 6.3.0
14 - API for adding gitlab-ci service 15 - API for adding gitlab-ci service
app/controllers/profiles/avatars_controller.rb
@@ -6,6 +6,8 @@ class Profiles::AvatarsController < ApplicationController @@ -6,6 +6,8 @@ class Profiles::AvatarsController < ApplicationController
6 @user.remove_avatar! 6 @user.remove_avatar!
7 7
8 @user.save 8 @user.save
  9 + @user.reset_events_cache
  10 +
9 redirect_to profile_path 11 redirect_to profile_path
10 end 12 end
11 end 13 end
app/models/user.rb
@@ -404,4 +404,18 @@ class User < ActiveRecord::Base @@ -404,4 +404,18 @@ class User < ActiveRecord::Base
404 project.namespace != namespace && 404 project.namespace != namespace &&
405 project.project_member(self) 405 project.project_member(self)
406 end 406 end
  407 +
  408 + # Reset project events cache related to this user
  409 + #
  410 + # Since we do cache @event we need to reset cache in special cases:
  411 + # * when the user changes their avatar
  412 + # Events cache stored like events/23-20130109142513.
  413 + # The cache key includes updated_at timestamp.
  414 + # Thus it will automatically generate a new fragment
  415 + # when the event is updated because the key changes.
  416 + def reset_events_cache
  417 + Event.where(author_id: self.id).
  418 + order('id DESC').limit(1000).
  419 + update_all(updated_at: Time.now)
  420 + end
407 end 421 end
app/uploaders/attachment_uploader.rb
@@ -3,6 +3,8 @@ @@ -3,6 +3,8 @@
3 class AttachmentUploader < CarrierWave::Uploader::Base 3 class AttachmentUploader < CarrierWave::Uploader::Base
4 storage :file 4 storage :file
5 5
  6 + after :store, :reset_events_cache
  7 +
6 def store_dir 8 def store_dir
7 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 9 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
8 end 10 end
@@ -27,4 +29,8 @@ class AttachmentUploader &lt; CarrierWave::Uploader::Base @@ -27,4 +29,8 @@ class AttachmentUploader &lt; CarrierWave::Uploader::Base
27 def file_storage? 29 def file_storage?
28 self.class.storage == CarrierWave::Storage::File 30 self.class.storage == CarrierWave::Storage::File
29 end 31 end
  32 +
  33 + def reset_events_cache(file)
  34 + model.reset_events_cache if model.is_a?(User)
  35 + end
30 end 36 end