module Cassie::Statements::Statement::Preparation

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:

Public Class Methods

cache() click to toggle source
# File lib/cassie/statements/statement/preparation/cache.rb, line 39
def self.cache
  @cache ||= init_cache
end
included(base) click to toggle source

@!visibility private

# File lib/cassie/statements/statement/preparation.rb, line 20
def self.included(base)
  base.extend ClassMethods
  base.instance_eval do
    self.prepare = true
  end
end
init_cache() click to toggle source
# 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

Public Instance Methods

statement() click to toggle source

override. The statement object, fetched from perpared statements cache if {#prepare?} is true @return [Cassandra::Statements::Prepared, Object] A bound, prepared statement if {#prepare?} is true, otherwise super

Calls superclass method
# 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

Protected Instance Methods

statement_cache() click to toggle source
# File lib/cassie/statements/statement/preparation.rb, line 72
def statement_cache
  Cassie::Statements::Statement::Preparation.cache
end