Skip to content

Support for simple cache #75

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

Merged
merged 8 commits into from
Jul 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
"php": "^5.6 || ^7.0",
"symfony/framework-bundle": "^2.7 || ^3.0",
"cache/taggable-cache": "^0.5",
"cache/session-handler": "^0.2"
"cache/session-handler": "^0.2",
Copy link

Choose a reason for hiding this comment

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

we have stable version, why dont use them?

Copy link
Member Author

Choose a reason for hiding this comment

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

It was not a part of this PR.

But here: #76

"nyholm/nsa": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^5.7.17",
"symfony/symfony": "^2.7 || ^3.0",
"cache/psr-6-doctrine-bridge": "^3.0",
"cache/array-adapter": "^0.5",
"nyholm/symfony-bundle-test": "^1.0.1",
"nyholm/symfony-bundle-test": "^1.2",
"matthiasnoback/symfony-dependency-injection-test": "^1.0"
},
"suggest": {
Expand Down
74 changes: 0 additions & 74 deletions src/Cache/Recording/Factory.php

This file was deleted.

48 changes: 0 additions & 48 deletions src/Cache/Recording/TaggablePool.php

This file was deleted.

11 changes: 5 additions & 6 deletions src/DataCollector/CacheDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Cache\CacheBundle\DataCollector;

use Cache\CacheBundle\Cache\Recording\CachePool;
use Cache\CacheBundle\Cache\Recording\TraceableAdapterEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -26,15 +25,15 @@
class CacheDataCollector extends DataCollector
{
/**
* @type CachePool[]
* @type CacheProxy[]
*/
private $instances = [];

/**
* @param string $name
* @param CachePool $instance
* @param string $name
* @param CacheProxy $instance
*/
public function addInstance($name, CachePool $instance)
public function addInstance($name, CacheProxy $instance)
{
$this->instances[$name] = $instance;
}
Expand All @@ -47,7 +46,7 @@ public function collect(Request $request, Response $response, \Exception $except
$empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []];
$this->data = ['instances' => $empty, 'total' => $empty];
foreach ($this->instances as $name => $instance) {
$this->data['instances']['calls'][$name] = $instance->getCalls();
$this->data['instances']['calls'][$name] = $instance->__getCalls();
}

$this->data['instances']['statistics'] = $this->calculateStatistics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
* with this source code in the file LICENSE.
*/

namespace Cache\CacheBundle\Cache\Recording;

use Cache\Hierarchy\HierarchicalPoolInterface;
namespace Cache\CacheBundle\DataCollector;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
* An interface for a cache proxy. A cache proxy is created when we profile a cache pool.
*
* @internal
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class HierarchyAndTaggablePool extends TaggablePool implements HierarchicalPoolInterface
interface CacheProxy
{
public function __getCalls();

public function __setName($name);
}
55 changes: 55 additions & 0 deletions src/DataCollector/DecoratingFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of php-cache\cache-bundle package.
*
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Cache\CacheBundle\DataCollector;

use Nyholm\NSA;
use Psr\Cache\CacheItemPoolInterface;

/**
* A factory that decorates another factory to be able to use the proxy cache.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class DecoratingFactory
{
/**
* @type ProxyFactory
*/
private $proxyFactory;

/**
* @param ProxyFactory $proxyFactory
*/
public function __construct(ProxyFactory $proxyFactory)
{
$this->proxyFactory = $proxyFactory;
}

/**
* @param CacheItemPoolInterface $originalObject original class
*
* @return CacheProxy|CacheItemPoolInterface
*/
public function create($originalObject)
{
$proxyClass = $this->proxyFactory->createProxy(get_class($originalObject));
$rc = new \ReflectionClass($proxyClass);
$pool = $rc->newInstanceWithoutConstructor();

// Copy properties from original pool to new
foreach (NSA::getProperties($originalObject) as $property) {
NSA::setProperty($pool, $property, NSA::getProperty($originalObject, $property));
}

return $pool;
}
}
74 changes: 74 additions & 0 deletions src/DataCollector/ProxyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of php-cache\cache-bundle package.
*
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Cache\CacheBundle\DataCollector;

/**
* Generate proxies over your cache pool. This should only be used in development.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ProxyFactory
{
/**
* @type string
*/
private $proxyDirectory;

/**
* @param string $proxyDirectory
*/
public function __construct($proxyDirectory)
{
$this->proxyDirectory = $proxyDirectory;
}

/**
* Create a proxy that handles data collecting better.
*
* @param string $class
* @param string &$proxyFile where we store the proxy class
*
* @return string the name of a much much better class
*/
public function createProxy($class, &$proxyFile = null)
{
$proxyClass = $this->getProxyClass($class);
$class = '\\'.rtrim($class, '\\');
$proxyFile = $this->proxyDirectory.'/'.$proxyClass.'.php';

if (class_exists($proxyClass)) {
return $proxyClass;
}

$content = file_get_contents(dirname(__DIR__).'/Resources/proxy/template.php');
$content = str_replace('__TPL_CLASS__', $proxyClass, $content);
$content = str_replace('__TPL_EXTENDS__', $class, $content);

$this->checkProxyDirectory();
file_put_contents($proxyFile, $content);
require $proxyFile;

return $proxyClass;
}

private function checkProxyDirectory()
{
if (!is_dir($this->proxyDirectory)) {
@mkdir($this->proxyDirectory, 0777, true);
}
}

private function getProxyClass($namespace)
{
return 'php_cache_proxy_'.str_replace('\\', '_', $namespace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
* with this source code in the file LICENSE.
*/

namespace Cache\CacheBundle\Cache\Recording;

use Cache\Hierarchy\HierarchicalPoolInterface;
namespace Cache\CacheBundle\DataCollector;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*
* @internal
*/
class HierarchyPool extends CachePool implements HierarchicalPoolInterface
class TraceableAdapterEvent
{
public $name;
public $argument;
public $start;
public $end;
public $result;
public $hits = 0;
public $misses = 0;
}
Loading