Class: Moneta::Adapters::Cassandra
- Inherits:
-
Object
- Object
- Moneta::Adapters::Cassandra
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/cassandra.rb
Overview
Cassandra backend
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this 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
#[], #[]=, #close, #decrement, #fetch, #increment
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Cassandra
Returns a new instance of Cassandra.
19 20 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 |
# File 'lib/moneta/adapters/cassandra.rb', line 19 def initialize( = {}) [:host] ||= '127.0.0.1' [:port] ||= 9160 @expires = [:expires] keyspace = ([:keyspace] ||= 'moneta') @cf = ([:column_family] || 'moneta').to_sym @client = ::Cassandra.new('system', "#{[:host]}:#{[:port]}") unless @client.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 @client.add_keyspace(ks_def) rescue Exception => ex puts "Cassandra: #{ex.}" end break if @client.keyspaces.include?(keyspace) sleep 0.1 end end @client.keyspace = keyspace end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
86 87 88 89 90 91 |
# File 'lib/moneta/adapters/cassandra.rb', line 86 def clear( = {}) @client.each_key(@cf) do |key| delete(key) end self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
78 79 80 81 82 83 |
# File 'lib/moneta/adapters/cassandra.rb', line 78 def delete(key, = {}) if value = load(key, ) @client.remove(@cf, key) value end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/moneta/adapters/cassandra.rb', line 48 def key?(key, = {}) if @client.exists?(@cf, key) if .include?(:expires) && (value = load(key)) store(key, value, ) end true else false end end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/moneta/adapters/cassandra.rb', line 60 def load(key, = {}) value = @client.get(@cf, key) if value if .include?(:expires) store(key, value['value'], ) else value['value'] end end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
72 73 74 75 |
# File 'lib/moneta/adapters/cassandra.rb', line 72 def store(key, value, = {}) @client.insert(@cf, key, {'value' => value}, :ttl => ([:expires] || @expires)) value end |