Symfony2和XCache。有没有人让它工作?

时间:2013-04-02 18:39:58

标签: php symfony xcache

使用Symfony 2.0,我试图让它与XCache一起工作。

正确安装了XCache。

至于正式的Symfony文档,我们应该有这个XcacheClassLoader.php。至于相同的文档,我们得到了这条建议:

 /**
 * XcacheClassLoader implements a wrapping autoloader cached in Xcache for PHP 5.3.
 *
 * It expects an object implementing a findFile method to find the file. This
 * allows using it as a wrapper around the other loaders of the component (the
 * ClassLoader and the UniversalClassLoader for instance) but also around any
 * other autoloader following this convention (the Composer one for instance)
 *
 *     $loader = new ClassLoader();
 *
 *     // register classes with namespaces
 *     $loader->add('Symfony\Component', __DIR__.'/component');
 *     $loader->add('Symfony',           __DIR__.'/framework');
 *
 *     $cachedLoader = new XcacheClassLoader('my_prefix', $loader);
 *
 *     // activate the cached autoloader
 *     $cachedLoader->register();
 *
 *     // eventually deactivate the non-cached loader if it was registered previously
 *     // to be sure to use the cached one.
 *     $loader->unregister();
 *
 * @author Fabien Potencier <fabien@symfony.com>
 * @author Kris Wallsmith <kris@symfony.com>
 * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
 *
 * @api
 */

如果我做对了,autoload.php应该包含该代码。这就是我在autoload.php中所做的事情:

声明正常加载器:

$loader = new UniversalClassLoader();

所有内容都是对那个$ loader完成的,比如registerNamespaces,registerPrefixes和所有外部依赖都被加载到$loader

$loader
->registerNamespaces(
    array(
        'Symfony' => array(__DIR__ . '/../vendor/symfony/src',
                __DIR__ . '/../vendor/bundles'),
        'Sensio' => __DIR__ . '/../vendor/bundles',
        'JMS' => __DIR__ . '/../vendor/bundles',
        'Doctrine\\Common' => __DIR__. '/../vendor/doctrine-common/lib',
        'Doctrine\\DBAL' => __DIR__
etc...

一旦对$ loader进行了所有“正常”操作,我就会声明$ cachedLoader,就像文档中所述:

 // register classes with namespaces
 $loader->add('Symfony\Component', __DIR__.'/component');
 $loader->add('Symfony',           __DIR__.'/framework');

 $cachedLoader = new XcacheClassLoader('my_prefix', $loader);

 // activate the cached autoloader
 $cachedLoader->register();

 // eventually deactivate the non-cached loader if it was registered previously
 // to be sure to use the cached one.
 $loader->unregister();

我说得对吗?显然不是,因为我得到的只是我浏览器中的一个完全空白的页面,甚至没有记录日志,以防他们能给我一些线索。

提前谢谢。

2 个答案:

答案 0 :(得分:1)

我没有使用XCache,但我可以建议你另一种方法来提高类加载性能,据我所知,这是最好的方法。

查看官方文档的Use Composer's Class Map Functionality部分。

参考文献:

By default, the Symfony2 standard edition uses Composer's autoloader in the 
autoload.php file. This autoloader is easy to use, as it will automatically find 
any new classes that you've placed in the registered directories.

Unfortunately, this comes at a cost, as the loader iterates over all configured 
namespaces to find a particular file, making file_exists calls until it finally 
finds the file it's looking for.

The simplest solution is to tell Composer to build a "class map" (i.e. a big 
array of the locations of all the classes). This can be done from the command 
line, and might become part of your deploy process:

    php composer.phar dump-autoload --optimize

Internally, this builds the big class map array in 
vendor/composer/autoload_namespaces.php.

答案 1 :(得分:1)

如果您还不是2.2,我建议您尽可能升级到2.2。 2.0现在处于维护周期的末尾。

但是,既然我们都有这方面的经验 - 对于symfony 2.0,没有包含XcacheClassLoader,你必须自己编写然后把它放在app / autoload.php中(在APC加载器的同一个地方) )。对于symfony 2.2,情况不再如此。

接下来,我建议你确保你的xcache正常工作 - 你可能想创建一个php文件并做这样的测试:

<?php

$something="test";
xcache_set('key', $something, 0);
if (xcache_get('key') !== $something)
    echo "something's wrong...";
else
    echo "xcache appears to be working";

最后,我们所做的几乎就是Symfony / web / app.php(针对XcacheClassLoader进行了修改)的最新版本 - 这将是(以其简单形式)的样子:

<?php

use Symfony\Component\ClassLoader\XcacheClassLoader;
use Symfony\Component\HttpFoundation\Request;

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';

$loader = new XcacheClassLoader('sf2', $loader);
$loader->register(true);

require_once __DIR__.'/../app/AppKernel.php';
require_once __DIR__.'/../app/AppCache.php';

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$kernel = new AppCache($kernel);

// rest of app.php

下面的symfony2文档链接应该解释一些事情(包括其他海报关于让作曲家转储自动加载文件的注释(注意网址中的'当前'):

http://symfony.com/doc/current/book/performance.html