You are here

class DrupalMemcachedUtils in Memcache Storage 8

Hierarchy

Expanded class hierarchy of DrupalMemcachedUtils

2 files declare their use of DrupalMemcachedUtils
MemcachedStatistics.php in src/Controller/MemcachedStatistics.php
Contains form for output of memcached usage statistics.
memcache_storage.install in ./memcache_storage.install
Provides requirements check for integration with memcached.

File

src/DrupalMemcachedUtils.php, line 7

Namespace

Drupal\memcache_storage
View source
class DrupalMemcachedUtils {

  /**
   * Parses the string of memcached server.
   *
   * @param $memcached_server string
   *   Examples of server string:
   *   - unix:///path/to/memcached.socket
   *   - 127.0.0.1:12111
   *
   * @return array
   *   Processed host and port values.
   */
  public static function parseServerInfo($memcached_server) {
    list($host, $port) = explode(':', $memcached_server);

    // Support unix sockets in the format 'unix:///path/to/socket'.
    if ($host == 'unix') {

      // PECL Memcache requires string format like 'unix:///path/to/socket' to
      // establish a connection, while PECL Memcached requires only
      // '/path/to/socket' string for the same purpose.
      $pecl_extension = self::getPeclExtension();
      $host = $pecl_extension == 'Memcached' ? substr($memcached_server, 7) : $memcached_server;

      // For unix sockets port is always 0.
      $port = 0;
    }
    return [
      $host,
      $port,
    ];
  }

  /**
   * Returns the name of PECL extension to use.
   */
  public static function getPeclExtension() {
    $settings = Settings::get('memcache_storage');
    if (!empty($settings['extension'])) {
      $extension = $settings['extension'];
    }
    elseif (class_exists('Memcached')) {
      $extension = 'Memcached';
    }
    elseif (class_exists('Memcache')) {
      $extension = 'Memcache';
    }
    return !empty($extension) ? $extension : FALSE;
  }

  /**
   * Register shutdown callable to log a message.
   * We need to do this on shutdown because in the initial phase
   * loggers might be not available.
   *
   * @param $level
   *   \Psr\Log\LogLevel constant
   *
   * @param $message
   *   A message which should be logged.
   *
   * @param $context
   *   An array with additional information about the event - uid, link, etc.
   */
  public static function log($level, $message, array $context = []) {
    \register_shutdown_function(array(
      'Drupal\\memcache_storage\\DrupalMemcachedUtils',
      'logShutdown',
    ), $level, $message, $context);
  }

  /**
   * Logs a message on the shutdown.
   *
   * @param $level
   *   \Psr\Log\LogLevel constant
   *
   * @param $message
   *   A message which should be logged.
   *
   * @param $context
   *   An array with additional information about the event - uid, link, etc.
   */
  public static function logShutdown($level, $message, array $context = []) {
    \Drupal::logger('memcache_storage')
      ->log($level, $message, $context);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalMemcachedUtils::getPeclExtension public static function Returns the name of PECL extension to use.
DrupalMemcachedUtils::log public static function Register shutdown callable to log a message. We need to do this on shutdown because in the initial phase loggers might be not available.
DrupalMemcachedUtils::logShutdown public static function Logs a message on the shutdown.
DrupalMemcachedUtils::parseServerInfo public static function Parses the string of memcached server.