Class: ActiveSupport::Cache::SmartMemCache

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

Instance Method Summary collapse

Constructor Details

#initialize(*addresses, **args) ⇒ SmartMemCache

known options:

ActiveSupport::Cache - universal options
  :namespace{nil}, :compress{true}, :compress_threshold{1k}, :coder, :expires_in{0}
  :race_condition_ttl{0}, :skip_nil{false}
ActiveSupport::Cache::Store
  pool: {:size{5}, :timeout{5}}
Dalli::Client
  :failover{true}, :serializer{Marshall}, :compressor{zlib}, :cache_nils{false}
  :namespace{nil}, :expires_in{0}, :compress{false} (separate from AS::C's same options above)
  :threadsafe{true} (using ConnPool turns this off automatically)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_support/cache/smart_mem_cache.rb', line 26

def initialize(*addresses, **args)
  args.reverse_merge!(
    namespace:          ENV['MEMCACHE_NAMESPACE'],
    expires_in:         1.day,
    race_condition_ttl: 5.seconds,
    failover:           true,
  )
  if ActiveSupport.version >= '7.1.0.a'
    args[:pool] ||= {}
    args[:pool][:size] ||= Integer(ENV.fetch('RAILS_MAX_THREADS'){ 5 })
  else
    args[:pool_size] ||= Integer(ENV.fetch('RAILS_MAX_THREADS'){ 5 })
  end
  addresses.push Array(args.delete(:url)) if addresses.empty? && args.key?(:url)
  super(*addresses, args)
end

Instance Method Details

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



59
60
61
62
63
64
65
66
# File 'lib/active_support/cache/smart_mem_cache.rb', line 59

def decrement(name, amount = 1, options = nil)
  options = merged_options(options)
  instrument(:decrement, name, amount: amount) do
    rescue_error_with nil do
      @data.with { |c| c.decr(normalize_key(name, options), amount, options[:expires_in], 0) }
    end
  end
end

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

MemCacheStore#increment docs say it will init any invalid value to 0. In reality, it will only increment a pre-existing, raw value. everything else returns nil or an error. This fixes init of missing value on both increment() and decrement(). Preexisting, invalid values still return errors.



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

def increment(name, amount = 1, options = nil)
  options = merged_options(options)
  instrument(:increment, name, amount: amount) do
    rescue_error_with nil do
      @data.with { |c| c.incr(normalize_key(name, options), amount, options[:expires_in], amount) }
    end
  end
end