Class: CacheStoreDatabase

Inherits:
Object
  • Object
show all
Defined in:
Library/Homebrew/cache_store.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.use(type) {|CacheStoreDatabase| ... } ⇒ Object

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

Parameters:

Yields:


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'Library/Homebrew/cache_store.rb', line 16

def self.use(type)
  @db_type_reference_hash ||= {}
  @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

#created?Boolean

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

Returns:

  • (Boolean)

73
74
75
# File 'Library/Homebrew/cache_store.rb', line 73

def created?
  cache_path.exist?
end

#delete(key) ⇒ Object

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


55
56
57
58
59
60
# File 'Library/Homebrew/cache_store.rb', line 55

def delete(key)
  return unless created?

  dirty!
  db.delete(key)
end

#each_key(&block) ⇒ Array

Performs a each_key on the underlying database.

Returns:

  • (Array)

103
104
105
# File 'Library/Homebrew/cache_store.rb', line 103

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

#empty?Boolean

Returns true if the cache is empty.

Returns:

  • (Boolean)

96
97
98
# File 'Library/Homebrew/cache_store.rb', line 96

def empty?
  db.empty?
end

#get(key) ⇒ Object

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


48
49
50
51
52
# File 'Library/Homebrew/cache_store.rb', line 48

def get(key)
  return unless created?

  db[key]
end

#mtimeTime

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

Returns:

  • (Time)

80
81
82
83
84
# File 'Library/Homebrew/cache_store.rb', line 80

def mtime
  return unless created?

  cache_path.mtime
end

#select(&block) ⇒ Array

Performs a select on the underlying database.

Returns:

  • (Array)

89
90
91
# File 'Library/Homebrew/cache_store.rb', line 89

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

#set(key, value) ⇒ Object

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


42
43
44
45
# File 'Library/Homebrew/cache_store.rb', line 42

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

#write_if_dirty!Object

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


63
64
65
66
67
68
# File 'Library/Homebrew/cache_store.rb', line 63

def write_if_dirty!
  return unless dirty?

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