Class: Moneta::Adapters::Cassandra
- Inherits:
-
Base
- Object
- Base
- Moneta::Adapters::Cassandra
show all
- Defined in:
- lib/moneta/adapters/cassandra.rb
Overview
Instance Method Summary
collapse
Methods inherited from Base
#[], #[]=, #close, #decrement, #fetch, #increment
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ 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(options = {})
options[:host] ||= '127.0.0.1'
options[:port] ||= 9160
@expires = options[:expires]
keyspace = (options[:keyspace] ||= 'moneta')
@cf = (options[:column_family] || 'moneta').to_sym
@client = ::Cassandra.new('system', "#{options[:host]}:#{options[: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])
10.times do
begin
@client.add_keyspace(ks_def)
rescue Exception => ex
puts "Cassandra: #{ex.message}"
end
break if @client.keyspaces.include?(keyspace)
sleep 0.1
end
end
@client.keyspace = keyspace
end
|
Instance Method Details
#clear(options = {}) ⇒ Object
81
82
83
84
85
86
|
# File 'lib/moneta/adapters/cassandra.rb', line 81
def clear(options = {})
@client.each_key(@cf) do |key|
delete(key)
end
self
end
|
#delete(key, options = {}) ⇒ Object
74
75
76
77
78
79
|
# File 'lib/moneta/adapters/cassandra.rb', line 74
def delete(key, options = {})
if value = load(key, options)
@client.remove(@cf, key)
value
end
end
|
#key?(key, options = {}) ⇒ Boolean
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/moneta/adapters/cassandra.rb', line 47
def key?(key, options = {})
if @client.exists?(@cf, key)
if options.include?(:expires) && (value = load(key))
store(key, value, options)
end
true
else
false
end
end
|
#load(key, options = {}) ⇒ Object
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/moneta/adapters/cassandra.rb', line 58
def load(key, options = {})
value = @client.get(@cf, key)
if value
if options.include?(:expires)
store(key, value['value'], options)
else
value['value']
end
end
end
|
#store(key, value, options = {}) ⇒ Object
69
70
71
72
|
# File 'lib/moneta/adapters/cassandra.rb', line 69
def store(key, value, options = {})
@client.insert(@cf, key, {'value' => value}, :ttl => (options[:expires] || @expires))
value
end
|