Class: Moneta::Adapters::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/adapters/activerecord.rb

Overview

ActiveRecord as key/value stores

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Defaults

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

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):

  • :table (String) — default: 'moneta'

    Table name

  • :connection (Hash)

    ActiveRecord connection configuration


82
83
84
# File 'lib/moneta/adapters/activerecord.rb', line 82

def initialize(options = {})
  @table = self.class.get(options)
end

Instance Attribute Details

#tableObject (readonly)


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

def table
  @table
end

Class Method Details

.get(options) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/moneta/adapters/activerecord.rb', line 27

def get(options)
  name = 'Table_' << options.inspect.gsub(/[^\w]+/) do
    $&.unpack('H2' * $&.bytesize).join.upcase
  end
  @table_mutex.synchronize do
    table =
      if const_defined?(name)
        const_get(name)
      else
        create(name, options)
      end
    @table_refcount[table] ||= 0
    @table_refcount[table] += 1
    table
  end
end

.release(table) ⇒ Object


18
19
20
21
22
23
24
25
# File 'lib/moneta/adapters/activerecord.rb', line 18

def release(table)
  @table_mutex.synchronize do
    if (@table_refcount[table] -= 1) <= 0
      remove_const(table.name.sub(/^.*::/, ''))
      @table_refcount.delete(table)
    end
  end
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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

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

def clear(options = {})
  @table.connection_pool.with_connection do
    @table.delete_all
  end
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil


169
170
171
172
# File 'lib/moneta/adapters/activerecord.rb', line 169

def close
  self.class.release(@table)
  @table = 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


146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/moneta/adapters/activerecord.rb', line 146

def create(key, value, options = {})
  @table.connection_pool.with_connection do
    record = @table.new
    record.k = key
    record.v = value
    record.save
    true
  end
rescue
  # FIXME: This catches too many errors
  # it should only catch a not-unique-exception
  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


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

def delete(key, options = {})
  @table.connection_pool.with_connection do
    if record = @table.where(k: key).first
      record.destroy
      record.v
    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


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/moneta/adapters/activerecord.rb', line 125

def increment(key, amount = 1, options = {})
  @table.connection_pool.with_connection do
    @table.transaction do
      if record = @table.where(k: key).lock.first
        value = Utils.to_int(record.v) + amount
        record.v = value.to_s
        record.save
        value
      elsif create(key, amount.to_s, options)
        amount
      else
        raise 'Concurrent modification'
      end
    end
  end
rescue
  tries ||= 0
  (tries += 1) < 10 ? 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)

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

def key?(key, options = {})
  @table.connection_pool.with_connection do
    !@table.where(k: key).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


94
95
96
97
98
99
# File 'lib/moneta/adapters/activerecord.rb', line 94

def load(key, options = {})
  @table.connection_pool.with_connection do
    record = @table.select(:v).where(k: key).first
    record && record.v
  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


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

def store(key, value, options = {})
  @table.connection_pool.with_connection do
    record = @table.select(:k).where(k: key).first_or_initialize
    record.v = value
    record.save
    value
  end
rescue
  tries ||= 0
  (tries += 1) < 10 ? retry : raise
end