8000 [RFC][WIP][2.3] Cache Bundle by dlsniper · Pull Request #5903 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[RFC][WIP][2.3] Cache Bundle #5903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Symfony/Bundle/CacheBundle/CacheBundle.php
Original file line number< 10000 /th> Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\CacheBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Bundle.
*
* @author Florin Patan <florinpatan@gmail.com>
*/
class CacheBundle extends Bundle
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\CacheBundle\DataCollector;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\Cache\CacheProfiler;

/**
* CacheDataCollector.
*
* @author Florin Patan <florinpatan@gmail.com>
*/
class CacheDataCollector extends DataCollector
{

private $cacheProfiler;

public function __construct(CacheProfiler $cacheProfiler)
{
$this->cacheProfiler = $cacheProfiler;
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data = $this->cacheProfiler->getResults();
}

public function getTotalHits()
{
return $this->data['hits'];
}

public function getTotalMisses()
{
return $this->data['ops'] - $this->data['hits'];
}

public function getTotalOps()
{
return $this->data['ops'];
}

public function getTotalTime()
{
return $this->data['time'];
}

public function getDrivers()
{
return $this->data['drivers'];
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'cache';
}
}
157 changes: 157 additions & 0 deletions src/Symfony/Bundle/CacheBundle/DependencyInjection/CacheExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\CacheBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Config\FileLocator;

/**
* Cache extension
*
* @author Florin Patan <florinpatan@gmail.com>
*/
class CacheExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('cache.xml');

$container->setParameter('cache.driver.class', $config['driver']['class']);

// Configure cache drivers
foreach ($config['drivers'] as $type => $driver) {
if (!$driver['enabled']) {
continue;
}

$container->setParameter(sprintf('cache.drivers.%s.class', $type), $driver['class']);
$container->setParameter(sprintf('cache.drivers.%s.ttl', $type), $driver['config']['ttl']);

$cacheDriverDefinition = new Definition();
$cacheDriverDefinition
->setPublic(false)
->setClass(sprintf('%%cache.drivers.%s.class%%', $type))
;

if (isset($driver['instance'])) {
$container->setParameter(sprintf('cache.internal.drivers.%s.instance', $type), $driver['instance']);

$internalDriverDefinition = new Definition();
$internalDriverDefinition
->setPublic(false)
->setClass(sprintf('%%cache.internal.drivers.%s.instance%%', $type))
;

if (isset($driver['servers']) && !empty($driver['servers'])) {
$container->setParameter(sprintf('cache.internal.drivers.%s.servers', $type), $driver['servers']);

$internalDriverDefinition->addMethodCall('addServers', array(sprintf('%%cache.internal.drivers.%s.servers%%', $type)));
}

$container->setDefinition('cache.internal.driver.' . $type, $internalDriverDefinition);
}

if (isset($driver['service'])) {
$cacheDriverDefinition->addArgument(new Reference($driver['service']));
} elseif(isset($driver['instance'])) {
$cacheDriverDefinition->addArgument(new Reference(sprintf('cache.internal.driver.%s', $type)));
}

$container->setDefinition('cache.driver.' . $type, $cacheDriverDefinition);
}


// Configure cache instances
foreach ($config['instances'] as $name => $instanceConfig) {

$instanceType = $instanceConfig['type'];
$driverInstance = $config['drivers'][$instanceType];

if (!$driverInstance['enabled']) {
continue;
}

$instanceConfig['config'] = array_merge($driverInstance['config'], $instanceConfig['config']);
$instanceConfig = array_merge($driverInstance, $instanceConfig);

if (isset($instanceConfig['service'])) {
unset($instanceConfig['servers'], $instanceConfig['instance']);
} else {
unset($instanceConfig['service']);
}

if (isset($instanceConfig['servers']) && isset($driverInstance['servers'])) {
$instanceConfig['servers'] = array_merge($driverInstance['servers'], $instanceConfig['servers']);
}

if (!empty($instanceConfig['servers']) && isset($instanceConfig['instance'])) {
$container->setParameter(sprintf('cache.internal.instances.%s.class', $name), $instanceConfig['instance']);

$internalDriverDefinition = new Definition();
$internalDriverDefinition
->setPublic(false)
->setClass(sprintf('%%cache.internal.instances.%s.class%%', $name))
;

$container->setParameter(sprintf('cache.internal.instances.%s.servers', $name), $instanceConfig['servers']);

$internalDriverDefinition->addMethodCall('addServers', array(sprintf('%%cache.internal.instances.%s.servers%%', $name)));

$internalCacheDriverName = sprintf('cache.internal.driver.%s_%s', $instanceConfig['type'], $name);
$container->setDefinition($internalCacheDriverName, $internalDriverDefinition);
} elseif (isset($instanceConfig['service'])) {
$internalCacheDriverName = $instanceConfig['service'];
} else {
$internalCacheDriverName = sprintf('cache.driver.%s', $instanceConfig['type']);
}

$container->setParameter(sprintf('cache.instances.%s.instance.class', $name), $instanceConfig['class']);
$container->setParameter(sprintf('cache.instances.%s.instance.config.ttl', $name), $instanceConfig['config']['ttl']);

$internalInstanceDefinition = new Definition();
$internalInstanceDefinition
->setPublic(false)
->setClass(sprintf('%%cache.instances.%s.instance.class%%', $name))
->addArgument(new Reference($internalCacheDriverName))
;

$internalInstanceName = sprintf('cache.instance.%s_%s', $instanceType, $name);

$container->setDefinition($internalInstanceName, $internalInstanceDefinition);

$container->setParameter(sprintf('cache.instances.%s.config.ttl', $name), $instanceConfig['config']['ttl']);

$cacheInstanceDefinition = new Definition();
$cacheInstanceDefinition
->setClass('%cache.driver.class%')
->addMethodCall('setDefaultTtl', array(sprintf('%%cache.instances.%s.config.ttl%%', $name)))
->addMethodCall('setProfiler', array(new Reference('cache.profiler')))
->addMethodCall('setLogger', array(new Reference('logger')))
->addArgument(new Reference($internalInstanceName))
->addArgument($name)
->addArgument($instanceConfig['type'])
;


$container->setDefinition('cache.' . $name, $cacheInstanceDefinition);
}
}
}
Loading
0