Exemplo n.º 1
0
 /**
  * Clean up tag id lists since as keys expire the ids remain in the tag id lists
  */
 protected function _collectGarbage()
 {
     // Clean up expired keys from tag id set and global id set
     $exists = array();
     $tags = (array) $this->_redis->sMembers(self::SET_TAGS);
     foreach ($tags as $tag) {
         // Get list of expired ids for each tag
         $tagMembers = $this->_redis->sMembers(self::PREFIX_TAG_IDS . $tag);
         $numTagMembers = count($tagMembers);
         $expired = array();
         $numExpired = $numNotExpired = 0;
         if ($numTagMembers) {
             while ($id = array_pop($tagMembers)) {
                 if (!isset($exists[$id])) {
                     $exists[$id] = $this->_redis->exists(self::PREFIX_KEY . $id);
                 }
                 if ($exists[$id]) {
                     $numNotExpired++;
                 } else {
                     $numExpired++;
                     $expired[] = $id;
                     // Remove incrementally to reduce memory usage
                     if (count($expired) % 100 == 0 && $numNotExpired > 0) {
                         $this->_redis->sRem(self::PREFIX_TAG_IDS . $tag, $expired);
                         if ($this->_notMatchingTags) {
                             // Clean up expired ids from ids set
                             $this->_redis->sRem(self::SET_IDS, $expired);
                         }
                         $expired = array();
                     }
                 }
             }
             if (!count($expired)) {
                 continue;
             }
         }
         // Remove empty tags or completely expired tags
         if ($numExpired == $numTagMembers) {
             $this->_redis->del(self::PREFIX_TAG_IDS . $tag);
             $this->_redis->sRem(self::SET_TAGS, $tag);
         } else {
             if (count($expired)) {
                 $this->_redis->sRem(self::PREFIX_TAG_IDS . $tag, $expired);
                 if ($this->_notMatchingTags) {
                     // Clean up expired ids from ids set
                     $this->_redis->sRem(self::SET_IDS, $expired);
                 }
             }
         }
         unset($expired);
     }
     // Clean up global list of ids for ids with no tag
     if ($this->_notMatchingTags) {
         // TODO
     }
 }