You are here

function dmemcache_delete in Memcache API and Integration 6

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

Deletes an item from the cache.

Parameters

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

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

Return value

Returns TRUE on success or FALSE on failure.

9 calls to dmemcache_delete()
cache_clear_all in ./memcache.db.inc
Expire data from the cache. If called without arguments, expirable entries will be cleared from the cache_page table.
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.
cache_get in ./memcache.inc
Return data from the persistent cache.
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.

... See full list

File

./dmemcache.inc, line 419

Code

function dmemcache_delete($key, $bin = 'cache') {
  $collect_stats = dmemcache_stats_init();
  $full_key = dmemcache_key($key, $bin);
  $rc = FALSE;
  if ($mc = dmemcache_object($bin)) {
    $rc = $mc
      ->delete($full_key, 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($full_key)) {

        // 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($full_key, NULL);
        $next_id = 0;
        do {

          // 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 ($mc
          ->delete($full_key, 0));

        // 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 =& dmemcache_static('dmemcache_piece_cache', array());
        foreach ($piece_cache as $cid => $expires) {
          if ($_SERVER['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(
      $full_key => $rc,
    ));
  }
  return $rc;
}