8000 [PropertyInfo] Cache support by dunglas · Pull Request #16917 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyInfo] Cache support #16917

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 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
dunglas committed Jan 26, 2016
commit 40081309ab0dbc62c1bcb08b8f31c16ff3324137
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@

namespace Symfony\Component\PropertyInfo;

use Doctrine\Common\Cache\Cache;
use Psr\Cache\CacheItemPoolInterface;

/**
* Adds a cache layer on top of an extractor.
* Adds a PSR-6 cache layer on top of an extractor.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class PropertyInfoExtractorCacheDecorator implements PropertyInfoExtractorInterface
class PropertyInfoCacheExtractor implements PropertyInfoExtractorInterface
{
/**
* @var PropertyInfoExtractorInterface
*/
private $propertyInfoExtractor;

/**
* @var Cache
* @var CacheItemPoolInterface
*/
private $cache;
private $cacheItemPool;

/**
* @var array
*/
private $arrayCache = array();

public function __construct(PropertyInfoExtractorInterface $propertyInfoExtractor, Cache $cache)
public function __construct(PropertyInfoExtractorInterface $propertyInfoExtractor, CacheItemPoolInterface $cacheItemPool)
{
$this->propertyInfoExtractor = $propertyInfoExtractor;
$this->cache = $cache;
$this->cacheItemPool = $cacheItemPool;
}

/**
Expand Down Expand Up @@ -106,19 +106,28 @@ private function extract($method, array $arguments)
return call_user_func_array(array($this->propertyInfoExtractor, $method), $arguments);
}

$key = $method.$serializedArguments;
$key = $this->escape($method.'.'.$serializedArguments);

if (isset($this->arrayCache[$key])) {
return $this->arrayCache[$key];
}

if ($value = $this->cache->fetch($key)) {
if ($value = $this->cacheItemPool->getItem($key)) {
return $this->arrayCache[$key] = $value;
}

$value = call_user_func_array(array($this->propertyInfoExtractor, $method), $arguments);
$this->cache->save($key, $value);
$this->cacheItemPool->save($key, $value);

return $this->arrayCache[$key] = $value;
}

private function escape($key)
{
str_replace(
array('_', '{', '}', '(', ')', '/', '\\', '@', ':'),
array('_95', '_123', '_125', '_40', '_41', '_47', '_92', '_64', '_58'),
$key
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Component\PropertyInfo\Tests;

use Symfony\Component\PropertyInfo\PropertyInfoCacheExtractor;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class PropertyInfoCacheExtractorTest extends AbstractPropertyInfoExtractorTest
{
public function setUp()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setUp is a protected method

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick search revealed there's few more occurrences. I'll send a PR to fix them.

{
parent::setUp();

$this->propertyInfo = new PropertyInfoCacheExtractor($this->propertyInfo, new ArrayCac);
}

public function testCache()
{
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array()));
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array()));
}

public function testNotSerializableContext()
{
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array('foo' => function () {})));
}

public function testEscape()
{
$reflectionMethod = new \ReflectionMethod($this->propertyInfo, 'escape');
$reflectionMethod->setAccessible(true);

$this->assertSame('foo_bar', $this->propertyInfo->escape('foo_95bar'));
$this->assertSame('foo_95bar', $this->propertyInfo->escape('foo_9595bar'));
$this->assertSame('foo{bar}', $this->propertyInfo->escape('foo_123bar_125'));
$this->assertSame('foo(bar)', $this->propertyInfo->escape('foo_40bar_41'));
$this->assertSame('foo/bar', $this->propertyInfo->escape('foo_47bar'));
$this->assertSame('foo\bar', $this->propertyInfo->escape('foo_92bar'));
$this->assertSame('foo@bar', $this->propertyInfo->escape('foo_64bar'));
$this->assertSame('foo:bar', $this->propertyInfo->escape('foo_58bar'));
}
}

This file was deleted.

0