Class: Sinarey::SmartCache

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

Instance Method Summary collapse

Constructor Details

#initialize(data_size, fifo_size) ⇒ SmartCache

Returns a new instance of SmartCache.



14
15
16
17
# File 'lib/sinarey_cache.rb', line 14

def initialize(data_size,fifo_size)
  @fifo = Sinarey::FifoCache.new(fifo_size)
  @data = Sinarey::LruCache.new(data_size)
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
# File 'lib/sinarey_cache.rb', line 19

def [](key)
  @data[key]
end

#[]=(key, val, options = {}) ⇒ Object Also known as: store



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sinarey_cache.rb', line 31

def []=(key,val,options={})
  return @data[key] = val if options[:force]
  val,version = val,options[:uuid]
  if @fifo.member?(key)
    if version.nil?
      @data[key] = val
    elsif version!= @fifo[key]
      @fifo[key] = version
      @data[key] = val
    end
  else
    @fifo[key] = version
  end
  val
end

#clearObject



66
67
68
69
# File 'lib/sinarey_cache.rb', line 66

def clear
  @fifo.clear
  @data.clear
end

#countObject



57
58
59
# File 'lib/sinarey_cache.rb', line 57

def count
  @data.count
end

#delete(k) ⇒ Object



61
62
63
64
# File 'lib/sinarey_cache.rb', line 61

def delete(k)
  @fifo.delete(k)
  @data.delete(k)
end

#fetch(key) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/sinarey_cache.rb', line 23

def fetch(key)
  if cache = @data[key]
    return cache
  else
    yield if block_given?
  end
end

#getset(key, options = {}) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/sinarey_cache.rb', line 49

def getset(key,options={})
  if cache = @data[key]
    return cache
  else
    store(key,yield,options) if block_given?
  end
end