Skip to content

Commit

Permalink
Merge pull request #21 from ingenerator/3.x-migrate-caching
Browse files Browse the repository at this point in the history
v3: Migrate from doctrine/cache to symfony/cache
  • Loading branch information
acoulton authored Dec 10, 2024
2 parents 16444ee + 0169982 commit b45dc5f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 45 deletions.
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
## v3.0.0 (2024-12-09)

* [BREAKING] Switch to using attributes instead of annotations for mapping
- Removes the `ExplicitCallslistAnnotationDriver` class
- Removes the `doctrine.config.metadata.reader` service definition
- Adds `ExplicitClasslistAttributeDriver` as the default mapping driver
- Drops composer dependency on doctrine/annotations
- Removes the `ExplicitCallslistAnnotationDriver` class
- Removes the `doctrine.config.metadata.reader` service definition
- Adds `ExplicitClasslistAttributeDriver` as the default mapping driver
- Drops composer dependency on doctrine/annotations
* [BREAKING] Switch to using PSR-6 cache implementations for metadata, query and result caches:
- drops the composer dependency on doctrine/cache in favour of symfony/cache
- DoctrineCacheFactory methods are now hard-typehinted to return CacheItemPoolInterface
* Narrow supported dependency versions to the current latest minor of the suppoerted
major version.
* Drop support for PHP < 8.2
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
"php": "~8.2.0 || ~8.3.0",
"ext-pdo": "*",
"composer/installers": "^1.12 || ^2",
"doctrine/cache": "^1.13",
"doctrine/common": "^3.4",
"doctrine/dbal": "^3.9",
"doctrine/orm": "^2.20",
"doctrine/persistence": "^2.5",
"ingenerator/kohana-core": "^4.12",
"ingenerator/kohana-dependencies": "^1.4"
"ingenerator/kohana-dependencies": "^1.4",
"symfony/cache": "^6.4 || ^7.2"
},
"require-dev": {
"kohana/koharness": "dev-master",
Expand Down
27 changes: 14 additions & 13 deletions src/Dependency/DoctrineCacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
namespace Ingenerator\KohanaDoctrine\Dependency;


use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\ArrayCache;
use Kohana;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

class DoctrineCacheFactory
{
Expand All @@ -14,30 +16,29 @@ class DoctrineCacheFactory
* The data cache is used for query result etc caching.
*
* It is always present, but only used if specific queries / operations indicate that they're cacheable. Also by
* default it uses an ArrayCache in all environments. This ensures code paths don't need to vary to cope with the
* default it uses an ArrayAdapter in all environments. This ensures code paths don't need to vary to cope with the
* presence / absence of cache. You'll of course want to switch to a suitable persistent cache in projects where
* you actually want to cache in production.
*
* @return ArrayCache
*/
public static function buildDataCache()
public static function buildDataCache(): CacheItemPoolInterface
{
return new ArrayCache;
return new ArrayAdapter();
}

/**
* The compiler cache is used for metadata and query compilation caching
*
* By default it uses ArrayCache in local development (to ensure changes are picked up live) and
* By default it uses ArrayAdapter in local development (to ensure changes are picked up live) and
* Apcu in all other environments. Note that this cache is tied to the codebase deployed, so can
* and should be separate on all instances in a cluster, there's no need to swap out for a shared
* (e.g. memcached) cache when scaling.
*
* @return ApcuCache|ArrayCache
*/
public static function buildCompilerCache()
public static function buildCompilerCache(): CacheItemPoolInterface
{
return \Kohana::$environment === \Kohana::DEVELOPMENT ? new ArrayCache : new ApcuCache;
return match (\Kohana::$environment) {
Kohana::DEVELOPMENT => new ArrayAdapter(),
default => new ApcuAdapter('doctrine-meta')
};
}

}
}
21 changes: 8 additions & 13 deletions src/Dependency/DoctrineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Ingenerator\KohanaDoctrine\Dependency;


use Doctrine\Common\Cache\Cache;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Ingenerator\KohanaDoctrine\ExplicitClasslistAttributeDriver;
use Psr\Cache\CacheItemPoolInterface;

