Skip to content

Commit

Permalink
Symfony 4 cache pool support
Browse files Browse the repository at this point in the history
  • Loading branch information
kayue committed Dec 20, 2019
1 parent 97c70f5 commit 532cf9e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 92 deletions.
5 changes: 5 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function getConfigTreeBuilder()
->scalarNode('cookie_domain')->defaultValue(null)->end()
->scalarNode('table_prefix')->defaultValue('wp_')->end()
->scalarNode('connection')->defaultValue('default')->end()
->arrayNode('orm')->children()
->scalarNode('metadata_cache_pool')->defaultValue('cache.system')->end()
->scalarNode('query_cache_pool')->defaultValue('cache.app')->end()
->scalarNode('result_cache_pool')->defaultValue('cache.app')->end()
->end()
->end();

return $treeBuilder;
Expand Down
3 changes: 3 additions & 0 deletions DependencyInjection/KayueWordpressExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.yml');

$container->setAlias('kayue_wordpress.dbal.connection', sprintf('doctrine.dbal.%s_connection', $config['connection']));
$container->setAlias('kayue_wordpress.orm.metadata_cache.pool', $config['orm']['metadata_cache_pool']);
$container->setAlias('kayue_wordpress.orm.result_cache.pool', $config['orm']['result_cache_pool']);
$container->setAlias('kayue_wordpress.orm.query_cache.pool', $config['orm']['query_cache_pool']);
}
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ I started that bundle two years ago and the original repository grew somewhat ch
* Twig extension (v1.1.0)
* WordPress style shortcode (v1.1.0)
* Major code update. (v2.0.0)
* Support Symfony 4, new cache configuration (v4.0.0)

#### Todo

Expand Down Expand Up @@ -73,6 +74,12 @@ kayue_wordpress:

# Doctrine connection to use. Default is 'default'.
connection: 'default'

# Specify Symfony cache pool
orm:
metadata_cache_pool: cache.system
query_cache_pool: cache.app
result_cache_pool: cache.app

# The following configuration only needed only when you use WordPress authentication.

Expand Down
3 changes: 3 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ services:
- '@event_dispatcher'
- '%kernel.root_dir%'
- '%kernel.environment%'
- '@kayue_wordpress.orm.metadata_cache.pool'
- '@kayue_wordpress.orm.result_cache.pool'
- '@kayue_wordpress.orm.query_cache.pool'

kayue_wordpress.helper.attachment:
class: Kayue\WordpressBundle\Wordpress\Helper\AttachmentHelper
Expand Down
115 changes: 24 additions & 91 deletions Wordpress/ManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BadMethodCallException;
use Doctrine\Common\Persistence\ManagerRegistry as ManagerRegistryInterface;
use Psr\Cache\CacheItemPoolInterface;
use Redis;
use Memcache;
use Memcached;
Expand All @@ -12,6 +13,9 @@
use Doctrine\ORM\Tools\Setup;
use Kayue\WordpressBundle\Doctrine\WordpressEntityManager;
use Kayue\WordpressBundle\WordpressEvents;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ProxyAdapter;
use Symfony\Component\Cache\DoctrineProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

Expand All @@ -38,19 +42,29 @@ class ManagerRegistry implements ManagerRegistryInterface
protected $previousBlogId = 1;
protected $managers = [];

private $metaCache;
private $queryCache;
private $resultCache;

public function __construct(
Connection $connection,
EntityManager $defaultEntityManager,
EventDispatcherInterface $eventDispatcher,
$rootDir,
$environment
$environment,
AdapterInterface $metadataCache,
AdapterInterface $queryCache,
AdapterInterface $resultCache
)
{
$this->connection = $connection;
$this->defaultEntityManager = $defaultEntityManager;
$this->eventDispatcher = $eventDispatcher;
$this->rootDir = $rootDir;
$this->environment = $environment;
$this->metadataCache = $metadataCache;
$this->queryCache = $queryCache;
$this->resultCache = $resultCache;
}

