Commit 2e750b98613d0f0a42b321919a495a3d34b72fec
1 parent
e5531222
Exists in
master
and in
19 other branches
Cache indexes on model [skip ci]
Showing
1 changed file
with
15 additions
and
6 deletions
Show diff stats
lib/searchkick/model.rb
... | ... | @@ -19,14 +19,22 @@ module Searchkick |
19 | 19 | raise ArgumentError, "Invalid value for callbacks" |
20 | 20 | end |
21 | 21 | |
22 | + index_name = | |
23 | + if options[:index_name] | |
24 | + options[:index_name] | |
25 | + elsif options[:index_prefix].respond_to?(:call) | |
26 | + -> { [options[:index_prefix].call, model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") } | |
27 | + else | |
28 | + [options.key?(:index_prefix) ? options[:index_prefix] : Searchkick.index_prefix, model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") | |
29 | + end | |
30 | + | |
22 | 31 | class_eval do |
23 | 32 | cattr_reader :searchkick_options, :searchkick_klass |
24 | 33 | |
25 | 34 | class_variable_set :@@searchkick_options, options.dup |
26 | 35 | class_variable_set :@@searchkick_klass, self |
27 | - class_variable_set :@@searchkick_index, options[:index_name] || | |
28 | - (options[:index_prefix].respond_to?(:call) && proc { [options[:index_prefix].call, model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") }) || | |
29 | - [options.key?(:index_prefix) ? options[:index_prefix] : Searchkick.index_prefix, model_name.plural, Searchkick.env, Searchkick.index_suffix].compact.join("_") | |
36 | + class_variable_set :@@searchkick_index, index_name | |
37 | + class_variable_set :@@searchkick_index_cache, {} | |
30 | 38 | |
31 | 39 | class << self |
32 | 40 | def searchkick_search(term = "*", **options, &block) |
... | ... | @@ -35,9 +43,10 @@ module Searchkick |
35 | 43 | alias_method Searchkick.search_method_name, :searchkick_search if Searchkick.search_method_name |
36 | 44 | |
37 | 45 | def searchkick_index |
38 | - index = class_variable_get :@@searchkick_index | |
39 | - index = index.call if index.respond_to? :call | |
40 | - Searchkick::Index.new(index, searchkick_options) | |
46 | + index = class_variable_get(:@@searchkick_index) | |
47 | + index = index.call if index.respond_to?(:call) | |
48 | + index_cache = class_variable_get(:@@searchkick_index_cache) | |
49 | + index_cache[name] ||= Searchkick::Index.new(index, searchkick_options) | |
41 | 50 | end |
42 | 51 | alias_method :search_index, :searchkick_index unless method_defined?(:search_index) |
43 | 52 | ... | ... |