The WordPress Object Cache is used to save on trips to the database. The Object Cache stores all of the cache data to memory and makes the cache contents available by using a key, which is used to name and later retrieve the cache contents. The Object Cache can be replaced by other caching mechanisms by placing files in the wp-content folder which is looked at in wp-settings. If that file exists, then this file will not be included.
Beispiel #1
0
function wp_cache_init()
{
    global $wp_object_cache, $sasl_memcached_config;
    $wp_object_cache = new WP_Object_Cache();
    if (isset($sasl_memcached_config) && is_array($sasl_memcached_config)) {
        $wp_object_cache->load_from_config($sasl_memcached_config);
    } else {
        $wp_object_cache->load_from_config(array('default' => array(array('host' => '127.0.0.1', 'port' => '11211', 'user' => null, 'pass' => null))));
    }
}
 function incr($id, $n, $group)
 {
     return parent::incr2($id, $n, $group);
 }
 public function test_redis_connect_custom_database()
 {
     global $redis_server;
     if (!class_exists('Redis')) {
         $this->markTestSkipped('PHPRedis extension not available.');
     }
     $redis_server['database'] = 2;
     $second_cache = new WP_Object_Cache();
     $this->cache->set('foo', 'bar');
     $this->assertEquals('bar', $this->cache->get('foo'));
     $this->assertFalse($second_cache->get('foo'));
     $second_cache->set('foo', 'apple');
     $this->assertEquals('apple', $second_cache->get('foo'));
     $this->assertEquals('bar', $this->cache->get('foo'));
 }
<?php

