You are here

function dmemcache_object in Memcache API and Integration 7

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

Return a Memcache object for the specified bin.

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

string $bin: The bin which is to be used.

bool $reset: If TRUE will reset all static caches. Defaults to FALSE. To force a reconnection to memcached, first call $mc->quit() or $mc->close() (as appropriate for your PECL extension).

Return value

mixed A Memcache object, or FALSE on failure.

12 calls to dmemcache_object()
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
Flush all stored items.
dmemcache_get in ./dmemcache.inc
Retrieve a value from the cache.
dmemcache_get_multi in ./dmemcache.inc
Retrieve multiple values from the cache.

... See full list

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

File

./dmemcache.inc, line 965
A memcache API for Drupal.

Code

function dmemcache_object($bin = NULL, $reset = FALSE) {
  static $memcache_cache = array();
  static $memcache_servers = array();
  static $memcache_bins = array();
  static $failed_connections = array();
  if ($reset) {
    $memcache_cache = array();
    $memcache_servers = array();
    $memcache_bins = array();
    $failed_connections = array();
  }
  if (empty($memcache_cache) || empty($memcache_cache[$bin])) {
    if (empty($memcache_servers)) {

      // Load the variables from settings.php if set.
      $memcache_servers = variable_get('memcache_servers', array(
        '127.0.0.1:11211' => 'default',
      ));
      $memcache_bins = variable_get('memcache_bins', array(
        'cache' => 'default',
      ));
    }

    // If not manually set, default this cluster to 'default'.
    $cluster = empty($memcache_bins[$bin]) ? 'default' : $memcache_bins[$bin];

    // If not manually set, map this bin to 'cache' which maps 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($bin);

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

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

              // We've made at least one 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];
}