Class: CacheStoreDatabase

Inherits:
Object
  • Object
show all
Defined in:
brew/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:



15
16
17
18
19
20
# File 'brew/Library/Homebrew/cache_store.rb', line 15

def self.use(type)
  database = CacheStoreDatabase.new(type)
  return_value = yield(database)
  database.close_if_open!
  return_value
end

Instance Method Details

#close_if_open!Object

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



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

def close_if_open!
  return unless @db

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

#created?Boolean

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

Returns:

  • (Boolean)


52
53
54
# File 'brew/Library/Homebrew/cache_store.rb', line 52

def created?
  cache_path.exist?
end

#delete(key) ⇒ Object

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



35
36
37
38
39
# File 'brew/Library/Homebrew/cache_store.rb', line 35

def delete(key)
  return unless created?

  db.delete(key)
end

#empty?Boolean

Returns true if the cache is empty.

Returns:

  • (Boolean)


75
76
77
# File 'brew/Library/Homebrew/cache_store.rb', line 75

def empty?
  db.empty?
end

#get(key) ⇒ Object

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



28
29
30
31
32
# File 'brew/Library/Homebrew/cache_store.rb', line 28

def get(key)
  return unless created?

  db[key]
end

#mtimeTime

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

Returns:

  • (Time)


59
60
61
62
63
# File 'brew/Library/Homebrew/cache_store.rb', line 59

def mtime
  return unless created?

  cache_path.mtime
end

#select(&block) ⇒ Array

Performs a select on the underlying database.

Returns:

  • (Array)


68
69
70
# File 'brew/Library/Homebrew/cache_store.rb', line 68

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

#set(key, value) ⇒ Object

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



23
24
25
# File 'brew/Library/Homebrew/cache_store.rb', line 23

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