You are here

function dmemcache_object in Memcache API and Integration 5

Same name and namespace in other branches
  1. 5.2 dmemcache.inc \dmemcache_object()
  2. 6 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.

5 calls to dmemcache_object()
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.
dmemcache_set in ./dmemcache.inc
Place an item into memcache
dmemcache_stats in ./dmemcache.inc

File

./dmemcache.inc, line 143

Code

function dmemcache_object($bin = NULL, $flush = FALSE) {
  static $memcacheCache = array(), $memcache_servers, $memcache_bins;
  if ($flush) {
    foreach ($memcacheCache as $cluster) {
      $cluster
        ->close();
    }
    $memcacheCache = array();
  }
  if (empty($memcacheCache) || empty($memcacheCache[$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($memcacheCache['cache'])) {
      $memcacheCache[$bin] =& $memcacheCache['cache'];
    }
    else {

      // Create a new Memcache object. Each cluster gets its own Memcache object.
      $memcache = new Memcache();

      // A variable to track whether we've connected to the first server.
      $init = FALSE;

      // Link all the servers to this cluster.
      foreach ($memcache_servers as $s => $c) {
        if ($c == $cluster) {
          list($host, $port) = explode(':', $s);

          // This is a server that belongs to this cluster.
          if (!$init) {

            // First server gets the connect.
            if (@$memcache
              ->addServer($host, $port)) {

              // Only set init to true if a connection was made.
              $init = TRUE;
            }
          }
          else {

            // Subsequent servers gett addServer.
            @$memcache
              ->addServer($host, $port);
          }
        }
      }
      if ($init) {

        // Map the current bin with the new Memcache object.
        $memcacheCache[$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.
            $memcacheCache[$b] =& $memcacheCache[$bin];
          }
        }
      }
    }
  }
  return empty($memcacheCache[$bin]) ? FALSE : $memcacheCache[$bin];
}