global $wp_object_cache;
$info = array();
$dropin = $this->validate_object_cache_dropin();
$info['Status'] = $this->get_status();
$info['Client'] = $this->get_redis_client_name();
$info['Drop-in'] = $dropin ? 'Valid' : 'Invalid';
if ($dropin) {
    try {
        $cache = new WP_Object_Cache(false);
        $info['Ping'] = $cache->redis_instance()->ping();
    } catch (Exception $exception) {
        $info['Connection Exception'] = sprintf('%s (%s)', $exception->getMessage(), get_class($exception));
    }
}
$info['Redis Extension'] = class_exists('Redis') ? phpversion('redis') : 'Not Found';
$info['Predis Client'] = class_exists('Predis\\Client') ? Predis\Client::VERSION : 'Not Found';
if (defined('PHP_VERSION')) {
    $info['PHP Version'] = PHP_VERSION;
}
if (defined('HHVM_VERSION')) {
    $info['HHVM Version'] = HHVM_VERSION;
}
$info['Multisite'] = is_multisite() ? 'Yes' : 'No';
if ($dropin) {
    $info['Global Prefix'] = json_encode($wp_object_cache->global_prefix);
    $info['Blog Prefix'] = json_encode($wp_object_cache->blog_prefix);
}
$constants = array('WP_REDIS_DISABLED', 'WP_REDIS_CLIENT', 'WP_REDIS_SCHEME', 'WP_REDIS_HOST', 'WP_REDIS_PORT', 'WP_REDIS_PATH', 'WP_REDIS_DATABASE', 'WP_REDIS_SERVERS', 'WP_REDIS_CLUSTER', 'WP_REDIS_MAXTTL', 'WP_REDIS_GLOBAL_GROUPS', 'WP_REDIS_IGNORED_GROUPS', 'WP_CACHE_KEY_SALT');
foreach ($constants as $constant) {
Beispiel #5
0
 public function test_redis_bad_authentication()
 {
     global $redis_server;
     if (!class_exists('Redis')) {
         $this->markTestSkipped('PHPRedis extension not available.');
     }
     $redis_server['host'] = '127.0.0.1';
     $redis_server['port'] = 9999;
     $redis_server['auth'] = 'foobar';
     $cache = new WP_Object_Cache();
     $this->assertEquals('WP Redis: Redis server went away', $cache->last_triggered_error);
     $this->assertFalse($cache->is_redis_connected);
     // Fails back to the internal object cache
     $cache->set('foo', 'bar');
     $this->assertEquals('bar', $cache->get('foo'));
 }
Beispiel #6
0
 /**
  * Get last modified value by key.
  *
  * @param string $key
  *
  * @return mixed
  */
 public function lastModified($key)
 {
     $value = $this->cache->get($key, 'bladerunner');
     return $value ? $value['modified'] : null;
 }
 function WP_Object_Cache()
 {
     global $memcached_servers;
     global $mcrouter_server;
     // get appropriate bucket
     if ($mcrouter_server && array_key_exists('host', $mcrouter_server) && array_key_exists('port', $mcrouter_server)) {
         // use mcrouter host and port if the global mcrouter is set
         $buckets = array("{$mcrouter_server['host']}:{$mcrouter_server['port']}");
         $this->mcrouter_prefix = '/rep/all/';
     } else {
         if ($mcrouter_server) {
             // we should be using mcrouter, but we can't
             error_log('[wpengine] Pod set to use mcrouter but host and port names are not specified in site config.json. Falling back to no replication.');
         }
         if (isset($memcached_servers)) {
             $buckets = $memcached_servers;
         } else {
             $buckets = array('unix:///tmp/memcached.sock');
         }
     }
     reset($buckets);
     if (is_int(key($buckets))) {
         $buckets = array('default' => $buckets);
     }
     foreach ($buckets as $bucket => $servers) {
         $this->mc[$bucket] = new Memcache();
         foreach ($servers as $server) {
             if (substr($server, 0, 5) == "unix:") {
                 $node = $server;
                 $port = 0;
             } else {
                 list($node, $port) = explode(':', $server);
                 if (!$port) {
                     $port = ini_get('memcache.default_port');
                 }
                 $port = intval($port);
                 if (!$port) {
                     $port = 11211;
                 }
             }
             $this->mc[$bucket]->addServer($node, $port, false, 1, 1, -1, true, array($this, 'failure_callback'));
             $this->mc[$bucket]->setCompressThreshold(20000, 0.2);
         }
     }
     global $blog_id, $table_prefix;
     $this->global_prefix = is_multisite() || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') ? '' : $table_prefix;
     $this->blog_prefix = (is_multisite() ? $blog_id : $table_prefix) . ':';
     // try to use the blog name but if we can't locate it, at least use something unique
     $customer = WP_Object_Cache::get_site_cache_key(__FILE__);
     $this->customer = $customer;
     // SO: blog prefix must come before any custom prefix
     $this->global_prefix = $this->global_prefix . ':' . $customer;
     $this->blog_prefix = $this->blog_prefix . ':' . $customer;
     $this->cache_hits =& $this->stats['get'];
     $this->cache_misses =& $this->stats['add'];
 }
Beispiel #8
0
 function &init_cache()
 {
     $cache = new WP_Object_Cache();
     $cache->add_global_groups(array('global-cache-test', 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache'));
     return $cache;
 }
/**
 * Invalidate a groups object cache
 *
 * @param mixed $groups A group or an array of groups to invalidate
 *
 * @return bool
 */
function wp_cache_flush_group($groups = 'default')
{
    return WP_Object_Cache::instance()->flush_groups($groups);
}
/**
 * Switch the internal blog id.
 *
 * This changes the blog id used to create keys in blog specific groups.
 *
 * @param int $blog_id Blog ID
 */
function wp_cache_switch_to_blog($blog_id)
{
    WP_Object_Cache::instance()->switch_to_blog($blog_id);
}
Beispiel #11
0
 function rm($fileglob)
 {
     if (is_file($fileglob)) {
         return @unlink($fileglob);
     } else {
         if (is_dir($fileglob)) {
             $ok = WP_Object_Cache::rm("{$fileglob}/*");
             if (!$ok) {
                 return false;
             }
             return @rmdir($fileglob);
         } else {
             $matching = glob($fileglob);
             if ($matching === false) {
                 return true;
             }
             $rcs = array_map(array('WP_Object_Cache', 'rm'), $matching);
             if (in_array(false, $rcs)) {
                 return false;
             }
         }
     }
     return true;
 }