Class: Hoodie::Stash::DiskStore

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodie/stash/disk_store.rb

Overview

Disk stashing method variable caching hash, string, array store.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store = file_store) ⇒ DiskStore

Initializes a new disked backed stash hash cache store.

Parameters:

  • path (String)

    Location for stash store cache.



39
40
41
# File 'lib/hoodie/stash/disk_store.rb', line 39

def initialize(store = file_store)
  @store = store
end

Instance Attribute Details

#:store(: store) ⇒ Stash::DiskStore (readonly)

Returns location of Stash::DiskStore store.

Returns:



30
# File 'lib/hoodie/stash/disk_store.rb', line 30

attr_reader :store

#storeObject (readonly)

Returns the value of attribute store.



30
31
32
# File 'lib/hoodie/stash/disk_store.rb', line 30

def store
  @store
end

Instance Method Details

#[](key) ⇒ Hash

Retrieves the value for a given key, if nothing is set returns nil.

Parameters:

  • key (Symbol, String)

    String or symbol representing the key.

Returns:

  • (Hash)

    The value for the key.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hoodie/stash/disk_store.rb', line 88

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) ⇒ undefined

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.

Parameters:

  • key (Symbol, String)

    String or symbol representing the key.

  • value (Object)

    That represents the value (optional)

  • block (&block)

    That returns the value to set (optional)

Returns:

  • (undefined)


117
118
119
# File 'lib/hoodie/stash/disk_store.rb', line 117

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.

Parameters:

  • key (Object) (defaults to: nil)

    The key to look up or store at.

Yields:

  • yields when the value is not present.

Yield Returns:

  • (Object)

    The value to store in the cache.

Returns:

  • (Object)

    The value at the key.



57
58
59
60
# File 'lib/hoodie/stash/disk_store.rb', line 57

def cache(key = nil, &code)
  key ||= Stash.caller_name
  @store[key.to_sym] ||= code.call
end

#cache_file(key) ⇒ String

Returns path to cache file with ‘key’

Returns:

  • (String)

    Path to cache file.



126
127
128
# File 'lib/hoodie/stash/disk_store.rb', line 126

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.

Parameters:

  • key (Symbol, String) (defaults to: nil)

    String or symbol representing the key to clear.

Returns:

  • (Hash)

    Previously stored Key/Value pair before the delete operation.



70
71
72
73
74
75
76
77
78
# File 'lib/hoodie/stash/disk_store.rb', line 70

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