diff --git a/memcache.inc b/memcache.inc
index 9fc1126..ba60f9b 100644
--- a/memcache.inc
+++ b/memcache.inc
@@ -234,6 +234,7 @@ function memcache_wildcard_flushes($cid, $table) {
 function memcache_wildcards($cid, $table, $flush = FALSE) {
   static $wildcards = array();
   $matching = array();
+  $length = strlen($cid);
 
   $wildcard_flushes = variable_get('memcache_wildcard_flushes', array());
   $wildcard_invalidate = variable_get('memcache_wildcard_invalidate', MEMCACHE_WILDCARD_INVALIDATE);
@@ -246,9 +247,9 @@ function memcache_wildcards($cid, $table, $flush = FALSE) {
     // Find statically cached wildcards, and determine possibly matching
     // wildcards for this cid based on a history of the lengths of past valid
     // wildcard flushes in this bin.
-    foreach ($wildcard_flushes[$table] as $length => $timestamp) {
-      if ($timestamp >= ($_SERVER['REQUEST_TIME'] - $wildcard_invalidate)) {
-        $key = '.wildcard-' . substr($cid, 0, $length);
+    foreach ($wildcard_flushes[$table] as $flush_length => $timestamp) {
+      if ($flush_length >= $length && $timestamp >= ($_SERVER['REQUEST_TIME'] - $wildcard_invalidate)) {
+        $key = '.wildcard-' . substr($cid, 0, $flush_length);
         $wildcard = dmemcache_key($key, $table);
         if (isset($wildcards[$table][$wildcard])) {
           $matching[$wildcard] = $wildcards[$table][$wildcard];
