Class: Workarea::QueryCacheMiddleware

Inherits:
Object
  • Object
show all
Defined in:
app/middleware/workarea/query_cache_middleware.rb

Overview

Execute sidekiq jobs with query caches enabled. Each job can specify one or both query_caches to use via ‘query_cache`, `mongoid_query_cache`, or `elasticsearch_query_cache` options.

Returns:

  • (Object)

    The result of the call.

Defined Under Namespace

Classes: CacheOptions

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ QueryCacheMiddleware

Returns a new instance of QueryCacheMiddleware.



9
10
11
# File 'app/middleware/workarea/query_cache_middleware.rb', line 9

def initialize(options = {})
  @options = options
end

Instance Method Details

#call(worker, msg, queue) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/middleware/workarea/query_cache_middleware.rb', line 13

def call(worker, msg, queue)
  cache_options = CacheOptions.new(worker.class.sidekiq_options)
  return yield if cache_options.none?

  if cache_options.all?
    Mongoid::QueryCache.cache do
      Elasticsearch::QueryCache.cache do
        yield
      end
    end
  elsif cache_options.mongoid?
    Mongoid::QueryCache.cache { yield }
  elsif cache_options.elasticsearch?
    Elasticsearch::QueryCache.cache { yield }
  end
ensure
  Mongoid::QueryCache.clear_cache
  Elasticsearch::QueryCache.clear_cache
end