Class: ActiveSupport::Cache::MonetaStore

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

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ MonetaStore

Returns a new instance of MonetaStore

Raises:

  • (ArgumentError)

5
6
7
8
9
10
# File 'lib/active_support/cache/moneta_store.rb', line 5

def initialize(options = nil)
  raise ArgumentError, 'Option :store is required' unless @store = options.delete(:store)
  @store = ::Moneta.new(@store, expires: true) if Symbol === @store
  super(options)
  extend Strategy::LocalCache
end

Instance Method Details

#clear(options = nil) ⇒ Object


26
27
28
29
30
31
# File 'lib/active_support/cache/moneta_store.rb', line 26

def clear(options = nil)
  options = merged_options(options)
  instrument(:clear, nil, nil) do
    @store.clear(moneta_options(options))
  end
end

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


19
20
21
22
23
24
# File 'lib/active_support/cache/moneta_store.rb', line 19

def decrement(key, amount = 1, options = nil)
  options = merged_options(options)
  instrument(:decrement, key, amount: amount) do
    @store.increment(normalize_key(key, options), -amount, moneta_options(options))
  end
end

#fetch_multi(*names) ⇒ Object

Raises:

  • (ArgumentError)

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/active_support/cache/moneta_store.rb', line 38

def fetch_multi(*names)
  raise ArgumentError, "Missing block: `Cache#fetch_multi` requires a block." \
    unless block_given?

  options = names.extract_options!
  options = merged_options(options)

  instrument :read_multi, names, options do |payload|
    read_multi_entries(names, options).tap do |results|
      payload[:hits] = results.keys
      payload[:super_operation] = :fetch_multi

      writes = {}

      (names - results.keys).each do |name|
        results[name] = writes[name] = yield(name)
      end

      write_multi writes, options
    end
  end
end

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


12
13
14
15
16
17
# File 'lib/active_support/cache/moneta_store.rb', line 12

def increment(key, amount = 1, options = nil)
  options = merged_options(options)
  instrument(:increment, key, amount: amount) do
    @store.increment(normalize_key(key, options), amount, moneta_options(options))
  end
end

#read_multi(*names) ⇒ Object


61
62
63
64
65
66
67
68
69
70
# File 'lib/active_support/cache/moneta_store.rb', line 61

def read_multi(*names)
  options = names.extract_options!
  options = merged_options(options)

  instrument :read_multi, names, options do |payload|
    read_multi_entries(names, options).tap do |results|
      payload[:hits] = results.keys
    end
  end
end

#write_multi(hash, options = nil) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_support/cache/moneta_store.rb', line 72

def write_multi(hash, options = nil)
  options = merged_options(options)

  instrument :write_multi, hash, options do |payload|
    entries = hash.each_with_object({}) do |(name, value), memo|
      memo[normalize_key(name, options)] = \
        Entry.new(value, options.merge(version: normalize_version(name, options)))
    end

    write_multi_entries entries, options
  end
end