You are here

function dmemcache_object in Memcache API and Integration 6

Same name and namespace in other branches
  1. 5.2 dmemcache.inc \dmemcache_object()
  2. 5 dmemcache.inc \dmemcache_object()
  3. 7 dmemcache.inc \dmemcache_object()

Returns an Memcache object based on the bin requested. Note that there is nothing preventing developers from calling this function directly to get the Memcache object. Do this if you need functionality not provided by this API or if you need to use legacy code. Otherwise, use the dmemcache (get, set, delete, flush) API functions provided here.

Parameters

$bin The bin which is to be used.:

$flush Rebuild the bin/server/cache mapping.:

Return value

an Memcache object or FALSE.

12 calls to dmemcache_object()
cache_clear_all in ./memcache.inc
Expire data from the cache. If called without arguments, expirable entries will be cleared from the cache_page and cache_block tables.
dmemcache_add in ./dmemcache.inc
Add an item into memcache
dmemcache_delete in ./dmemcache.inc
Deletes an item from the cache.
dmemcache_flush in ./dmemcache.inc
Immediately invalidates all existing items. dmemcache_flush doesn't actually free any resources, it only marks all the items as expired, so occupied memory will be overwritten by new items.
dmemcache_get in ./dmemcache.inc
Retrieve a value from the cache.

... See full list

1 string reference to 'dmemcache_object'
memcache_enable in ./memcache.install
Implements hook_enable().

File

./dmemcache.inc, line 679

Code

function dmemcache_object($bin = NULL, $flush = FALSE) {
  static $memcache_cache = array();
  static $memcache_servers = array();
  static $memcache_bins = array();
  static $failed_connections = array();
  if ($flush) {
    foreach ($memcache_cache as $cluster) {
      memcache_close($cluster);
    }
    $memcache_cache = array();
  }
  $extension = dmemcache_extension();
  if (empty($memcache_cache) || empty($memcache_cache[$bin])) {

    // $memcache_servers and $memcache_bins originate from settings.php.
    // $memcache_servers_custom and $memcache_bins_custom get set by
    // memcache.module. They are then merged into $memcache_servers and
    // $memcache_bins, which are statically cached for performance.
    if (empty($memcache_servers)) {

      // Values from settings.php
      $memcache_servers = variable_get('memcache_servers', array(
        '127.0.0.1:11211' => 'default',
      ));
      $memcache_bins = variable_get('memcache_bins', array(
        'cache' => 'default',
      ));
    }

    // If there is no cluster for this bin in $memcache_bins, cluster is 'default'.
    $cluster = empty($memcache_bins[$bin]) ? 'default' : $memcache_bins[$bin];

    // If this bin isn't in our $memcache_bins configuration array, and the
    // 'default' cluster is already initialized, map the bin to 'cache' because
    // we always map the 'cache' bin to the 'default' cluster.
    if (empty($memcache_bins[$bin]) && !empty($memcache_cache['cache'])) {
      $memcache_cache[$bin] =& $memcache_cache['cache'];
    }
    else {

      // Create a new memcache object for each cluster.
      $memcache = dmemcache_instance();

      // Track whether or not we've opened any memcache connections.
      $connection = FALSE;

      // Link all the servers to this cluster.
      foreach ($memcache_servers as $server => $c) {
        if ($c == $cluster && !isset($failed_connections[$server])) {
          $rc = dmemcache_connect($memcache, $server, $connection);
          if ($rc !== FALSE) {

            // We've made at least one successful connection.
            $connection = TRUE;
          }
          else {

            // Memcache connection failure. We can't log to watchdog directly
            // because we're in an early Drupal bootstrap phase where watchdog
            // is non-functional. Instead, register a shutdown handler so it
            // gets recorded at the end of the page load.
            register_shutdown_function('watchdog', 'memcache', 'Failed to connect to memcache server: !server', array(
              '!server' => $server,
            ), WATCHDOG_ERROR);
            $failed_connections[$server] = FALSE;
          }
        }
      }
      if ($connection) {

        // Map the current bin with the new Memcache object.
        $memcache_cache[$bin] = $memcache;

        // Now that all the servers have been mapped to this cluster, look for
        // other bins that belong to the cluster and map them too.
        foreach ($memcache_bins as $b => $c) {
          if ($c == $cluster && $b != $bin) {

            // Map this bin and cluster by reference.
            $memcache_cache[$b] =& $memcache_cache[$bin];
          }
        }
      }
    }
  }
  return empty($memcache_cache[$bin]) ? FALSE : $memcache_cache[$bin];
}