Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Configured timeout to clear Symfony cache (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
mremi authored and OskarStark committed Sep 2, 2016
1 parent 1498328 commit 988c8bb
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 13 deletions.
24 changes: 21 additions & 3 deletions Adapter/SymfonyCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,44 @@ class SymfonyCache implements CacheAdapterInterface
*/
protected $servers;

/**
* @var array
*/
protected $timeouts;

/**
* Constructor.
*
* NEXT_MAJOR: make the timeouts argument mandatory
*
* @param RouterInterface $router A router instance
* @param Filesystem $filesystem A Symfony Filesystem component instance
* @param string $cacheDir A Symfony cache directory
* @param string $token A token to clear the related cache
* @param bool $phpCodeCacheEnabled If true, will clear APC or PHP OPcache code cache
* @param array $types A cache types array
* @param array $servers An array of servers
* @param array $timeouts An array of timeout options
*/
public function __construct(RouterInterface $router, Filesystem $filesystem, $cacheDir, $token, $phpCodeCacheEnabled, array $types, array $servers)
public function __construct(RouterInterface $router, Filesystem $filesystem, $cacheDir, $token, $phpCodeCacheEnabled, array $types, array $servers, array $timeouts = array())
{
if (!$timeouts) {
@trigger_error('The "timeouts" argument is available since 3.x and will become mandatory in 4.0, please provide it.', E_USER_DEPRECATED);

$timeouts = array(
'RCV' => array('sec' => 2, 'usec' => 0),
'SND' => array('sec' => 2, 'usec' => 0),
);
}

$this->router = $router;
$this->filesystem = $filesystem;
$this->cacheDir = $cacheDir;
$this->token = $token;
$this->types = $types;
$this->phpCodeCacheEnabled = $phpCodeCacheEnabled;
$this->servers = $servers;
$this->timeouts = $timeouts;
}

/**
Expand Down Expand Up @@ -117,8 +135,8 @@ public function flush(array $keys = array('all'))
$command .= "Connection: Close\r\n\r\n";

// setup the default timeout (avoid max execution time)
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 2, 'usec' => 0));
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 2, 'usec' => 0));
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $this->timeouts['SND']);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $this->timeouts['RCV']);

socket_connect($socket, $server['ip'], $server['port']);
socket_write($socket, $command);
Expand Down
39 changes: 39 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,45 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('timeout')
->addDefaultsIfNotSet()
->children()
->arrayNode('RCV')
->addDefaultsIfNotSet()
->children()
->integerNode('sec')
->info('Timeout value specifying the amount of seconds for input operations')
->defaultValue(2)
->end()
->integerNode('usec')
->info('Timeout value specifying the amount of microseconds for input operations')
->defaultValue(0)
->end()
->end()
->end()
->arrayNode('SND')
->addDefaultsIfNotSet()
->children()
->integerNode('sec')
->info(<<<'INFO'
Timeout value specifying the amount of seconds that an output function
blocks because flow control prevents data from being sent
INFO
)
->defaultValue(2)
->end()
->integerNode('usec')
->info(<<<'INFO'
Timeout value specifying the amount of microseconds that an output function
blocks because flow control prevents data from being sent
INFO
)
->defaultValue(0)
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/SonataCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public function configureCache(ContainerBuilder $container, $config)
->replaceArgument(4, $config['caches']['symfony']['php_cache_enabled'])
->replaceArgument(5, $config['caches']['symfony']['types'])
->replaceArgument(6, $this->configureServers($config['caches']['symfony']['servers']))
->replaceArgument(7, $config['caches']['symfony']['timeout'])
;
} else {
$container->removeDefinition('sonata.cache.symfony');
Expand Down
1 change: 1 addition & 0 deletions Resources/config/cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<argument/>
<argument/>
<argument type="collection"/>
<argument/>
</service>
<service id="sonata.cache.invalidation.simple" class="Sonata\CacheBundle\Invalidation\SimpleCacheInvalidation">
<argument type="service" id="logger"/>
Expand Down
5 changes: 4 additions & 1 deletion Resources/doc/reference/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ To use the ``CacheBundle``, add the following lines to your application configur
php_cache_enabled: true # Optional (default: false), clear APC or PHP OPcache
types: [mytype1, mycustomtype2] # Optional, you can restrict allowed cache types
servers:
- { domain: kooqit.local, ip: 127.0.0.1, port: 80}
- { domain: kooqit.local, ip: 127.0.0.1, port: 80 }
timeout:
RCV: { sec: 2, usec: 0 }
SND: { sec: 2, usec: 0 }
For APC and Symfony caches, you can specify a basic parameter for servers definition (useful to clear cache for staging area behind this kind of protection)

Expand Down
22 changes: 20 additions & 2 deletions Tests/Adapter/SymfonyCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ protected function setUp()
$this->router = $this->getMock('Symfony\Component\Routing\RouterInterface');
$this->filesystem = $this->getMock('Symfony\Component\Filesystem\Filesystem');

$this->cache = new SymfonyCache($this->router, $this->filesystem, '/cache/dir', 'token', false, array('all', 'translations'), array());
$this->cache = new SymfonyCache(
$this->router,
$this->filesystem,
'/cache/dir',
'token',
false,
array('all', 'translations'),
array(),
array()
);
}

/**
Expand Down Expand Up @@ -126,7 +135,8 @@ public function testFlushThrowsExceptionWithWrongIP()
array('all', 'translations'),
array(
array('ip' => 'wrong ip'),
)
),
array()
);

$this->setExpectedException('\InvalidArgumentException', '"wrong ip" is not a valid ip address');
Expand All @@ -150,6 +160,10 @@ public function testFlushWithIPv4()
array('all', 'translations'),
array(
array('ip' => '213.186.35.9', 'domain' => 'www.example.com', 'basic' => false, 'port' => 80),
),
array(
'RCV' => array('sec' => 2, 'usec' => 0),
'SND' => array('sec' => 2, 'usec' => 0),
)
);

Expand Down Expand Up @@ -204,6 +218,10 @@ public function testFlushWithIPv6()
array('all', 'translations'),
array(
array('ip' => '2001:41d0:1:209:FF:FF:FF:FF', 'domain' => 'www.example.com', 'basic' => false, 'port' => 80),
),
array(
'RCV' => array('sec' => 2, 'usec' => 0),
'SND' => array('sec' => 2, 'usec' => 0),
)
);

Expand Down
67 changes: 60 additions & 7 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
*/
public function testApcDefaultTimeout()
{
$expected = array(
'RCV' => array(),
'SND' => array(),
);

$configs = array(array(
'caches' => array(
'apc' => array(
Expand All @@ -41,7 +36,13 @@ public function testApcDefaultTimeout()
$config = $this->process($configs);

$this->assertArrayHasKey('timeout', $config['caches']['apc']);
$this->assertEquals($expected, $config['caches']['apc']['timeout']);
$this->assertSame(
array(
'RCV' => array(),
'SND' => array(),
),
$config['caches']['apc']['timeout']
);
}

/**
Expand All @@ -67,7 +68,59 @@ public function testApcCustomTimeout()
$config = $this->process($configs);

$this->assertArrayHasKey('timeout', $config['caches']['apc']);
$this->assertEquals($expected, $config['caches']['apc']['timeout']);
$this->assertSame($expected, $config['caches']['apc']['timeout']);
}

/**
* Asserts Symfony has default timeout values.
*/
public function testSymfonyDefaultTimeout()
{
$configs = array(array(
'caches' => array(
'symfony' => array(
'token' => '',
'types' => array('all'),
),
),
));

$config = $this->process($configs);

$this->assertArrayHasKey('timeout', $config['caches']['symfony']);
$this->assertSame(
array(
'RCV' => array('sec' => 2, 'usec' => 0),
'SND' => array('sec' => 2, 'usec' => 0),
),
$config['caches']['symfony']['timeout']
);
}

/**
* Asserts Symfony timeout has custom values.
*/
public function testSymfonyCustomTimeout()
{
$expected = array(
'RCV' => array('sec' => 10, 'usec' => 0),
'SND' => array('sec' => 18, 'usec' => 12),
);

$configs = array(array(
'caches' => array(
'symfony' => array(
'token' => '',
'types' => array('all'),
'timeout' => $expected,
),
),
));

$config = $this->process($configs);

$this->assertArrayHasKey('timeout', $config['caches']['symfony']);
$this->assertSame($expected, $config['caches']['symfony']['timeout']);
}

/**
Expand Down

0 comments on commit 988c8bb

Please sign in to comment.