Zack Tollmanz为WordPress编写了一个新的Memcached对象缓存库。这个库是基于Ryan Boren开发的WordPress Memcache插件。
WordPress带有默认的对象缓存。可以通过将名为object-Cache.php文件的文件复制到wp内容文件夹的根目录中来替换此默认缓存。object-cache.php文件将包含替换对象缓存的实现。
Tollmanz Memcached对象缓存库不是插件。它是object-cache.php文件的替换版本。以下是Tollmanz的源代码和安装说明。问题是Tollmanz库假设PECL Memcached库安装在开发环境中,并且Memcached Server实例存在。我们的项目有多名开发人员,需要能够在未安装Memcached的本地开发环境(如笔记本电脑(中工作。Memcache服务器和PECL库当然安装在我们的集成和生产环境中。
问题是如何设置Tollmanz Memcached对象缓存库,使其可以在没有安装Memcached的本地环境中使用?
好问题John。答案是创建一个"stub"object-cache.php文件,并将其放入wp内容文件夹的根目录中。这个"存根"文件可以检查环境常数,以确定是否加载Tollmanz Memcached对象缓存库。如果TollmanzMemcached缓存没有加载,WordPress将恢复到其默认的对象缓存。
总结:
- 遵循Tollmanz安装说明
- 将Tollmanz object-cache.php文件复制到一个新的"plugins"文件夹中。这示例使用了一个名为/plugins/pecl-memcached对象缓存的文件夹/
- 将下面的"stub"object-cache.php文件复制到/wp内容文件夹中。WordPress将在引导序列的对象缓存设置部分加载此文件
- 如果在wp-confile.php文件中定义了名为MEMCACHED_IS_ENABLED的常量,则"存根"将加载Tollmanz对象缓存。否则,它什么也不做,并使用默认的WordPress对象缓存
- 下面列出了"stub"object-cache.php文件的源代码
<?php
//
// WordPress PECL Memcached Object Cache Stub File
//
// Name this file "object-cache.php" and place in the root of the /wp-content folder.
//
// This "stub" file integrates WordPress with the Tollmanz PECL Memcached Object Cache
// https://github.com/tollmanz/wordpress-pecl-memcached-object-cache
//
// This Constant can be defined in the wp-config.php file.
if (defined('MEMCACHED_IS_ENABLED') && MEMCACHED_IS_ENABLED) {
// The Tollmanz Memcached Object Cache uses this global variable for the list of Memcached Servers
global $memcached_servers;
$memcached_servers = array(
array(
'127.0.0.1', // Memcached server IP address
11211 // Memcached server port
)
);
// Load the Tollmanz Memcached Library
// This example assumes that the Library file was copied to a plugins folder called "pecl-memcached-object-cache".
$memcache_plugin_file = dirname(__FILE__) . '/plugins/pecl-memcached-object-cache/object-cache.php';
require_once($memcache_plugin_file);
}