Class: Moneta::Adapters::Cassandra
- Inherits:
-
Object
- Object
- Moneta::Adapters::Cassandra
- Includes:
- Defaults, ExpiresSupport
- Defined in:
- lib/moneta/adapters/cassandra.rb
Overview
Cassandra backend
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
Attributes included from ExpiresSupport
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the store.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#initialize(options = {}) ⇒ Cassandra
constructor
A new instance of Cassandra.
-
#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
#[], #[]=, #create, #decrement, #features, #fetch, included, #increment, #supports?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Cassandra
Returns a new instance of Cassandra.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/moneta/adapters/cassandra.rb', line 21 def initialize( = {}) self.default_expires = [:expires] @cf = ([:column_family] || 'moneta').to_sym if [:backend] @backend = [:backend] else keyspace = [:keyspace] || 'moneta' @backend = ::Cassandra.new('system', "#{[:host] || '127.0.0.1'}:#{[:port] || 9160}") unless @backend.keyspaces.include?(keyspace) cf_def = ::Cassandra::ColumnFamily.new(:keyspace => keyspace, :name => @cf.to_s) ks_def = ::Cassandra::Keyspace.new(:name => keyspace, :strategy_class => 'SimpleStrategy', :strategy_options => { 'replication_factor' => '1' }, :replication_factor => 1, :cf_defs => [cf_def]) # Wait for keyspace to be created (issue #24) 10.times do begin @backend.add_keyspace(ks_def) rescue Exception => ex warn "Moneta::Adapters::Cassandra - #{ex.}" end break if @backend.keyspaces.include?(keyspace) sleep 0.1 end end @backend.keyspace = keyspace end end |
Instance Attribute Details
#backend ⇒ Object (readonly)
12 13 14 |
# File 'lib/moneta/adapters/cassandra.rb', line 12 def backend @backend end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
89 90 91 92 93 94 |
# File 'lib/moneta/adapters/cassandra.rb', line 89 def clear( = {}) @backend.each_key(@cf) do |key| delete(key) end self end |
#close ⇒ Object
Explicitly close the store
97 98 99 100 |
# File 'lib/moneta/adapters/cassandra.rb', line 97 def close @backend.disconnect! nil end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
81 82 83 84 85 86 |
# File 'lib/moneta/adapters/cassandra.rb', line 81 def delete(key, = {}) if value = load(key, ) @backend.remove(@cf, key) value end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
52 53 54 55 56 57 58 59 |
# File 'lib/moneta/adapters/cassandra.rb', line 52 def key?(key, = {}) if @backend.exists?(@cf, key) load(key, ) if .include?(:expires) true else false end end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
62 63 64 65 66 67 68 |
# File 'lib/moneta/adapters/cassandra.rb', line 62 def load(key, = {}) if value = @backend.get(@cf, key) expires = expires_value(, nil) @backend.insert(@cf, key, {'value' => value['value'] }, :ttl => expires || nil) if expires != nil value['value'] end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
71 72 73 74 75 76 77 78 |
# File 'lib/moneta/adapters/cassandra.rb', line 71 def store(key, value, = {}) @backend.insert(@cf, key, {'value' => value}, :ttl => expires_value() || nil) value rescue # FIXME: We get spurious cassandra transport exceptions tries ||= 0 (tries += 1) < 10 ? retry : raise end |