Class: ActiveSupport::Cache::LmcStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::LmcStore
- Defined in:
- lib/active_support/cache/lmc_store.rb
Constant Summary collapse
- DEFAULT_DIR =
Dir.tmpdir
- DEFAULT_NAME =
'localmemcache'
- DEFAULT_SIZE =
also the minimum size, as localmemcache is unreliable below this value
16.megabytes
Class Method Summary collapse
Instance Method Summary collapse
- #cleanup(options = nil) ⇒ Object
- #clear(options = nil) ⇒ Object
- #decrement(name, amount = 1, options = nil) ⇒ Object
- #delete_matched(matcher, options = nil) ⇒ Object
- #free_bytes ⇒ Object
- #increment(name, amount = 1, options = nil) ⇒ Object
-
#initialize(options = {}) ⇒ LmcStore
constructor
A new instance of LmcStore.
-
#inspect ⇒ Object
:nodoc:.
- #prune(target_size, max_time = nil) ⇒ Object
- #pruning? ⇒ Boolean
- #synchronize(&block) ⇒ Object
- #total_bytes ⇒ Object
- #used_bytes ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ LmcStore
Returns a new instance of LmcStore.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/active_support/cache/lmc_store.rb', line 12 def initialize( = {}) super directory = .fetch(:directory, DEFAULT_DIR) name = .fetch(:name, DEFAULT_NAME) size = .fetch(:size, DEFAULT_SIZE) = { filename: Pathname.new(directory).join(name).to_s, size_mb: [DEFAULT_SIZE, size].max / 1.megabyte } @data = LocalMemCache.new() @max_prune_time = [:max_prune_time] || 2 @monitor = Monitor.new @pruning = false end |
Class Method Details
.supports_cache_versioning? ⇒ Boolean
30 31 32 |
# File 'lib/active_support/cache/lmc_store.rb', line 30 def self.supports_cache_versioning? true end |
Instance Method Details
#cleanup(options = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_support/cache/lmc_store.rb', line 40 def cleanup( = nil) = () instrument(:cleanup, size: used_bytes) do keys = synchronize { @data.keys } keys.each do |key| payload = @data[key] entry = deserialize_entry(payload) if payload delete_entry(key, **) if entry&.expired? end end end |
#clear(options = nil) ⇒ Object
34 35 36 37 38 |
# File 'lib/active_support/cache/lmc_store.rb', line 34 def clear( = nil) synchronize do @data.clear end end |
#decrement(name, amount = 1, options = nil) ⇒ Object
78 79 80 |
# File 'lib/active_support/cache/lmc_store.rb', line 78 def decrement(name, amount = 1, = nil) modify_value(name, -amount, ) end |
#delete_matched(matcher, options = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/active_support/cache/lmc_store.rb', line 82 def delete_matched(matcher, = nil) = () instrument(:delete_matched, matcher.inspect) do matcher = key_matcher(matcher, ) keys = synchronize { @data.keys } keys.each do |key| delete_entry(key, **) if key.match(matcher) end end end |
#free_bytes ⇒ Object
105 106 107 |
# File 'lib/active_support/cache/lmc_store.rb', line 105 def free_bytes @data.shm_status[:free_bytes] end |
#increment(name, amount = 1, options = nil) ⇒ Object
74 75 76 |
# File 'lib/active_support/cache/lmc_store.rb', line 74 def increment(name, amount = 1, = nil) modify_value(name, amount, ) end |
#inspect ⇒ Object
:nodoc:
93 94 95 |
# File 'lib/active_support/cache/lmc_store.rb', line 93 def inspect # :nodoc: "#<#{self.class.name} entries=#{@data.size}, free=#{free_bytes}/#{total_bytes}, options=#{@options.inspect}>" end |
#prune(target_size, max_time = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_support/cache/lmc_store.rb', line 52 def prune(target_size, max_time = nil) return if pruning? @pruning = true begin start_time = Concurrent.monotonic_time cleanup instrument(:prune, target_size, from: used_bytes) do loop do key, _ = @data.random_pair delete_entry(key, **) return if used_bytes <= target_size || (max_time && Concurrent.monotonic_time - start_time > max_time) end end ensure @pruning = false end end |
#pruning? ⇒ Boolean
70 71 72 |
# File 'lib/active_support/cache/lmc_store.rb', line 70 def pruning? @pruning end |
#synchronize(&block) ⇒ Object
97 98 99 |
# File 'lib/active_support/cache/lmc_store.rb', line 97 def synchronize(&block) @monitor.synchronize(&block) end |
#total_bytes ⇒ Object
101 102 103 |
# File 'lib/active_support/cache/lmc_store.rb', line 101 def total_bytes @data.shm_status[:total_bytes] end |
#used_bytes ⇒ Object
109 110 111 |
# File 'lib/active_support/cache/lmc_store.rb', line 109 def used_bytes total_bytes - free_bytes end |