Class: Moneta::Adapters::ActiveRecord

Inherits:
Moneta::Adapter show all
Defined in:
lib/moneta/adapters/activerecord.rb

Overview

ActiveRecord as key/value stores

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Moneta::Adapter

#backend

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Moneta::Adapter

backend, backend_block, backend_required?

Methods included from Config

#config, included

Methods included from Defaults

#[], #[]=, #decrement, #features, #fetch, included, #supports?, #update

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :backend (Object)

    A class object inheriting from ActiveRecord::Base to use as a table

  • :table (String, Symbol) — default: :moneta

    Table name

  • :connection (Hash/String/Symbol)

    ActiveRecord connection configuration (‘Hash` or `String`), or symbol giving the name of a Rails connection (e.g. :production)

  • :create_table (Proc, Boolean)

    Proc called with a connection if table needs to be created. Pass false to skip the create table check all together.

  • :key_column (Symbol) — default: :k

    The name of the column to use for keys

  • :value_column (Symbol) — default: :v

    The name of the column to use for values



68
69
70
71
72
73
74
75
76
# File 'lib/moneta/adapters/activerecord.rb', line 68

def initialize(options = {})
  super

  # If a :backend was provided, use it to set the spec and table
  if backend
    @spec = backend.connection_pool.spec
    @table = ::Arel::Table.new(backend.table_name)
  end
end

Class Attribute Details

.connection_lockObject (readonly)



16
17
18
# File 'lib/moneta/adapters/activerecord.rb', line 16

def connection_lock
  @connection_lock
end

Instance Attribute Details

#tableObject (readonly)



11
12
13
# File 'lib/moneta/adapters/activerecord.rb', line 11

def table
  @table
end

Class Method Details

.establish_connection(spec_name) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/moneta/adapters/activerecord.rb', line 23

def establish_connection(spec_name)
  connection_lock.synchronize do
    if connection_pool = retrieve_connection_pool(spec_name)
      connection_pool
    else
      connection_handler.establish_connection(spec_name.to_sym)
    end
  end
end

.retrieve_connection_pool(spec_name) ⇒ Object



19
20
21
# File 'lib/moneta/adapters/activerecord.rb', line 19

def retrieve_connection_pool(spec_name)
  connection_handler.retrieve_connection_pool(spec_name.to_s)
end

.retrieve_or_establish_connection_pool(spec_name) ⇒ Object



33
34
35
# File 'lib/moneta/adapters/activerecord.rb', line 33

def retrieve_or_establish_connection_pool(spec_name)
  retrieve_connection_pool(spec_name) || establish_connection(spec_name)
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})


163
164
165
166
167
168
# File 'lib/moneta/adapters/activerecord.rb', line 163

def clear(options = {})
  with_connection do |conn|
    conn.delete(arel_del)
  end
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil



171
172
173
174
# File 'lib/moneta/adapters/activerecord.rb', line 171

def close
  @table = nil
  @spec = nil
end

#create(key, value, options = {}) ⇒ Boolean

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically sets a key to value if it’s not set.

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

Returns:

  • (Boolean)

    key was set



153
154
155
156
157
158
159
160
# File 'lib/moneta/adapters/activerecord.rb', line 153

def create(key, value, options = {})
  with_connection do |conn|
    conn_ins(conn, key, value)
    true
  end
rescue ::ActiveRecord::RecordNotUnique
  false
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/moneta/adapters/activerecord.rb', line 113

def delete(key, options = {})
  with_connection do |conn|
    conn.transaction do
      sel = arel_sel_key(key).project(table[config.value_column]).lock
      value = decode(conn, conn.select_value(sel))

      del = arel_del.where(table[config.key_column].eq(key))
      conn.delete(del)

      value
    end
  end
end

#each_keyEnumerator #each_key {|key| ... } ⇒ self

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.

Overloads:

  • #each_keyEnumerator

    Returns An all-the-keys enumerator.

    Returns:

    • (Enumerator)

      An all-the-keys enumerator

  • #each_key {|key| ... } ⇒ self

    Yield Parameters:

    • key (Object)

      Each key is yielded to the supplied block

    Returns:

    • (self)


88
89
90
91
92
93
94
# File 'lib/moneta/adapters/activerecord.rb', line 88

def each_key(&block)
  with_connection do |conn|
    return enum_for(:each_key) { conn.select_value(arel_sel.project(table[config.key_column].count)) } unless block_given?
    conn.select_values(arel_sel.project(table[config.key_column])).each { |k| yield(k) }
  end
  self
end

#fetch_values(*keys, **options) ⇒ Object #fetch_values(*keys, **options) {|key| ... } ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically. The default implmentation uses #values_at.

Behaves identically to #values_at except that it accepts an optional block. When supplied, the block will be called successively with each supplied key that is not present in the store. The return value of the block call will be used in place of nil in returned the array of values.

Overloads:

  • #fetch_values(*keys, **options) {|key| ... } ⇒ Array<Object, nil>

    Returns Array containing the values requested, or where keys are missing, the return values from the corresponding block calls.

    Yield Parameters:

    • key (Object)

      Each key that is not found in the store

    Yield Returns:

    • (Object, nil)

      The value to substitute for the missing one

    Returns:

    • (Array<Object, nil>)

      Array containing the values requested, or where keys are missing, the return values from the corresponding block calls



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/moneta/adapters/activerecord.rb', line 216

def fetch_values(*keys, **options)
  return values_at(*keys, **options) unless block_given?
  hash = Hash[slice(*keys, **options)]
  keys.map do |key|
    if hash.key?(key)
      hash[key]
    else
      yield key
    end
  end
end

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

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/moneta/adapters/activerecord.rb', line 128

def increment(key, amount = 1, options = {})
  with_connection do |conn|
    begin
      conn_ins(conn, key, amount.to_s)
      amount
    rescue ::ActiveRecord::RecordNotUnique
      conn.transaction do
        sel = arel_sel_key(key).project(table[config.value_column]).lock
        value = decode(conn, conn.select_value(sel))
        value = (value ? Integer(value) : 0) + amount
        # Re-raise if the upate affects no rows (i.e. row deleted after attempted insert,
        # before select for update)
        raise unless conn_upd(conn, key, value.to_s) == 1
        value
      end
    end
  end
rescue ::ActiveRecord::RecordNotUnique, ::ActiveRecord::Deadlocked
  # This handles the "no row updated" issue, above, as well as deadlocks
  # which may occur on some adapters
  tries ||= 0
  (tries += 1) <= 3 ? retry : raise
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/moneta/adapters/activerecord.rb', line 79

def key?(key, options = {})
  with_connection do |conn|
    sel = arel_sel_key(key).project(::Arel.sql('1'))
    result = conn.select_all(sel)
    !result.empty?
  end
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



97
98
99
100
101
# File 'lib/moneta/adapters/activerecord.rb', line 97

def load(key, options = {})
  with_connection do |conn|
    conn_sel_value(conn, key)
  end
end

#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

Note:

Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses #key?, #load and #store.

Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.

Overloads:

  • #merge!(pairs, options = {}) ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

    • options (Hash) (defaults to: {})

    Returns:

    • (self)
  • #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

    • options (Hash) (defaults to: {})

    Yield Parameters:

    • key (Object)

      A key that whose value is being overwritten

    • old_value (Object)

      The existing value which is being overwritten

    • new_value (Object)

      The value supplied in the method call

    Yield Returns:

    • (Object)

      The value to use for overwriting

    Returns:

    • (self)


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/moneta/adapters/activerecord.rb', line 229

def merge!(pairs, options = {})
  with_connection do |conn|
    conn.transaction do
      existing = Hash[slice(*pairs.map { |k, _| k }, lock: true, **options)]
      update_pairs, insert_pairs = pairs.partition { |k, _| existing.key?(k) }
      insert_pairs.each { |key, value| conn_ins(conn, key, encode(conn, value)) }

      if block_given?
        update_pairs.map! do |key, new_value|
          [key, yield(key, existing[key], new_value)]
        end
      end

      update_pairs.each { |key, value| conn_upd(conn, key, encode(conn, value)) }
    end
  end

  self
end

#slice(*keys, lock: false, **options) ⇒ <(Object, Object)>

Note:

The keys in the return value may be the same objects that were supplied (i.e. Object#equal?), or may simply be equal (i.e. Object#==).

Note:

Some adapters may implement this method atomically. The default implmentation uses #values_at.

Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values. Only those keys present in the store will have pairs in the return value. The return value can be any enumerable object that yields pairs, so it could be a hash, but needn’t be.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

Options Hash (**options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (<(Object, Object)>)

    A collection of key-value pairs



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/moneta/adapters/activerecord.rb', line 177

def slice(*keys, lock: false, **options)
  with_connection do |conn|
    conn.create_table(:slice_keys, temporary: true) do |t|
      t.string :key, null: false
    end

    begin
      temp_table = ::Arel::Table.new(:slice_keys)
      keys.each do |key|
        conn.insert ::Arel::InsertManager.new
          .into(temp_table)
          .insert([[temp_table[:key], key]])
      end

      sel = arel_sel
        .join(temp_table)
        .on(table[config.key_column].eq(temp_table[:key]))
        .project(table[config.key_column], table[config.value_column])
      sel = sel.lock if lock
      result = conn.select_all(sel)

      k = config.key_column.to_s
      v = config.value_column.to_s
      result.map do |row|
        [row[k], decode(conn, row[v])]
      end
    ensure
      conn.drop_table(:slice_keys)
    end
  end
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



104
105
106
107
108
109
110
# File 'lib/moneta/adapters/activerecord.rb', line 104

def store(key, value, options = {})
  with_connection do |conn|
    encoded = encode(conn, value)
    conn_ins(conn, key, encoded) unless conn_upd(conn, key, encoded) == 1
  end
  value
end

#values_at(*keys, **options) ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.

Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

Options Hash (**options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Array<Object, nil>)

    Array containing the values requested, with nil for missing values



210
211
212
213
# File 'lib/moneta/adapters/activerecord.rb', line 210

def values_at(*keys, **options)
  hash = Hash[slice(*keys, **options)]
  keys.map { |key| hash[key] }
end