Class: RingCache

Inherits:
Object
  • Object
show all
Defined in:
lib/ring_cache.rb,
lib/ring_cache/version.rb

Defined Under Namespace

Classes: KeyNotFoundError

Constant Summary collapse

VERSION =
'1.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RingCache

Returns a new instance of RingCache.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ring_cache.rb', line 9

def initialize(options = {})
  @duplicate_on_store = options.fetch(:duplicate_on_store, false)
  @duplicate_on_retrieve = options.fetch(:duplicate_on_retrieve, false)

  execute_on_retrieve = options.fetch(:execute_on_retrieve, [])
  @execute_on_retrieve = execute_on_retrieve.kind_of?(Array) ? execute_on_retrieve : [execute_on_retrieve]

  @capacity = options.fetch(:capacity, nil)
  @target_hit_rate = options.fetch(:target_hit_rate, nil)
  unless @target_hit_rate.nil? or (@target_hit_rate > 0.0 and @target_hit_rate < 1.0)
    raise ArgumentError, 'Invalid target_hit_rate'
  end

  reset
end

Instance Attribute Details

#capacityObject (readonly)

Returns the value of attribute capacity.



7
8
9
# File 'lib/ring_cache.rb', line 7

def capacity
  @capacity
end

#target_hit_rateObject (readonly)

Returns the value of attribute target_hit_rate.



7
8
9
# File 'lib/ring_cache.rb', line 7

def target_hit_rate
  @target_hit_rate
end

Instance Method Details

#evict(key) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/ring_cache.rb', line 25

def evict(key)
  if @cache.has_key?(key)
    @access_time_index.delete([@cache[key][:last_accessed_at], key])
    @cache.delete(key)
    true
  else
    false
  end
end

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



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ring_cache.rb', line 35

def fetch(key, options = {}, &block)
  cache_nil = options.fetch(:cache_nil, true)

  unless (data = read(key))
    data = catch(:dont_cache) do
      data_to_cache = block.call
      write(key, data_to_cache) unless data_to_cache.nil? and !cache_nil
      data_to_cache
    end
  end
  data
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ring_cache.rb', line 48

def has_key?(key)
  @cache.has_key?(key)
end

#hit_rateObject



52
53
54
# File 'lib/ring_cache.rb', line 52

def hit_rate
  (@access_count > 0) ? (@hit_count / @access_count.to_f) : 0.0
end

#last_access(key) ⇒ Object



56
57
58
# File 'lib/ring_cache.rb', line 56

def last_access(key)
  has_key?(key) ? @cache[key][:last_accessed_at] : nil
end

#read(key) ⇒ Object



93
94
95
96
97
# File 'lib/ring_cache.rb', line 93

def read(key)
  read!(key)
rescue KeyNotFoundError
  return nil
end

#read!(key) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ring_cache.rb', line 60

def read!(key)
  @access_count += 1

  if @cache.has_key?(key)
    access_time = Time.now
    @access_time_index.delete([@cache[key][:last_accessed_at], key])
    @access_time_index << [access_time, key]
    @cache[key][:last_accessed_at] = access_time

    @hit_count += 1

    data = @cache[key][:data]
    data = data.dup if @duplicate_on_retrieve and !data.nil?

    unless @execute_on_retrieve.empty? or data.nil?
      @execute_on_retrieve.each do |method|
        method = method.to_sym
        if data.respond_to?(method)
          data.send(method)
        elsif data.kind_of?(Enumerable) and data.all? { |d| d.respond_to?(method) }
          data.each { |d| d.send(method) }
        else
          raise RuntimeError, "Retrieved data does not respond to #{method.inspect}"
        end
      end
    end

    data
  else
    fail KeyNotFoundError, "Cache does not have content indexed by #{key}"
  end
end

#resetObject



99
100
101
102
103
104
105
# File 'lib/ring_cache.rb', line 99

def reset
  @cache = {}
  @access_time_index = SortedSet.new
  @access_count = 0
  @hit_count = 0
  true
end

#sizeObject



107
108
109
# File 'lib/ring_cache.rb', line 107

def size
  @cache.size
end

#write(key, data) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/ring_cache.rb', line 111

def write(key, data)
  unless evict(key)
    evict_oldest if must_evict?
  end
  data = data.dup if @duplicate_on_store and !data.nil?
  access_time = Time.now
  @cache[key] = { last_accessed_at: access_time, data: data }
  @access_time_index << [access_time, key]
  true
end