Class: Moneta::Adapters::Cassandra

Inherits:
Object
  • Object
show all
Includes:
Defaults, ExpiresSupport
Defined in:
lib/moneta/adapters/cassandra.rb

Overview

Cassandra backend

Author:

  • Potapov Sergey (aka Blake)

Instance Attribute Summary collapse

Attributes included from ExpiresSupport

#default_expires

Instance Method Summary collapse

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.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :keyspace (String) — default: 'moneta'

    Cassandra keyspace

  • :column_family (String) — default: 'moneta'

    Cassandra column family

  • :host (String) — default: '127.0.0.1'

    Server host name

  • :port (Integer) — default: 9160

    Server port

  • :expires (Integer)

    Default expiration time

  • :backend (::Cassandra)

    Use existing backend instance

  • Other (Object)

    options passed to ‘Cassandra#new`



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
50
51
52
53
54
55
# File 'lib/moneta/adapters/cassandra.rb', line 22

def initialize(options = {})
  self.default_expires = options.delete(:expires)
  @cf = (options.delete(:column_family) || 'moneta').to_sym
  if options[:backend]
    @backend = options[:backend]
  else
    keyspace = options.delete(:keyspace) || 'moneta'
    options[:retries] ||= 3
    options[:connect_timeout] ||= 10
    options[:timeout] ||= 10
    @backend = ::Cassandra.new('system',
                               "#{options[:host] || '127.0.0.1'}:#{options[:port] || 9160}",
                               options)
    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.message}"
        end
        break if @backend.keyspaces.include?(keyspace)
        sleep 0.1
      end
    end
    @backend.keyspace = keyspace
  end
end

Instance Attribute Details

#backendObject (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

Parameters:

  • options (Hash) (defaults to: {})


91
92
93
94
# File 'lib/moneta/adapters/cassandra.rb', line 91

def clear(options = {})
  @backend.clear_column_family!(@cf)
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil



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

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



83
84
85
86
87
88
# File 'lib/moneta/adapters/cassandra.rb', line 83

def delete(key, options = {})
  if value = load(key, options)
    @backend.remove(@cf, key)
    value
  end
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
# File 'lib/moneta/adapters/cassandra.rb', line 58

def key?(key, options = {})
  if @backend.exists?(@cf, key)
    load(key, options) if options.include?(:expires)
    true
  else
    false
  end
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



68
69
70
71
72
73
74
# File 'lib/moneta/adapters/cassandra.rb', line 68

def load(key, options = {})
  if value = @backend.get(@cf, key)
    expires = expires_value(options, 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

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



77
78
79
80
# File 'lib/moneta/adapters/cassandra.rb', line 77

def store(key, value, options = {})
  @backend.insert(@cf, key, {'value' => value}, ttl: expires_value(options) || nil)
  value
end