You are here

function dmemcache_delete in Memcache API and Integration 7

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

Deletes an item from the cache.

Parameters

string $key: The key with which the item was stored.

string $bin: The bin in which the item was stored.

Return value

bool Returns TRUE on success or FALSE on failure.

5 calls to dmemcache_delete()
lock_release in ./memcache-lock-code.inc
Release a lock previously acquired by lock_acquire().
lock_release_all in ./memcache-lock-code.inc
Release all locks acquired by this request.
MemCacheClearCase::clearWildcardPrefixTest in tests/memcache.test
Test cache clears using wildcard prefixes.
MemCacheClearCase::clearWildcardPrefixTest in tests/memcache6.test
Test cache clears using wildcard prefixes.
MemCacheDrupal::clear in ./memcache.inc
Implements DrupalCacheInterface::clear().

File

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

Code

function dmemcache_delete($key, $bin = 'cache', $mc = NULL) {
  $collect_stats = dmemcache_stats_init();
  $full_keys = dmemcache_key($key, $bin, TRUE);
  $rc = FALSE;
  if ($mc || ($mc = dmemcache_object($bin))) {
    foreach ($full_keys as $fk) {
      $rc = $mc
        ->delete($fk, 0);
      if (empty($rc) && $mc instanceof Memcached && _dmemcache_ensure_ascii_auth($fk, $mc)) {
        $rc = $mc
          ->delete($fk, 0);
      }
      if ($rc) {

        // If the delete succeeded, we now check to see if this item has
        //  multiple pieces also needing to be cleaned up. If the delete failed,
        // we assume these keys have already expired or been deleted (memcache
        // will auto-expire eventually if we're wrong).
        if ($piece_cache = dmemcache_piece_cache_get($fk)) {

          // First, remove from the piece_cache so we don't try and delete it
          // again in another thread, then delete the actual cache data pieces.
          dmemcache_piece_cache_set($fk, NULL);
          $next_id = 0;
          do {
            if ($inner_rc) {
              _dmemcache_write_debug("delete", $bin, $full_key, $inner_rc);
            }

            // Generate the cid of the next data piece.
            $piece_key = _dmemcache_key_piece($key, $next_id);
            $full_key = dmemcache_key($piece_key, $bin);
            $next_id++;

            // Keep deleting pieces until the operation fails. We accept that
            // this could lead to orphaned pieces as memcache will auto-expire
            // them eventually.
          } while ($inner_rc = $mc
            ->delete($full_key, 0));
          _dmemcache_write_debug("delete", $bin, $full_key, $inner_rc);

          // Perform garbage collection for keys memcache has auto-expired. If
          // we don't do this, this variable could grow over enough time as a
          // slow memory leak.
          // @todo: Consider moving this to hook_cron() and requiring people to
          // enable the memcache module.
          timer_start('memcache_gc_piece_cache');
          $gc_counter = 0;
          $piece_cache =& drupal_static('dmemcache_piece_cache', array());
          foreach ($piece_cache as $cid => $expires) {
            if (REQUEST_TIME > $expires) {
              $gc_counter++;
              dmemcache_piece_cache_set($cid, NULL);
            }
          }
          if ($gc_counter) {
            register_shutdown_function('watchdog', 'memcache', 'Spent !time ms in garbage collection cleaning !count stale keys from the dmemcache_piece_cache.', array(
              '!time' => timer_read('memcache_gc_piece_cache'),
              '!count' => $gc_counter,
            ), WATCHDOG_WARNING);
          }
        }
      }
      if ($collect_stats) {
        dmemcache_stats_write('delete', $bin, array(
          $fk => $rc,
        ));
      }
      _dmemcache_write_debug('delete', $bin, $fk, $rc);
    }
  }
  return $rc;
}