class DoctrineFactory
{
Expand Down Expand Up @@ -187,19 +187,14 @@ public static function buildEntityManager(
/**
* Creates and configures the ORM config
*
* @param MappingDriver $meta_driver
* @param Cache $compiler_cache
* @param Cache $data_cache
* @param array|NULL $config
*
* @return Configuration
* @throws \Doctrine\DBAL\DBALException
*/
public static function buildORMConfig(
MappingDriver $meta_driver,
Cache $compiler_cache,
Cache $data_cache,
array $config = NULL
CacheItemPoolInterface $compiler_cache,
CacheItemPoolInterface $data_cache,
?array $config = NULL
) {
$config = \array_merge(
[
Expand All @@ -208,15 +203,15 @@ public static function buildORMConfig(
'proxy_namespace' => 'DoctrineEntityProxy',
'custom_types' => [],
],
$config ?: []
$config ?? []
);
$orm_cfg = new \Doctrine\ORM\Configuration;
$orm_cfg->setMetadataDriverImpl($meta_driver);

// Configure caches
$orm_cfg->setMetadataCacheImpl($compiler_cache);
$orm_cfg->setQueryCacheImpl($compiler_cache);
$orm_cfg->setResultCacheImpl($data_cache);
$orm_cfg->setMetadataCache($compiler_cache);
$orm_cfg->setQueryCache($compiler_cache);
$orm_cfg->setResultCache($data_cache);

// Configure proxy generation
$orm_cfg->setProxyDir($config['proxy_dir']);
Expand Down
26 changes: 13 additions & 13 deletions test/integration/Dependency/DoctrineFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
namespace test\integration\Ingenerator\KohanaDoctrine\Dependency;


use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\EventManager;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Proxy\AbstractProxyFactory;
Expand All @@ -22,6 +19,9 @@
use Ingenerator\KohanaDoctrine\Dependency\DoctrineFactory;
use Ingenerator\KohanaDoctrine\ExplicitClasslistAttributeDriver;
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

class DoctrineFactoryTest extends TestCase
{
Expand Down Expand Up @@ -49,8 +49,8 @@ protected function tearDown(): void
public function provider_expected_services()
{
return [
['doctrine.cache.data_cache', Cache::class, TRUE],
['doctrine.cache.compiler_cache', Cache::class, TRUE],
['doctrine.cache.data_cache', CacheItemPoolInterface::class, TRUE],
['doctrine.cache.compiler_cache', CacheItemPoolInterface::class, TRUE],
['doctrine.config.connection_config', ConnectionConfigProvider::class, FALSE],
['doctrine.config.metadata.driver', ExplicitClasslistAttributeDriver::class, TRUE],
['doctrine.config.orm_config', Configuration::class, TRUE],
Expand Down Expand Up @@ -89,9 +89,9 @@ public function test_it_can_configure_all_published_services(
public function provider_expected_compiler_cache()
{
return [
[\Kohana::DEVELOPMENT, ArrayCache::class],
[\Kohana::STAGING, ApcuCache::class],
[\Kohana::PRODUCTION, ApcuCache::class],
[\Kohana::DEVELOPMENT, ArrayAdapter::class],
[\Kohana::STAGING, ApcuAdapter::class],
[\Kohana::PRODUCTION, ApcuAdapter::class],
];
}

Expand All @@ -108,12 +108,12 @@ public function test_it_uses_apcu_compiler_cache_by_default_or_array_cache_in_de
/** @var Configuration $config */
$this->assertSame(
$cache,
$config->getMetadataCacheImpl(),
$config->getMetadataCache(),
'Should use compiler cache for metadata'
);
$this->assertSame(
$cache,
$config->getQueryCacheImpl(),
$config->getQueryCache(),
'Should use compiler cache for parsed queries'
);
}
Expand All @@ -134,16 +134,16 @@ public function test_it_uses_array_cache_for_data_cache_by_default_in_all_enviro
\Kohana::$environment = $env;
$container = $this->newContainer(DoctrineFactory::definitions());
$cache = $container->get('doctrine.cache.data_cache');
$this->assertInstanceOf(ArrayCache::class, $cache);
$this->assertInstanceOf(ArrayAdapter::class, $cache);
$config = $container->get('doctrine.config.orm_config');
/** @var Configuration $config */
$this->assertSame(
$cache,
$config->getResultCacheImpl(),
$config->getResultCache(),
'Should use data cache for result caching'
);
$this->assertNull(
$config->getHydrationCacheImpl(),
$config->getHydrationCache(),
'Should not assign hydration cache by default'
);
}
Expand Down

0 comments on commit b45dc5f

Please sign in to comment.