Class: Juno::Adapters::Cassandra
- Defined in:
- lib/juno/adapters/cassandra.rb
Overview
Cassandra backend
Instance Method Summary collapse
- #clear(options = {}) ⇒ Object
- #delete(key, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Cassandra
constructor
Constructor.
- #key?(key, options = {}) ⇒ Boolean
- #load(key, options = {}) ⇒ Object
- #store(key, value, options = {}) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ Cassandra
Constructor
Options:
-
:keyspace - Cassandra keyspace (default ‘juno’)
-
:column_family - Cassandra column family (default ‘juno’)
-
:host - Server host name (default 127.0.0.1)
-
:port - Server port (default 9160)
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/juno/adapters/cassandra.rb', line 20 def initialize( = {}) [:host] ||= '127.0.0.1' [:port] ||= 9160 keyspace = ([:keyspace] ||= 'juno') @cf = ([:column_family] || 'juno').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 = {}) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/juno/adapters/cassandra.rb', line 74 def clear( = {}) @client.each_key(@cf) do |key| delete(key) end self end |
#delete(key, options = {}) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/juno/adapters/cassandra.rb', line 62 def delete(key, = {}) if value = load(key, ) @client.remove(@cf, key) value end end |
#key?(key, options = {}) ⇒ Boolean
47 48 49 |
# File 'lib/juno/adapters/cassandra.rb', line 47 def key?(key, = {}) @client.exists?(@cf, key) end |
#load(key, options = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/juno/adapters/cassandra.rb', line 51 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
69 70 71 72 |
# File 'lib/juno/adapters/cassandra.rb', line 69 def store(key, value, = {}) @client.insert(@cf, key, {'value' => value}, :ttl => [:expires]) value end |