Class: ActiveSupport::Cache::Lru
- Inherits:
-
Object
- Object
- ActiveSupport::Cache::Lru
- Defined in:
- lib/active_support/cache/lru.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#silence ⇒ Object
(also: #silence?)
readonly
Returns the value of attribute silence.
Instance Method Summary collapse
- #decrement(name, amount = 1, options = nil) ⇒ Object
- #delete(name, options = nil) ⇒ Object
- #exist?(name, options = nil) ⇒ Boolean
- #fetch(name, options = nil) ⇒ Object
- #increment(name, amount = 1, options = nil) ⇒ Object
-
#initialize(options) ⇒ Lru
constructor
A new instance of Lru.
- #keys ⇒ Object
-
#mute ⇒ Object
Silence the logger within a block.
- #read(name, options = nil) ⇒ Object
- #read_multi(*names) ⇒ Object
- #reset(options = {}) ⇒ Object (also: #clear)
-
#silence! ⇒ Object
Silence the logger.
- #write(name, value, options = nil) ⇒ Object
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() = @sized_list = SizedList.new [:max_size] @sized_list.enable_time_based_stats = !! [:enable_time_based_stats] end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/active_support/cache/lru.rb', line 5 def end |
#silence ⇒ Object (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, =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, =nil) @sized_list.delete name end |
#exist?(name, options = nil) ⇒ Boolean
70 71 72 |
# File 'lib/active_support/cache/lru.rb', line 70 def exist?(name, =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, =nil) if block_given? entry = instrument(:read, name, ) do |payload| payload[:super_operation] = :fetch if payload read(name, ) end if !entry.nil? instrument(:fetch_hit, name, ) { |payload| } entry else result = instrument(:generate, name, ) do |payload| yield end write(name, result, ) result end else read(name, ) 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, =nil) if v = @sized_list[name] @sized_list[name] = v + amount end end |
#keys ⇒ Object
105 106 107 |
# File 'lib/active_support/cache/lru.rb', line 105 def keys @sized_list.keys end |
#mute ⇒ Object
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, =nil) instrument(:read, name, ) 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(={}) @sized_list = SizedList.new [: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, =nil) instrument(:write, name, ) 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 |