Class: Gemmy::Components::Cache

Inherits:
Hash
  • Object
show all
Defined in:
lib/gemmy/components/cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_name, hash = {}) ⇒ Cache

Returns a new instance of Cache.



16
17
18
19
20
# File 'lib/gemmy/components/cache.rb', line 16

def initialize(db_name, hash={})
  cache_path = self.class.setup_cache_folder
  @db = hash.persisted "#{cache_path}/#{db_name}.yaml"
  @db.set_state hash
end

Class Method Details

.setup_cache_folderObject



8
9
10
11
12
13
14
# File 'lib/gemmy/components/cache.rb', line 8

def self.setup_cache_folder
  cache_path = ENV["GEMMY_CACHE_PATH"] || "#{home}/gemmy/caches"
  unless Dir.exists?(cache_path)
    `mkdir -p #{cache_path}`
  end
  cache_path
end

Instance Method Details

#clearObject



54
55
56
57
# File 'lib/gemmy/components/cache.rb', line 54

def clear
  each_key &m(:delete)
  @db.clear
end

#dataObject



31
32
33
# File 'lib/gemmy/components/cache.rb', line 31

def data
  @db.data
end

#get(*keys) ⇒ Object



39
40
41
# File 'lib/gemmy/components/cache.rb', line 39

def get(*keys)
  @db.dig *keys
end

#get_or_set(*keys, &blk) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/gemmy/components/cache.rb', line 22

def get_or_set(*keys, &blk)
  result = get(*keys)
  if result.blank?
    result = blk.call
    set(*keys, result)
  end
  result
end

#keysObject



35
36
37
# File 'lib/gemmy/components/cache.rb', line 35

def keys
  @db.data.keys
end

#set(*keys, val) ⇒ Object



49
50
51
52
# File 'lib/gemmy/components/cache.rb', line 49

def set(*keys, val)
  Gemmy.patch("hash/i/bury").bury(self, *keys, val)
  @db.set(*keys, val)
end

#set_state(hash) ⇒ Object



43
44
45
46
47
# File 'lib/gemmy/components/cache.rb', line 43

def set_state(hash)
  @db.set_state hash
  each_key &m(:delete)
  deep_merge! hash
end