如何在Joomla缓存设置中设置两个memcached服务器主机?

时间:2016-07-08 05:07:42

标签: joomla memcached joomla3.0

我已在两台服务器上设置了两个memcached实例。如何在Joomla缓存设置中设置主机?

enter image description here

1 个答案:

答案 0 :(得分:2)

您必须编辑站点根文件夹中的configuration.php文件并添加服务器详细信息。找到缓存变量 $caching, $cache_handler, $memcache_server_host, $memcache_server_port.

将它们设置如下

public $caching = '2';
public $cache_handler = 'memcache';
public $memcache_server_host = 'localhost';//Give your memcache server address
public $memcache_server_port = '11111'; //Memcache server port

在libraries / joomla / cache / storage / memcache.php文件中,我找到了这行

       /*
         * This will be an array of loveliness
         ***@todo: multiple servers***
         * $servers = (isset($params['servers'])) ? $params['servers'] : array();
         */

如果您想添加两个不同的memcache服务器,您可以看到它仍在开发中。但有一种方法---通过黑客攻击核心文件。

不要尝试生产环境中的方法

虽然使用核心文件很危险但是你可以按照我现在建议的方法自行承担风险。先备份。

备份文件(configuration.php和libraries / joomla / cache / storage / memcache.php和libraries \ vendor \ joomla \ session \ Joomla \ Session \ Storage \ Memcache.php)。在memcache文件中搜索文件中的此代码

$server['host'] = $config->get('memcache_server_host', 'localhost');
        $server['port'] = $config->get('memcache_server_port', 11211);

        // Create the memcache connection
        self::$_db = new Memcache;
        self::$_db->addServer($server['host'], $server['port'], $this->_persistent);

        $memcachetest = @self::$_db->connect($server['host'], $server['port']);

        if ($memcachetest == false)
        {
            throw new RuntimeException('Could not connect to memcache server', 404);
        }

在下面添加此代码

    //Give second server details like this
$server['host2'] = $config->get('memcache_server_host2', 'localhost');
$server['port2'] = $config->get('memcache_server_port2', 11211);
    // Create the memcache connection
            self::$_db = new Memcache;
            self::$_db->addServer($server['host2'], $server['port2'], $this->_persistent);

            $memcachetest = @self::$_db->connect($server['host2'], $server['port2']);

            if ($memcachetest == false)
            {
                throw new RuntimeException('Could not connect to memcache server', 404);
            }

在configuration.php文件中添加以下行

public $memcache_server_host2 = 'localhost';//Give your second memcache server address
public $memcache_server_port2 = '11111'; //Memcache second server port

现在,要存储会话,您必须编辑存储会话缓存的文件

在文件库\ vendor \ joomla \ session \ Joomla \ Session \ Storage \ Memcache.php中,你会发现这些行

$this->_servers = array(
            array(
                'host' => isset($options['memcache_server_host']) ? $options['memcache_server_host'] : 'localhost',
                'port' => isset($options['memcache_server_port']) ? $options['memcache_server_port'] : 11211
            )
        );

将此更改为

$this->_servers = array(
            array(
                'host' => isset($options['memcache_server_host']) ? $options['memcache_server_host'] : 'localhost',
                'port' => isset($options['memcache_server_port']) ? $options['memcache_server_port'] : 11211
            ),
array(
                'host2' => isset($options['memcache_server_host2']) ? $options['memcache_server_host2'] : 'localhost',
                'port2' => isset($options['memcache_server_port2']) ? $options['memcache_server_port2'] : 11211
            )
        );