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



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

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



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

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.



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

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