Class: CacheStoreDatabase Private

Inherits:
Object show all
Extended by:
T::Generic
Defined in:
cache_store.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

CacheStoreDatabase acts as an interface to a persistent storage mechanism residing in the HOMEBREW_CACHE.

Constant Summary collapse

Key =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

Sorbet type members are mutable by design and cannot be frozen.

type_member
Value =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

Sorbet type members are mutable by design and cannot be frozen.

type_member

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Creates a CacheStoreDatabase.

Parameters:



55
56
57
58
# File 'cache_store.rb', line 55

def initialize(type)
  @type = type
  @dirty = T.let(false, T.nilable(T::Boolean))
end

Class Method Details

.use(type, &_blk) ⇒ T.type_parameter(:U)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Yields the cache store database. Closes the database after use if it has been loaded.

Parameters:

Returns:

  • (T.type_parameter(:U))


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'cache_store.rb', line 28

def self.use(type, &_blk)
  @db_type_reference_hash ||= T.let({}, T.nilable(T::Hash[T.untyped, T.untyped]))
  @db_type_reference_hash[type] ||= {}
  type_ref = @db_type_reference_hash[type]

  type_ref[:count] ||= 0
  type_ref[:count]  += 1

  type_ref[:db] ||= CacheStoreDatabase.new(type)

  return_value = yield(type_ref[:db])
  if type_ref[:count].positive?
    type_ref[:count] -= 1
  else
    type_ref[:count] = 0
  end

  if type_ref[:count].zero?
    type_ref[:db].write_if_dirty!
    type_ref.delete(:db)
  end

  return_value
end

Instance Method Details

#clear!void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Deletes all content from the underlying database (if it already exists).



86
87
88
89
90
91
# File 'cache_store.rb', line 86

def clear!
  return unless created?

  dirty!
  db.clear
end

#created?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if the cache file has been created for the given @type.

Returns:

  • (Boolean)


104
105
106
# File 'cache_store.rb', line 104

def created?
  cache_path.exist?
end

#delete(key) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Deletes a value from the underlying database (if it already exists).

Parameters:



77
78
79
80
81
82
# File 'cache_store.rb', line 77

def delete(key)
  return unless created?

  dirty!
  db.delete(key)
end

#each_key(&block) ⇒ Hash{Key => Value}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Performs a each_key on the underlying database.

Parameters:

  • block (T.proc.params(arg0: Key).returns(BasicObject))

Returns:



134
135
136
# File 'cache_store.rb', line 134

def each_key(&block)
  db.each_key(&block)
end

#empty?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if the cache is empty.

Returns:

  • (Boolean)


126
127
128
# File 'cache_store.rb', line 126

def empty?
  db.empty?
end

#get(key) ⇒ Value?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Gets a value from the underlying database (if it already exists).

Parameters:

Returns:



69
70
71
72
73
# File 'cache_store.rb', line 69

def get(key)
  return unless created?

  db[key]
end

#mtimeTime?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the modification time of the cache file (if it already exists).

Returns:



110
111
112
113
114
# File 'cache_store.rb', line 110

def mtime
  return unless created?

  cache_path.mtime
end

#select(&block) ⇒ Hash{Key => Value}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Performs a select on the underlying database.

Parameters:

  • block (T.proc.params(arg0: Key, arg1: Value).returns(BasicObject))

Returns:



120
121
122
# File 'cache_store.rb', line 120

def select(&block)
  db.select(&block)
end

#set(key, value) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Sets a value in the underlying database (and creates it if necessary).

Parameters:



62
63
64
65
# File 'cache_store.rb', line 62

def set(key, value)
  dirty!
  db[key] = value
end

#write_if_dirty!void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Closes the underlying database (if it is created and open).



95
96
97
98
99
100
# File 'cache_store.rb', line 95

def write_if_dirty!
  return unless dirty?

  cache_path.dirname.mkpath
  cache_path.atomic_write(JSON.dump(@db))
end