Class: Gemmy::Components::Cache

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

Constant Summary collapse

CachePath =
ENV["GEMMY_CACHE_PATH"] || "#{home}/gemmy/caches"

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Cache.



14
15
16
17
# File 'lib/gemmy/components/cache.rb', line 14

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

Instance Method Details

#clearObject



51
52
53
54
# File 'lib/gemmy/components/cache.rb', line 51

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

#dataObject



28
29
30
# File 'lib/gemmy/components/cache.rb', line 28

def data
  @db.data
end

#get(*keys) ⇒ Object



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

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

#get_or_set(*keys, &blk) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/gemmy/components/cache.rb', line 19

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

#keysObject



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

def keys
  @db.data.keys
end

#set(*keys, val) ⇒ Object



46
47
48
49
# File 'lib/gemmy/components/cache.rb', line 46

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

#set_state(hash) ⇒ Object



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

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