Class: CacheStoreDatabase
Overview
CacheStoreDatabase acts as an interface to a persistent storage mechanism
residing in the HOMEBREW_CACHE
.
Class Method Summary collapse
-
.use(type) {|CacheStoreDatabase| ... } ⇒ Object
Yields the cache store database.
Instance Method Summary collapse
-
#created? ⇒ Boolean
Returns
true
if the cache file has been created for the given@type
. -
#delete(key) ⇒ Object
Deletes a value from the underlying database (if it already exists).
-
#each_key(&block) ⇒ Array
Performs a
each_key
on the underlying database. -
#empty? ⇒ Boolean
Returns
true
if the cache is empty. -
#get(key) ⇒ Object
Gets a value from the underlying database (if it already exists).
-
#initialize(type) ⇒ nil
constructor
Creates a CacheStoreDatabase.
-
#mtime ⇒ Time
Returns the modification time of the cache file (if it already exists).
-
#select(&block) ⇒ Array
Performs a
select
on the underlying database. -
#set(key, value) ⇒ Object
Sets a value in the underlying database (and creates it if necessary).
-
#write_if_dirty! ⇒ Object
Closes the underlying database (if it is created and open).
Constructor Details
#initialize(type) ⇒ nil
Creates a CacheStoreDatabase.
45 46 47 48 |
# File 'cache_store.rb', line 45 def initialize(type) @type = type @dirty = false end |
Class Method Details
.use(type) {|CacheStoreDatabase| ... } ⇒ Object
Yields the cache store database. Closes the database after use if it has been loaded.
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 '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
.
82 83 84 |
# File 'cache_store.rb', line 82 def created? cache_path.exist? end |
#delete(key) ⇒ Object
Deletes a value from the underlying database (if it already exists).
64 65 66 67 68 69 |
# File 'cache_store.rb', line 64 def delete(key) return unless created? dirty! db.delete(key) end |
#each_key(&block) ⇒ Array
Performs a each_key
on the underlying database.
112 113 114 |
# File 'cache_store.rb', line 112 def each_key(&block) db.each_key(&block) end |
#empty? ⇒ Boolean
Returns true
if the cache is empty.
105 106 107 |
# File 'cache_store.rb', line 105 def empty? db.empty? end |
#get(key) ⇒ Object
Gets a value from the underlying database (if it already exists).
57 58 59 60 61 |
# File 'cache_store.rb', line 57 def get(key) return unless created? db[key] end |
#mtime ⇒ Time
Returns the modification time of the cache file (if it already exists).
89 90 91 92 93 |
# File 'cache_store.rb', line 89 def mtime return unless created? cache_path.mtime end |
#select(&block) ⇒ Array
Performs a select
on the underlying database.
98 99 100 |
# File 'cache_store.rb', line 98 def select(&block) db.select(&block) end |
#set(key, value) ⇒ Object
Sets a value in the underlying database (and creates it if necessary).
51 52 53 54 |
# File 'cache_store.rb', line 51 def set(key, value) dirty! db[key] = value end |
#write_if_dirty! ⇒ Object
Closes the underlying database (if it is created and open).
72 73 74 75 76 77 |
# File 'cache_store.rb', line 72 def write_if_dirty! return unless dirty? cache_path.dirname.mkpath cache_path.atomic_write(JSON.dump(@db)) end |