@@ -2419,5 +2419,72 @@ will query the database and separately cache their results.
2419
2419
To use the cached results, call ``all.to_a.first`` on the model class.
2420
2420
2421
2421
2422
- .. _legacy-query-cache-limitations :
2422
+ .. _load-async :
2423
2423
2424
+ Asynchronous Queries
2425
+ ====================
2426
+
2427
+ Mongoid allows running database queries asynchronously in the background.
2428
+ This can be beneficial when there is a need to get documents from different
2429
+ collections.
2430
+
2431
+ In order to schedule an asynchronous query call the ``load_async`` method on a
2432
+ ``Criteria``:
2433
+
2434
+ .. code-block:: ruby
2435
+
2436
+ class PagesController < ApplicationController
2437
+ def index
2438
+ @active_bands = Band.where(active: true).load_async
2439
+ @best_events = Event.best.load_async
2440
+ @public_articles = Article.where(public: true).load_async
2441
+ end
2442
+ end
2443
+
2444
+ In the above example three queries will be scheduled for asynchronous execution.
2445
+ Results of the queries can be later accessed as usual:
2446
+
2447
+ .. code-block:: html
2448
+
2449
+ <ul>
2450
+ <%- @active_bands.each do -%>
2451
+ <li><%= band.name %></li>
2452
+ <%- end -%>
2453
+ </ul>
2454
+
2455
+ Even if a query is scheduled for asynchronous execution, it might be executed
2456
+ synchronously on the caller's thread. There are three possible scenarios depending
2457
+ on when the query results are being accessed:
2458
+
2459
+ #. If the scheduled asynchronous task has been already executed, the results are returned.
2460
+ #. If the task has been started, but not finished yet, the caller's thread blocks until the task is finished.
2461
+ #. If the task has not been started yet, it is removed from the execution queue, and the query is executed synchronously on the caller's thread.
2462
+
2463
+ .. note::
2464
+
2465
+ Even though ``load_async`` method returns a ``Criteria`` object, you should not
2466
+ do any operations on this object except accessing query results. The query is
2467
+ scheduled for execution immediately after calling ``load_async``, therefore
2468
+ later changes to the `Criteria`` object may not be applied.
2469
+
2470
+
2471
+ Configuring asynchronous query execution
2472
+ ----------------------------------------
2473
+
2474
+ Asynchronous queries are disabled by default. When asynchronous queries are
2475
+ disabled, ``load_async`` will execute the query immediately on the current thread,
2476
+ blocking as necessary. Therefore, calling ``load_async`` on criteria in this case
2477
+ is roughly the equivalent of calling ``to_a`` to force query execution.
2478
+
2479
+ In order to enable asynchronous query execution, the following config options
2480
+ must be set:
2481
+
2482
+ .. code-block:: yaml
2483
+
2484
+ development:
2485
+ ...
2486
+ options:
2487
+ # Execute asynchronous queries using a global thread pool.
2488
+ async_query_executor: :global_thread_pool
2489
+ # Number of threads in the pool. The default is 4.
2490
+ # global_executor_concurrency: 4
0 commit comments