Class: Gemmy::Components::Cache

Inherits:
Hash
  • Object
show all
Defined in:
lib/gemmy/patches_loaded/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
21
# File 'lib/gemmy/patches_loaded/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"
  # copy db state into memory
  @db.data.each { |k,v| self[k] = v.deep_dup }
end

Class Method Details

.setup_cache_folderObject



8
9
10
11
12
13
14
# File 'lib/gemmy/patches_loaded/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



68
69
70
71
# File 'lib/gemmy/patches_loaded/components/cache.rb', line 68

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

#dataObject



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

def data
  @db.data
end

#dig_delete(*keys) ⇒ Object

Delete functions like “dig_delete” I.e. if given a few keys as arguments, it will treat only the last as a delete key and all the rest as dig keys.



47
48
49
50
51
52
53
54
55
# File 'lib/gemmy/patches_loaded/components/cache.rb', line 47

def dig_delete(*keys)
  @db.dig_delete *keys
  key_to_delete = keys.pop
  if keys.empty?
    self.delete key_to_delete
  else
    self.dig(*keys).delete key_to_delete
  end
end

#get(*keys) ⇒ Object



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

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

#get_or_set(*keys, &blk) ⇒ Object



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

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

#keysObject



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

def keys
  @db.data.keys
end

#set(*keys, val) ⇒ Object



63
64
65
66
# File 'lib/gemmy/patches_loaded/components/cache.rb', line 63

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

#set_state(hash) ⇒ Object



57
58
59
60
61
# File 'lib/gemmy/patches_loaded/components/cache.rb', line 57

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