Module: Cassie::Statements::Statement::Preparation

Extended by:
ClassMethods
Included in:
Cassie::Statements::Statement
Defined in:
lib/cassie/statements/statement/preparation.rb,
lib/cassie/statements/statement/preparation/cache.rb

Overview

Sepcific functionality and DSL for prepared statements.

When included, a local, in-memory statement cache will be used when generating the statement object if prepared? is true.

By default, prepare defaults to true, since most statements should be prepared.

The cache key is the cql for the statment, or the statement itself if it does not respond to cql. For bound statments (recommended) this results in only cacheing once per unique statement type, independent of the values for a particular statemtn (which are in the params attribute, not the cql attribute).

The following class attributes are affected when included:

  • Sets prepared to true since most statements should be prepared.

Defined Under Namespace

Modules: ClassMethods Classes: Cache

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

prepare, prepare=, prepare?

Class Method Details

.cacheObject



39
40
41
# File 'lib/cassie/statements/statement/preparation/cache.rb', line 39

def self.cache
  @cache ||= init_cache
end

.init_cacheObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cassie/statements/statement/preparation/cache.rb', line 43

def self.init_cache
  previous_cache = defined?(@cache) ? @cache : nil

  # @todo research why memory story is blowing up when
  #     serializing the Cassandra prepared statement result
  # @cache = ActiveSupport::Cache::MemoryStore.new
  @cache = Cache.new

  previous_cache.close if previous_cache
  @cache
end

Instance Method Details

#statementCassandra::Statements::Prepared, Object

override. The statement object, fetched from perpared statements cache if Cassie::Statements::Statement::Preparation::ClassMethods#prepare? is true prepared statement if Cassie::Statements::Statement::Preparation::ClassMethods#prepare? is true, otherwise super

Returns:

  • (Cassandra::Statements::Prepared, Object)

    A bound,



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cassie/statements/statement/preparation.rb', line 55

def statement
  statement = super
  if self.class.prepare?
    key = statement.respond_to?(:cql) ? statement.cql : statement.to_s

    unbound = statement_cache.fetch(key) do
      statement.cql.freeze
      session.prepare(statement)
    end
    unbound.bind(statement.params)
  else
    statement
  end
end