1

使用 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();

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

先感谢您。

4

2 回答 2

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 文档链接应该解释一些事情(包括其他发帖人关于让作曲家转储自动加载文件的评论(注意 url 中的“当前”):

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

于 2013-04-10T07:16:19.723 回答
1

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

检查官方文档的使用 Composer 的类映射功能部分。

参考:

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.
于 2013-04-02T19:30:33.780 回答