function clear_wp_redis_cache() { include_once "predis5.2.php"; //we need this to use Redis inside of PHP $args = array('post_type' => 'any', 'posts_per_page' => -1); $wp_query = new WP_Query($args); // to get all Posts $redis = new Predis_Client(); // Loop all posts and clear the cache $i = 0; while ($wp_query->have_posts()) { $wp_query->the_post(); $incremented = false; $permalink = get_permalink(); $redis_key = md5($permalink); $redis_ssl_key = 'ssl_' . $redis_key; if ($redis->exists($redis_key) == true) { $redis->del($redis_key); $i++; $incremented = true; } if ($redis->exists($redis_ssl_key) == true) { $redis->del($redis_ssl_key); if (!$incremented) { $i++; } } } echo $i . " of " . $wp_query->found_posts . " posts was cleared in cache"; die; }
/** * 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 } }
die("Error occurred. Error message: " . $e->getMessage()); } } //Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment if (refreshHasSecret($secret_string) || requestHasSecret($secret_string) || isRemotePageLoad($current_url, $websiteIp)) { if ($debug) { echo "<!-- manual refresh was required -->\n"; } $redis->del($redis_key); $redis->del("ssl_" . $redis_key); require $wp_blog_header_path; //$unlimited = get_option('wp-redis-cache-debug',false); $seconds_cache_redis = get_option('wp-redis-cache-seconds', 86400); // This page is cached, lets display it } else { if ($redis->exists($redis_key)) { if ($debug) { echo "<!-- serving page from cache: key: {$redis_key} -->\n"; } $cache = true; $html_of_page = $redis->get($redis_key); if ($compress) { $html_of_page = gzuncompress($html_of_page); } echo $html_of_page; // If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page } else { if ($_SERVER['REMOTE_ADDR'] != $websiteIp && strstr($current_url, 'preview=true') == false) { if ($debug) { echo "<!-- displaying page without cache -->\n"; }
/** * @see sfCache */ public function has($key) { return $this->redis->exists($this->getKey($key)); }