使用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('SymfonyComponent', __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
*/
如果我是正确的,autolload .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('SymfonyComponent', __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();
我写对了吗?显然不是,因为我在浏览器中得到的是一个完全空白的页面,甚至没有日志记录,以防他们能给我一些线索。
提前感谢。
我没有使用XCache,但是我可以建议你一种替代方法来提高类加载性能,而且就我所记得的,这是最好的方法。
查看官方文档中的Use Composer's Class Map function部分
参:
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.
如果您还没有使用2.2,我建议您尽可能升级到2.2。2.0现在处于维护周期的末尾。
但是,因为我们在这两个方面都有经验-对于symfony 2.0没有XcacheClassLoader包含,你必须自己写它,然后把它放在app/autolload .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 SymfonyComponentClassLoaderXcacheClassLoader;
use SymfonyComponentHttpFoundationRequest;
$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文档链接应该解释了一些事情(包括其他海报关于让composer转储自动加载文件的评论(注意url中的'current'):
http://symfony.com/doc/current/book/performance.html