/**
Expand All @@ -75,9 +89,10 @@ public function getManager($blogId = null)
$this->eventDispatcher->dispatch(WordpressEvents::CREATE_ENTITY_MANAGER, new GenericEvent($em));

$em->setBlogId($this->currentBlogId);
$em->getMetadataFactory()->setCacheDriver($this->getCacheImpl('metadata_cache', $this->currentBlogId));
$em->getConfiguration()->setQueryCacheImpl($this->getCacheImpl('query_cache', $this->currentBlogId));
$em->getConfiguration()->setResultCacheImpl($this->getCacheImpl('result_cache', $this->currentBlogId));

$em->getMetadataFactory()->setCacheDriver($this->getCacheProvider($this->metadataCache, $this->currentBlogId));
$em->getConfiguration()->setQueryCacheImpl($this->getCacheProvider($this->queryCache, $this->currentBlogId));
$em->getConfiguration()->setResultCacheImpl($this->getCacheProvider($this->resultCache, $this->currentBlogId));

$this->managers[$this->currentBlogId] = $em;
}
Expand Down Expand Up @@ -108,95 +123,13 @@ public function restorePreviousBlog()
$this->setCurrentBlogId($this->previousBlogId);
}

/**
* Loads a configured object manager metadata, query or result cache driver.
*
* @param string $cacheName
*
* @param $blogId
* @throws \InvalidArgumentException In case of unknown driver type.
* @return \Doctrine\Common\Cache\Cache
*/
protected function getCacheImpl($cacheName, $blogId)
protected function getCacheProvider(CacheItemPoolInterface $pool, $blogId)
{
$config = $this->defaultEntityManager->getConfiguration();

switch ($cacheName) {
case 'metadata_cache':
$baseCache = $config->getMetadataCacheImpl();
break;
case 'query_cache':
$baseCache = $config->getQueryCacheImpl();
break;
case 'result_cache':
$baseCache = $config->getResultCacheImpl();
break;
default:
throw new \InvalidArgumentException(sprintf('"%s" is an unrecognized Doctrine cache name.
Supported cache names are: "metadata_cache", "query_cache" and "result_cache"', $cacheName));
}

$namespace = 'sf2_kayue_wordpress_bundle_blog_'.$blogId.'_'.md5($this->rootDir.$this->environment);

$className = get_class($baseCache);

switch ($className) {
case 'Doctrine\Common\Cache\ApcCache':
case 'Doctrine\Common\Cache\ApcuCache':
case 'Doctrine\Common\Cache\ArrayCache':
case 'Doctrine\Common\Cache\XcacheCache':
case 'Doctrine\Common\Cache\WinCacheCache':
case 'Doctrine\Common\Cache\ZendDataCache':
$cache = new $className();
break;
case 'Doctrine\Common\Cache\MemcacheCache':
$memcache = $baseCache->getMemcache();
$rawStats = $memcache->getExtendedStats();
$servers = array_keys($rawStats);

$cache = new $className();
$newMemcache = new Memcache();

foreach ($servers as $server) {
$host = substr($server, 0, strpos($server, ':'));
$port = substr($server, strpos($server, ':') + 1);
$newMemcache->connect($host, $port);
}

$cache->setMemcache($newMemcache);
break;
case 'Doctrine\Common\Cache\MemcachedCache':
$memcached = $baseCache->getMemcached();
$servers = $memcached->getServerList();

$cache = new $className();
$newMemcached = new Memcached();

foreach ($servers as $server) {
$newMemcached->connect($server['host'], $server['port']);
}

$cache->setMemcached($newMemcached);
break;
case 'Doctrine\Common\Cache\RedisCache':
$redis = $baseCache->getRedis();
$host = $redis->getHost();
$port = $redis->getPort();

$cache = new $className();

$newRedis = new Redis();
$newRedis->connect($host, $port);

$cache->setRedis($newRedis);
break;
default:
throw new \InvalidArgumentException(sprintf('Unknown or unsupported cache type class in configuration: "%s"', get_class($baseCache)));
}

$cache->setNamespace($namespace);
$namespace = sprintf('wordpress_blog_%s_', $blogId);
$proxyAdapter = new ProxyAdapter($pool, $namespace);
$doctrineCache = new DoctrineProvider($proxyAdapter);

return $cache;
return $doctrineCache;
}

public function getDefaultConnectionName()
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

"require": {
"php": "^5.5.9|~7.0",
"symfony/framework-bundle": "^2.7|^3.0|^4.0|^5.0",
"symfony/framework-bundle": "^4.0|^5.0",
"doctrine/orm": "~2.2,>=2.2.3"
},
"require-dev": {
Expand Down

0 comments on commit 532cf9e

Please sign in to comment.