Вы здесь

Asynchronous MySQL driver

Асинхронные запросы через DBTNG.

Модуль Asynchronous MySQL driver разрешает выполнение асинхронных запросов через DBTNG.

Установка

  • Скопируйте (или сделайте ссылку-синоним) подпапку в папку драйвера базы данных, чтобы Друпал мог её найти и использовать
  • %> cd /path/to/drupal/path/to/module
  • %> cp -r database /path/to/drupal/includes/database/mysql_async
  • Очистите кеш

Настройка

Измените драйвер в файле settings.php:

  $databases['default']['default']['driver'] = 'mysql_async';
  $databases['default']['default']['async_max_connections'] = 10;
  $databases['default']['default']['async_min_connections'] = 2;

  // Use async queries inside transactions.
  $databases['default']['default']['async_transaction'] = TRUE;

  // Use async queries for all request. Experimental.
  $databases['default']['default']['default_options'] = array('async' => TRUE);

  // Use a connection timeout of 4 seconds. By default, the timeout specified
  // by $databases['default']['default']['pdo'][PDO::ATTR_TIMEOUT] will be
  // used.
  $databases['default']['default']['mysqli'] = array(PDO::ATTR_TIMEOUT => 4);

Значения по умолчанию для mysql_async:

  array(
    'async_max_connections' => 30,
    'async_max_connections' => 5,
    'default_options' => array(
      'async' => FALSE,
      'async_transaction' => FALSE,
    ),
    'mysqli' => array(),
);

Использование

Выполнение двух тяжёлых запросов асинхронно:

// Initiate queries.
$q1 = db_query("MY HEAVY QUERY ONE", array(), array('async' => TRUE));
$q2 = db_query("MY HEAVY QUERY TWO", array(), array('async' => TRUE));

// Wait no more than 30 seconds for queries to complete.
$until = microtime(TRUE) + 30;
do {
   $done = $q1->rowCount(TRUE) !== NULL && $q2->rowCount(TRUE) !== NULL;
} while (!$done && microtime(TRUE) < $until);

// Get results.
var_dump($q1->fetchAll());
var_dump($q2->fetchAll());

Драйвер также работает с бэк-эндом кеша:

// Add async support to the default database driver.
$databases['default']['default']['driver'] = 'mysql_async';

// Add cache backends. Note that Cache Consistent is necessary due to cache
// operations are not performed in the default database connection connection,
// and is thus potentially outside a transaction.
$conf['cache_backends'][] = 'sites/all/modules/contrib/cache_heuristic/cache_heuristic.inc';
$conf['cache_backends'][] = 'sites/all/modules/contrib/cache_consistent/cache_consistent.inc';
$conf['cache_backends'][] = 'sites/all/modules/contrib/mysql_async/mysql_async.cache.inc';

// Setup backends.
$conf['cache_default_class'] = 'HeuristicCache';
$conf['heuristic_cache_default_class'] = 'ConsistentCache';
$conf['consistent_cache_default_class'] = 'DrupalDatabaseCacheAsync';

Группа проекта: 
Теги: