Class: Condenser::CacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/condenser/cache_store.rb

Instance Method Summary collapse

Instance Method Details

#delete(key) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/condenser/cache_store.rb', line 34

def delete(key)
  @key_access.delete(key)
  if value = @cache.delete(key)
    @cache_size -= cached_size(key, value)
    true
  else
    false
  end
end

#fetch(key) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/condenser/cache_store.rb', line 5

def fetch(key)
  value = get(key)
  
  if value.nil?
    value = yield
    set(key, Marshal.dump(value))
  else
    value = Marshal.load(value)
  end
  value
end

#fetch_if(key_or_proc, key, &block) ⇒ Object

Try to fetch key_or_proc if key exists.

If the key exist it will fetch key_or_proc from the cache

If key does not exists run the block and store results under key_or_proc.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/condenser/cache_store.rb', line 22

def fetch_if(key_or_proc, key, &block)
  if get(key)
    key_or_proc = key_or_proc.call if key_or_proc.is_a?(Proc)
    value = fetch(key_or_proc, &block)
  else
    value = block.call
    key_or_proc = key_or_proc.call if key_or_proc.is_a?(Proc)
    set(key_or_proc, Marshal.dump(value))
  end
  value
end