Class: BennyCache::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/benny_cache/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



3
4
5
# File 'lib/benny_cache/cache.rb', line 3

def initialize 
  @cache = {}
end

Instance Method Details

#clear(options = nil) ⇒ Object



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

def clear(options = nil)
  @cache = {}
end

#delete(key, options = nil) ⇒ Object



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

def delete(key, options = nil)
  @cache.delete(key)
end

#fetch(key, options = nil, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/benny_cache/cache.rb', line 7

def fetch(key, options = nil, &block)
  val = @cache[key]

  if val.nil? && block_given?
    val = block.call()
    @cache[key] = val
  end

  begin
    val = val.dup unless val.nil?
  rescue TypeError
    #okay
  end
  val

end

#read(key, options = nil) ⇒ Object



24
25
26
27
28
# File 'lib/benny_cache/cache.rb', line 24

def read(key, options = nil)
  val = @cache[key]
  val = val.dup unless val.nil?

end

#write(key, val, options = nil) ⇒ Object



30
31
32
33
# File 'lib/benny_cache/cache.rb', line 30

def write(key, val, options = nil)
  @cache[key] = val.dup
  return true
end