Class: ActiveSupport::Cache::LmcStore

Inherits:
Store
  • Object
show all
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

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(options = {})
  super options

  directory = options.fetch(:directory, DEFAULT_DIR)
  name      = options.fetch(:name, DEFAULT_NAME)
  size      = options.fetch(:size, DEFAULT_SIZE)

  data_store_options = {
    filename: Pathname.new(directory).join(name).to_s,
    size_mb:  [DEFAULT_SIZE, size].max / 1.megabyte
  }

  @data           = LocalMemCache.new(data_store_options)
  @max_prune_time = options[:max_prune_time] || 2
  @monitor        = Monitor.new
  @pruning        = false
end

Class Method Details

.supports_cache_versioning?Boolean

Returns:

  • (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(options = nil)
  options = merged_options(options)
  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, **options) 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(options = 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, options = nil)
  modify_value(name, -amount, options)
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, options = nil)
  options = merged_options(options)
  instrument(:delete_matched, matcher.inspect) do
    matcher = key_matcher(matcher, options)
    keys    = synchronize { @data.keys }
    keys.each do |key|
      delete_entry(key, **options) if key.match(matcher)
    end
  end
end

#free_bytesObject



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, options = nil)
  modify_value(name, amount, options)
end

#inspectObject

: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, **options)
        return if used_bytes <= target_size || (max_time && Concurrent.monotonic_time - start_time > max_time)
      end
    end
  ensure
    @pruning = false
  end
end

#pruning?Boolean

Returns:

  • (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_bytesObject



101
102
103
# File 'lib/active_support/cache/lmc_store.rb', line 101

def total_bytes
  @data.shm_status[:total_bytes]
end

#used_bytesObject



109
110
111
# File 'lib/active_support/cache/lmc_store.rb', line 109

def used_bytes
  total_bytes - free_bytes
end