Class: Moneta::Adapters::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
Defaults, IncrementSupport
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

#[], #[]=, #close, #decrement, #fetch

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ ActiveRecord

Returns a new instance of ActiveRecord

Options Hash (options):

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

    Table name

  • :connection (Hash)

    ActiveRecord connection configuration


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/moneta/adapters/activerecord.rb', line 20

def initialize(options = {})
  table = options[:table] || 'moneta'
  @table = self.class.tables[table] ||=
    begin
      c = Class.new(::ActiveRecord::Base)
      c.table_name = table
      c.primary_key = :k
      c
    end
  @table.establish_connection(options[:connection]) if options[:connection]
  unless @table.table_exists?
    @table.connection.create_table(@table.table_name, :id => false) do |t|
      # Do not use binary columns (Issue #17)
      t.string :k, :null => false
      t.string :v
    end
    @table.connection.add_index(@table.table_name, :k, :unique => true)
  end
end

Instance Attribute Details

#tableObject (readonly)


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

def table
  @table
end

Class Method Details

.tablesObject


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

def self.tables
  @tables ||= {}
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store


77
78
79
80
# File 'lib/moneta/adapters/activerecord.rb', line 77

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

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

Delete the key from the store and return the current value

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware


60
61
62
63
64
65
# File 'lib/moneta/adapters/activerecord.rb', line 60

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


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

def increment(key, amount = 1, options = {})
  record = @table.where(:k => key).lock.first_or_initialize
  value = convert_for_increment(record.v) + amount
  record.v = value.to_s
  record.save
  value
end

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

Exists the value with key

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See `Moneta::Expires`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware


41
42
43
# File 'lib/moneta/adapters/activerecord.rb', line 41

def key?(key, options = {})
  !@table.where(:k => key).empty?
end

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

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware


46
47
48
49
# File 'lib/moneta/adapters/activerecord.rb', line 46

def load(key, options = {})
  record = @table.select(:v).where(:k => key).first
  record && record.v
end

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

Store value with key

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware


52
53
54
55
56
57
# File 'lib/moneta/adapters/activerecord.rb', line 52

def store(key, value, options = {})
  record = @table.select(:k).where(:k => key).first_or_initialize
  record.v = value
  record.save
  value
end