Class: Gitlab::GithubImport::EventsCache

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/github_import/events_cache.rb

Constant Summary collapse

MAX_NUMBER_OF_EVENTS =
100
MAX_EVENT_SIZE =
100.kilobytes

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ EventsCache

Returns a new instance of EventsCache.



9
10
11
# File 'lib/gitlab/github_import/events_cache.rb', line 9

def initialize(project)
  @project = project
end

Instance Method Details

#add(record, issue_event) ⇒ Object

Add issue event as JSON to the cache

Parameters:

  • record (ActiveRecord::Model)

    Model that responds to :iid

  • event (GitLab::GitHubImport::Representation::IssueEvent)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/github_import/events_cache.rb', line 17

def add(record, issue_event)
  json = issue_event.to_hash.to_json

  if json.bytesize > MAX_EVENT_SIZE
    Logger.warn(
      message: 'Event too large to cache',
      project_id: project.id,
      github_identifiers: issue_event.github_identifiers
    )

    return
  end

  Gitlab::Cache::Import::Caching.list_add(events_cache_key(record), json, limit: MAX_NUMBER_OF_EVENTS)
end

#delete(record) ⇒ Object

Deletes the cache

Parameters:

  • record (ActiveRecord::Model)

    Model that responds to :iid



48
49
50
# File 'lib/gitlab/github_import/events_cache.rb', line 48

def delete(record)
  Gitlab::Cache::Import::Caching.del(events_cache_key(record))
end

#events(record) ⇒ Object

Reads issue events from cache

Parameters:

  • record (ActiveRecord::Model)

    Model that responds to :iid



37
38
39
40
41
42
43
# File 'lib/gitlab/github_import/events_cache.rb', line 37

def events(record)
  events = Gitlab::Cache::Import::Caching.values_from_list(events_cache_key(record)).map do |event|
    Representation::IssueEvent.from_json_hash(Gitlab::Json.parse(event))
  end

  events.sort_by(&:created_at)
end