Class: ActiveSupport::Cache::Lru

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Lru

Returns a new instance of Lru.



8
9
10
11
12
# File 'lib/active_support/cache/lru.rb', line 8

def initialize(options)
  @options = options
  @sized_list = SizedList.new @options[:max_size]
  @sized_list.enable_time_based_stats = !! @options[:enable_time_based_stats]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/active_support/cache/lru.rb', line 5

def options
  @options
end

#silenceObject (readonly) Also known as: silence?

Returns the value of attribute silence.



5
6
7
# File 'lib/active_support/cache/lru.rb', line 5

def silence
  @silence
end

Instance Method Details

#decrement(name, amount = 1, options = nil) ⇒ Object



94
95
96
97
98
# File 'lib/active_support/cache/lru.rb', line 94

def decrement(name, amount = 1, options=nil)
  if v = @sized_list[name]
    @sized_list[name] = v - amount
  end
end

#delete(name, options = nil) ⇒ Object



74
75
76
# File 'lib/active_support/cache/lru.rb', line 74

def delete(name, options=nil)
  @sized_list.delete name
end

#exist?(name, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/active_support/cache/lru.rb', line 70

def exist?(name, options=nil)
  @sized_list.exist? name
end

#fetch(name, options = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_support/cache/lru.rb', line 28

def fetch(name, options=nil)
  if block_given?
    entry = instrument(:read, name, options) do |payload|
      payload[:super_operation] = :fetch if payload
      read(name, options)
    end

    if !entry.nil?
      instrument(:fetch_hit, name, options) { |payload| }
      entry
    else
      result = instrument(:generate, name, options) do |payload|
        yield
      end
      write(name, result, options)
      result
    end
  else
    read(name, options)
  end
end

#increment(name, amount = 1, options = nil) ⇒ Object



88
89
90
91
92
# File 'lib/active_support/cache/lru.rb', line 88

def increment(name, amount = 1, options=nil)
  if v = @sized_list[name]
    @sized_list[name] = v + amount
  end
end

#keysObject



105
106
107
# File 'lib/active_support/cache/lru.rb', line 105

def keys
  @sized_list.keys
end

#muteObject

Silence the logger within a block.



21
22
23
24
25
26
# File 'lib/active_support/cache/lru.rb', line 21

def mute
  previous_silence, @silence = defined?(@silence) && @silence, true
  yield
ensure
  @silence = previous_silence
end

#read(name, options = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/active_support/cache/lru.rb', line 50

def read(name, options=nil)
  instrument(:read, name, options) do |payload|
    entry = @sized_list[name]
    payload[:hit] = !!entry if payload
    entry
  end
end

#read_multi(*names) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/active_support/cache/lru.rb', line 78

def read_multi(*names)
  {}.tap do |results|
    names.each do |n|
      if v = @sized_list[n]
        results[n] = v
      end
    end
  end
end

#reset(options = {}) ⇒ Object Also known as: clear



100
101
102
# File 'lib/active_support/cache/lru.rb', line 100

def reset(options={})
  @sized_list = SizedList.new @options[:max_size]
end

#silence!Object

Silence the logger.



15
16
17
18
# File 'lib/active_support/cache/lru.rb', line 15

def silence!
  @silence = true
  self
end

#write(name, value, options = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_support/cache/lru.rb', line 58

def write(name, value, options=nil)
  instrument(:write, name, options) do |payload|
    @sized_list[name] = value
    if payload
      payload[:eviction] = @sized_list.evicted?
      payload[:total_evictions] = @sized_list.evictions
      payload[:eviction_frequency] = @sized_list.eviction_frequency
      payload[:last_time_between_evictions] = @sized_list.last_time_between_evictions
    end
  end
end