44
55namespace  Doctrine \ORM \Cache ;
66
7- use  Doctrine \Common \Cache \Cache  as  CacheAdapter ;
8- use  Doctrine \Common \Cache \CacheProvider ;
9- use  Doctrine \Common \ Cache \ MultiGetCache ;
7+ use  Doctrine \Common \Cache \Cache  as  LegacyCache ;
8+ use  Doctrine \Common \Cache \Psr6 \ CacheAdapter ;
9+ use  Doctrine \Deprecations \ Deprecation ;
1010use  Doctrine \ORM \Cache ;
1111use  Doctrine \ORM \Cache \Persister \Collection \NonStrictReadWriteCachedCollectionPersister ;
1212use  Doctrine \ORM \Cache \Persister \Collection \ReadOnlyCachedCollectionPersister ;
1313use  Doctrine \ORM \Cache \Persister \Collection \ReadWriteCachedCollectionPersister ;
1414use  Doctrine \ORM \Cache \Persister \Entity \NonStrictReadWriteCachedEntityPersister ;
1515use  Doctrine \ORM \Cache \Persister \Entity \ReadOnlyCachedEntityPersister ;
1616use  Doctrine \ORM \Cache \Persister \Entity \ReadWriteCachedEntityPersister ;
17- use  Doctrine \ORM \Cache \Region \DefaultMultiGetRegion ;
1817use  Doctrine \ORM \Cache \Region \DefaultRegion ;
1918use  Doctrine \ORM \Cache \Region \FileLockRegion ;
2019use  Doctrine \ORM \Cache \Region \UpdateTimestampCache ;
2423use  Doctrine \ORM \Persisters \Entity \EntityPersister ;
2524use  InvalidArgumentException ;
2625use  LogicException ;
26+ use  Psr \Cache \CacheItemPoolInterface ;
27+ use  TypeError ;
2728
2829use  function  assert ;
30+ use  function  get_debug_type ;
2931use  function  sprintf ;
3032
3133use  const  DIRECTORY_SEPARATOR ;
3234
3335class  DefaultCacheFactory implements  CacheFactory
3436{
35-     /** @var CacheAdapter  */ 
36-     private  $ cache  ;
37+     /** @var CacheItemPoolInterface  */ 
38+     private  $ cacheItemPool  ;
3739
3840    /** @var RegionsConfiguration */ 
3941    private  $ regionsConfig ;
@@ -47,9 +49,33 @@ class DefaultCacheFactory implements CacheFactory
4749    /** @var string|null */ 
4850    private  $ fileLockRegionDirectory ;
4951
50-     public  function  __construct (RegionsConfiguration   $ cacheConfig , CacheAdapter   $ cache )
52+     /** 
53+      * @param CacheItemPoolInterface $cacheItemPool 
54+      */ 
55+     public  function  __construct (RegionsConfiguration   $ cacheConfig , $ cacheItemPool )
5156    {
52-         $ this  ->cache          = $ cache ;
57+         if  ($ cacheItemPool  instanceof  LegacyCache) {
58+             Deprecation::trigger (
59+                 'doctrine/orm ' ,
60+                 'https://github.com/doctrine/orm/pull/9322 ' ,
61+                 'Passing an instance of %s to %s is deprecated, pass a %s instead. ' ,
62+                 get_debug_type ($ cacheItemPool ),
63+                 __METHOD__ ,
64+                 CacheItemPoolInterface::class
65+             );
66+ 
67+             $ this  ->cacheItemPool  = CacheAdapter::wrap ($ cacheItemPool );
68+         } elseif  (! $ cacheItemPool  instanceof  CacheItemPoolInterface) {
69+             throw  new  TypeError (sprintf (
70+                 '%s: Parameter #2 is expected to be an instance of %s, got %s. ' ,
71+                 __METHOD__ ,
72+                 CacheItemPoolInterface::class,
73+                 get_debug_type ($ cacheItemPool )
74+             ));
75+         } else  {
76+             $ this  ->cacheItemPool  = $ cacheItemPool ;
77+         }
78+ 
5379        $ this  ->regionsConfig  = $ cacheConfig ;
5480    }
5581
@@ -183,13 +209,9 @@ public function getRegion(array $cache)
183209            return  $ this  ->regions [$ cache ['region ' ]];
184210        }
185211
186-         $ name          = $ cache ['region ' ];
187-         $ cacheAdapter  = $ this  ->createRegionCache ($ name );
188-         $ lifetime      = $ this  ->regionsConfig ->getLifetime ($ cache ['region ' ]);
189- 
190-         $ region  = $ cacheAdapter  instanceof  MultiGetCache
191-             ? new  DefaultMultiGetRegion ($ name , $ cacheAdapter , $ lifetime )
192-             : new  DefaultRegion ($ name , $ cacheAdapter , $ lifetime );
212+         $ name      = $ cache ['region ' ];
213+         $ lifetime  = $ this  ->regionsConfig ->getLifetime ($ cache ['region ' ]);
214+         $ region    = new  DefaultRegion ($ name , $ this  ->cacheItemPool , $ lifetime );
193215
194216        if  ($ cache ['usage ' ] === ClassMetadata::CACHE_USAGE_READ_WRITE ) {
195217            if  (
@@ -209,25 +231,6 @@ public function getRegion(array $cache)
209231        return  $ this  ->regions [$ cache ['region ' ]] = $ region ;
210232    }
211233
212-     private  function  createRegionCache (string  $ name ): CacheAdapter 
213-     {
214-         $ cacheAdapter  = clone  $ this  ->cache ;
215- 
216-         if  (! $ cacheAdapter  instanceof  CacheProvider) {
217-             return  $ cacheAdapter ;
218-         }
219- 
220-         $ namespace  = $ cacheAdapter ->getNamespace ();
221- 
222-         if  ($ namespace  !== '' ) {
223-             $ namespace  .= ': ' ;
224-         }
225- 
226-         $ cacheAdapter ->setNamespace ($ namespace  . $ name );
227- 
228-         return  $ cacheAdapter ;
229-     }
230- 
231234    /** 
232235     * {@inheritdoc} 
233236     */ 
@@ -237,7 +240,7 @@ public function getTimestampRegion()
237240            $ name      = Cache::DEFAULT_TIMESTAMP_REGION_NAME ;
238241            $ lifetime  = $ this  ->regionsConfig ->getLifetime ($ name );
239242
240-             $ this  ->timestampRegion  = new  UpdateTimestampCache ($ name , clone   $ this  ->cache , $ lifetime );
243+             $ this  ->timestampRegion  = new  UpdateTimestampCache ($ name , $ this  ->cacheItemPool , $ lifetime );
241244        }
242245
243246        return  $ this  ->timestampRegion ;
@@ -246,8 +249,8 @@ public function getTimestampRegion()
246249    /** 
247250     * {@inheritdoc} 
248251     */ 
249-     public  function  createCache (EntityManagerInterface   $ em  )
252+     public  function  createCache (EntityManagerInterface   $ entityManager  )
250253    {
251-         return  new  DefaultCache ($ em  );
254+         return  new  DefaultCache ($ entityManager  );
252255    }
253256}
0 commit comments