Class: Rosette::Core::CachedHeadSnapshotFactory

Inherits:
HeadSnapshotFactory show all
Defined in:
lib/rosette/core/snapshots/cached_head_snapshot_factory.rb

Overview

Takes head snapshots and caches the results. Take a look at ActiveSupport::Cache for a good set of cache stores that conform to the right interface.

Instance Attribute Summary collapse

Attributes inherited from HeadSnapshotFactory

#repo_config

Instance Method Summary collapse

Methods inherited from HeadSnapshotFactory

#set_repo_config, #take_snapshot

Constructor Details

#initialize(cache) ⇒ CachedHeadSnapshotFactory

Creates a new cached head snapshot factory that uses the given cache.

Parameters:

  • cache (#fetch)

    The cache to use.


23
24
25
# File 'lib/rosette/core/snapshots/cached_head_snapshot_factory.rb', line 23

def initialize(cache)
  @cache = cache
end

Instance Attribute Details

#cache#fetch (readonly)

Returns the cache store. This can be any object that responds to #fetch (and passes a block).

Returns:

  • (#fetch)

    the cache store. This can be any object that responds to #fetch (and passes a block).


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rosette/core/snapshots/cached_head_snapshot_factory.rb', line 17

class CachedHeadSnapshotFactory < HeadSnapshotFactory
  attr_reader :cache

  # Creates a new cached head snapshot factory that uses the given cache.
  #
  # @param [#fetch] cache The cache to use.
  def initialize(cache)
    @cache = cache
  end

  protected

  def process_ref(rev_walk, ref)
    cache_key = head_snapshot_cache_key(
      repo_config.name,
      repo_config.repo.get_rev_commit(ref, rev_walk).getId.name
    )

    cache.fetch(cache_key) do
      super
    end
  end

  def head_snapshot_cache_key(repo_name, commit_id)
    ['head_snapshots', repo_config.name, commit_id].join('/')
  end

  def head_snapshot_factory
    Rosette::Core::HeadSnapshotFactory
  end

end