Class: Rosette::Core::CachedSnapshotFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/rosette/core/snapshots/cached_snapshot_factory.rb

Overview

Takes 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

Instance Method Summary collapse

Constructor Details

#initialize(cache) ⇒ CachedSnapshotFactory

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

Parameters:

  • cache (#fetch)

    The cache to use.


23
24
25
# File 'lib/rosette/core/snapshots/cached_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rosette/core/snapshots/cached_snapshot_factory.rb', line 17

class CachedSnapshotFactory
  attr_reader :cache

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

  # Takes the snapshot.
  #
  # @param [RepoConfig] repo_config The repo config for the repo to take
  #   the snapshot for.
  # @param [String] commit_id The commit id to take the snapshot of.
  # @param [Array<String>] paths The list of paths to include in the
  #   snapshot. If +paths+ is empty, this method will return a snapshot
  #   that contains all paths.
  # @return [Hash<String, String>] The snapshot hash (path to commit id
  #   pairs).
  def take_snapshot(repo_config, commit_id, paths = [])
    paths = Array(paths)
    cache_key = snapshot_cache_key(repo_config.name, commit_id, paths)

    cache.fetch(cache_key) do
      factory = snapshot_factory.new
        .set_repo_config(repo_config)
        .set_start_commit_id(commit_id)

      factory.set_paths(paths) if paths.size > 0
      factory.take_snapshot
    end
  end

  private

  def snapshot_cache_key(repo_name, commit_id, paths)
    ['snapshots', repo_name, commit_id, path_digest(paths)].join('/')
  end

  def path_digest(paths)
    Digest::MD5.hexdigest(paths.join)
  end

  def snapshot_factory
    Rosette::Core::SnapshotFactory
  end
end

Instance Method Details

#take_snapshot(repo_config, commit_id, paths = []) ⇒ Hash<String, String>

Takes the snapshot.

Parameters:

  • repo_config (RepoConfig)

    The repo config for the repo to take the snapshot for.

  • commit_id (String)

    The commit id to take the snapshot of.

  • paths (Array<String>) (defaults to: [])

    The list of paths to include in the snapshot. If paths is empty, this method will return a snapshot that contains all paths.

Returns:

  • (Hash<String, String>)

    The snapshot hash (path to commit id pairs).


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rosette/core/snapshots/cached_snapshot_factory.rb', line 37

def take_snapshot(repo_config, commit_id, paths = [])
  paths = Array(paths)
  cache_key = snapshot_cache_key(repo_config.name, commit_id, paths)

  cache.fetch(cache_key) do
    factory = snapshot_factory.new
      .set_repo_config(repo_config)
      .set_start_commit_id(commit_id)

    factory.set_paths(paths) if paths.size > 0
    factory.take_snapshot
  end
end