Class: Users::LastPushEventService

Inherits:
Object
  • Object
show all
Defined in:
app/services/users/last_push_event_service.rb

Overview

Service class for caching and retrieving the last push event of a user.

Constant Summary collapse

EXPIRATION =
2.hours

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ LastPushEventService

Returns a new instance of LastPushEventService.



8
9
10
# File 'app/services/users/last_push_event_service.rb', line 8

def initialize(user)
  @user = user
end

Instance Method Details

#cache_last_push_event(event) ⇒ Object

Caches the given push event for the current user in the Rails cache.

event - An instance of PushEvent to cache.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/users/last_push_event_service.rb', line 15

def cache_last_push_event(event)
  keys = [
    project_cache_key(event.project),
    user_cache_key
  ]

  if forked_from = event.project.forked_from_project
    keys << project_cache_key(forked_from)
  end

  keys.each { |key| set_key(key, event.id) }
end

#find_cached_event(cache_key) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/services/users/last_push_event_service.rb', line 44

def find_cached_event(cache_key)
  event_id = get_key(cache_key)

  return unless event_id

  unless (event = find_event_in_database(event_id))
    # We don't want to keep querying the same data over and over when a
    # merge request has been created, thus we remove the key if no event
    # (meaning an MR was created) is returned.
    Rails.cache.delete(cache_key)
  end

  event
end

#last_event_for_project(project) ⇒ Object

Returns the last PushEvent for the current user and the given project.

project - An instance of Project for which to retrieve the PushEvent.

This method will return nil if no event was found.



40
41
42
# File 'app/services/users/last_push_event_service.rb', line 40

def last_event_for_project(project)
  find_cached_event(project_cache_key(project))
end

#last_event_for_userObject

Returns the last PushEvent for the current user.

This method will return nil if no event was found.



31
32
33
# File 'app/services/users/last_push_event_service.rb', line 31

def last_event_for_user
  find_cached_event(user_cache_key)
end