Class: ChefStash::DiskStore
- Inherits:
-
Object
- Object
- ChefStash::DiskStore
- Defined in:
- lib/chef_stash/disk_store.rb
Overview
Disk stashing method variable caching hash, string, array store.
Instance Attribute Summary collapse
-
#store ⇒ String
readonly
Location of DiskStash::Cache.store.
Instance Method Summary collapse
-
#[](key) ⇒ Hash, ...
Retrieves the value for a given key, if nothing is set, returns nil.
-
#[]=(key, value) ⇒ Object
Store the given value with the given key, either an an argument or block.
-
#cache(key = nil) { ... } ⇒ Object
Retrieves a value from the cache, if available and not expired, or yields to a block that calculates the value to be stored in the cache.
-
#cache_file(key) ⇒ Object
returns path to cache file with ‘key’.
-
#clear!(key = nil) ⇒ Hash
Clear the whole stash or the value of a key.
-
#initialize(store = file_store) ⇒ Object
constructor
Initializes a new disked backed stash hash cache store.
Constructor Details
#initialize(store = file_store) ⇒ Object
Initializes a new disked backed stash hash cache store.
37 38 39 |
# File 'lib/chef_stash/disk_store.rb', line 37 def initialize(store = file_store) @store = store end |
Instance Attribute Details
#store ⇒ String (readonly)
Returns location of DiskStash::Cache.store.
29 30 31 |
# File 'lib/chef_stash/disk_store.rb', line 29 def store @store end |
Instance Method Details
#[](key) ⇒ Hash, ...
Retrieves the value for a given key, if nothing is set, returns nil
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/chef_stash/disk_store.rb', line 85 def [](key) if key.is_a? Array hash = {} key.each do |k| hash[k] = Marshal.load(read_cache_file(k)) end hash unless hash.empty? else Marshal.load(read_cache_file(key)) end rescue Errno::ENOENT nil # key hasn't been created end |
#[]=(key, value) ⇒ Object
Store the given value with the given key, either an an argument or block. If a previous value was set it will be overwritten with the new value.
109 110 111 |
# File 'lib/chef_stash/disk_store.rb', line 109 def []=(key, value) write_cache_file(key, Marshal.dump(value)) end |
#cache(key = nil) { ... } ⇒ Object
Retrieves a value from the cache, if available and not expired, or yields to a block that calculates the value to be stored in the cache.
55 56 57 58 |
# File 'lib/chef_stash/disk_store.rb', line 55 def cache(key = nil, &code) key ||= Stash.caller_name @store[key.to_sym] ||= code.call end |
#cache_file(key) ⇒ Object
returns path to cache file with ‘key’
114 115 116 |
# File 'lib/chef_stash/disk_store.rb', line 114 def cache_file(key) File.join(store, key.to_s + '.cache') end |
#clear!(key = nil) ⇒ Hash
Clear the whole stash or the value of a key
representing the key to clear
returns {}
68 69 70 71 72 73 74 75 76 |
# File 'lib/chef_stash/disk_store.rb', line 68 def clear!(key = nil) if key.nil? Dir[File.join(store, '*.cache')].each do |file| File.delete(file) end else File.delete(cache_file(key)) if File.exist?(cache_file(key)) end end |