Class: Moneta::Adapters::ActiveRecord
- Inherits:
-
Object
- Object
- Moneta::Adapters::ActiveRecord
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/activerecord.rb
Overview
ActiveRecord as key/value stores
Instance Attribute Summary collapse
- #table ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
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.
82 83 84 |
# File 'lib/moneta/adapters/activerecord.rb', line 82 def initialize( = {}) @table = self.class.get() end |
Instance Attribute Details
#table ⇒ Object (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() name = 'Table_' << .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, ) 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
161 162 163 164 165 166 |
# File 'lib/moneta/adapters/activerecord.rb', line 161 def clear( = {}) @table.connection_pool.with_connection do @table.delete_all end self end |
#close ⇒ Object
Explicitly close the store
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
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.
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, = {}) @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
115 116 117 118 119 120 121 122 |
# File 'lib/moneta/adapters/activerecord.rb', line 115 def delete(key, = {}) @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
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.
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, = {}) @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, ) 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
87 88 89 90 91 |
# File 'lib/moneta/adapters/activerecord.rb', line 87 def key?(key, = {}) @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
94 95 96 97 98 99 |
# File 'lib/moneta/adapters/activerecord.rb', line 94 def load(key, = {}) @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
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/moneta/adapters/activerecord.rb', line 102 def store(key, value, = {}) @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 |