Class: HousingMisc::AerospikeCacheExtension

Inherits:
ActiveSupport::Cache::AerospikeStore
  • Object
show all
Defined in:
lib/housing_misc/aerospike_cache_extension.rb

Constant Summary collapse

READ_WRITE_BATCH_SIZE =
10

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AerospikeCacheExtension

Returns a new instance of AerospikeCacheExtension.



6
7
8
9
10
# 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

Instance Method Details

#fetch(key, options = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



63
64
65
# File 'lib/housing_misc/aerospike_cache_extension.rb', line 63

def write_multi(hash)
  hash.each { |key, value| write(key, value.to_json) }
end