klsoft/yii3-cache-doctrine

The package provides the PSR-16 cache using the Doctrine ORM

Maintainers

👁 klsoft-web

Package info

github.com/klsoft-web/yii3-cache-doctrine

pkg:composer/klsoft/yii3-cache-doctrine

Statistics

Installs: 25

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-04-23 10:49 UTC

Requires

Requires (Dev)

None

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 2f4739eac0ae8d60b5f5602d26a170f2d82878b2

ormcachedoctrinepsr-16yii3

This package is auto-updated.

Last update: 2026-06-23 11:07:47 UTC


README

The package provides the PSR-16 cache using the Doctrine ORM.

Requirement

  • PHP 8.2 or higher.

Installation

composer require klsoft/yii3-cache-doctrine

How to use

1. Configure the EntityManagerInterface.

2. Use the Doctrine console command to create or update the database schema.

Create the database schema:

./yii doctrine:orm:schema-tool:create

Update the database schema:

./yii doctrine:orm:schema-tool:update --force 

3. Configure the CacheInterface.

use Yiisoft\Cache\CacheInterface; 
use Yiisoft\Cache\Cache; 
use Doctrine\ORM\EntityManagerInterface; 
use Klsoft\Yii3CacheDoctrine\DoctrineCache;

return [
 // ...
 CacheInterface::class => static function (ContainerInterface $container) { 
 return new Cache(new DoctrineCache($container->get(EntityManagerInterface::class))); 
 },
];

4. Cache the data.

Example:

namespace MyNamespace;

use Yiisoft\Cache\CacheInterface;

final class ProductRepository implements ProductRepositoryInterface
{
 private const TOP_PRODUCTS = 'top_poroducts';

 public function __construct(
 private CacheInterface $cache,
 private int $cacheDuration)
 {
 }

 public function getTopProducts(int $numberOfTopProducts): array
 {
 return $this->cache->getOrSet(
 ProductRepository::TOP_PRODUCTS,
 function () use ($numberOfTopProducts) {
 return $this->getTopProductsFromDb($numberOfTopProducts);
 },
 $this->cacheDuration);
 }
}