class HousingMisc::AerospikeCacheExtension

Constants

READ_WRITE_BATCH_SIZE

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/housing_misc/aerospike_cache_extension.rb, line 6
def initialize(options = {})
  super
  aerospike_connection_policy = ::Aerospike::ClientPolicy.new(::JSON.parse(::Figaro.env.aerospike_connection_policy).with_indifferent_access)
  @conn_pool = ConnectionPool.new(size: 20) { ::Aerospike::Client.new(::JSON.parse(Housing.aerospike_hosts).collect {|host_port| host_port.join(':') }.join(','), policy: aerospike_connection_policy)}
end

Public Instance Methods

fetch(key, options=nil) { || ... } click to toggle source
# File lib/housing_misc/aerospike_cache_extension.rb, line 31
def fetch(key, options=nil)
  if block_given?
    if !options.nil? and options[:force] 
      result = yield
      write(key, result, options)
      return result
    end

    cached_object = read(key, options)
    return cached_object if cached_object != nil

    result = yield
    write(key, result, options)
    return result

  end
end
fetch_multi(*names) { |name| ... } click to toggle source
# File lib/housing_misc/aerospike_cache_extension.rb, line 49
def fetch_multi(*names)
  options = names.extract_options!
  options = merged_options(options)
  results = read_multi(*names, options)
  names.map do |name|
    results.fetch(name) do
      value = yield name
      write(name, value, options)
      results[name] = value
    end
  end
  results
end
read_multi(*names) click to toggle source
# File lib/housing_misc/aerospike_cache_extension.rb, line 13
def read_multi(*names)
  results = {}
  aerospike_cache_config = ::JSON.parse(::Figaro.env.aerospike_cache_config)
  key_list = names.map {|key| Aerospike::Key.new(aerospike_cache_config['namespace'], aerospike_cache_config['set'], key) }
  @conn_pool.with do |conn|
    unformatted_rslt = []
    key_list.each_slice(READ_WRITE_BATCH_SIZE) do |batch_names|
      unformatted_rslt += conn.batch_get(batch_names)
    end
    names.each_with_index do |name, i|
      if unformatted_rslt[i] != nil
        results[name] = decrypt_data(unformatted_rslt[i].bins['cache_entry']).value
      end 
    end
  end
  return results
end
write_multi(hash) click to toggle source
# File lib/housing_misc/aerospike_cache_extension.rb, line 63
def write_multi(hash)
  hash.each { |key, value| write(key, value.to_json) }
end

Protected Instance Methods

decrypt_data(data) click to toggle source
# File lib/housing_misc/aerospike_cache_extension.rb, line 91
def decrypt_data(data)
  begin
    return nil if data == nil
    return Marshal.load(Zlib::Inflate.inflate(data))
  rescue Zlib::DataError => e
    return nil
  end
end
read_entry(key, options) click to toggle source
# File lib/housing_misc/aerospike_cache_extension.rb, line 69
def read_entry(key, options)
  if value = internal_read_entry(key, options)
    # if it is not raw it is a marshalled ActiveSupport::Cache::Entry
    value = options[:raw]? ActiveSupport::Cache::Entry.new(value) : decrypt_data(value)
  else
    nil
  end
end
write_entry(key, entry, options) click to toggle source
# File lib/housing_misc/aerospike_cache_extension.rb, line 78
def write_entry(key, entry, options)
  options[:expiration] ||= options[:expires_in] if options[:expires_in]
  options[:record_exists_action] ||= options[:unless_exist]? Aerospike::RecordExistsAction::CREATE_ONLY : Aerospike::RecordExistsAction::REPLACE
  value = options[:raw]? entry.value : Marshal.dump(entry)
  begin
    @client.put(as_key(key, options), {options[:bin] => Zlib::Deflate.deflate(value)}, options)
  rescue Aerospike::Exceptions::Aerospike => e
    raise unless (e.result_code == Aerospike::ResultCode::KEY_EXISTS_ERROR)
    false
  end
end