77use Doctrine \Common \Annotations \AnnotationReader ;
88use Doctrine \Common \Annotations \Reader ;
99use Psr \Cache \CacheItemPoolInterface ;
10+ use Psr \Container \ContainerExceptionInterface ;
11+ use Psr \Container \ContainerInterface ;
12+ use Psr \Container \NotFoundExceptionInterface ;
1013
1114trait CacheableClassTrait
1215{
1316 /**
1417 * Long name to avoid collision
15- * @var CacheItemPoolInterface
18+ * @var ContainerInterface
1619 */
17- protected $ cacheServiceForMethod ;
20+ protected $ serviceLocatorCache ;
1821
1922 /**
2023 * Long name to avoid colision
@@ -24,11 +27,11 @@ trait CacheableClassTrait
2427 protected $ readerForCacheMethod ;
2528
2629 /**
27- * @param CacheItemPoolInterface $cacheServiceForMethod
30+ * @param ContainerInterface $serviceLocatorCache
2831 */
29- public function setCacheServiceForMethod ( CacheItemPoolInterface $ cacheServiceForMethod )
32+ public function setServiceLocatorCache ( ContainerInterface $ serviceLocatorCache )
3033 {
31- $ this ->cacheServiceForMethod = $ cacheServiceForMethod ;
34+ $ this ->serviceLocatorCache = $ serviceLocatorCache ;
3235 }
3336
3437 /**
@@ -39,6 +42,13 @@ public function setReaderForCacheMethod(Reader $readerForCacheMethod)
3942 $ this ->readerForCacheMethod = $ readerForCacheMethod ;
4043 }
4144
45+ /**
46+ * @param \ReflectionMethod $method
47+ * @param $params
48+ * @return mixed
49+ * @throws CacheException
50+ * @throws \Psr\Cache\InvalidArgumentException
51+ */
4252 public function getCached (\ReflectionMethod $ method , $ params )
4353 {
4454 $ method ->setAccessible (true );
@@ -47,7 +57,15 @@ public function getCached(\ReflectionMethod $method, $params)
4757
4858 $ cacheKey = $ this ->getCacheKey ($ method , $ params , $ annotation );
4959
50- $ cacheItem = $ this ->cacheServiceForMethod ->getItem ($ cacheKey );
60+ try {
61+ $ cacheItemPool = $ this ->getCacheService ($ annotation ->getStorage ());
62+ } catch (NotFoundExceptionInterface $ e ) {
63+ throw new CacheException ('Requested cache service not found ' , $ e ->getCode (), $ e );
64+ } catch (ContainerExceptionInterface $ e ) {
65+ throw new CacheException ($ e ->getMessage (), $ e ->getCode (), $ e );
66+ }
67+
68+ $ cacheItem = $ cacheItemPool ->getItem ($ cacheKey );
5169
5270 if ($ cacheItem ->isHit () && !$ annotation ->isReset ()) {
5371 return $ cacheItem ->get ();
@@ -57,7 +75,7 @@ public function getCached(\ReflectionMethod $method, $params)
5775
5876 $ cacheItem ->set ($ result );
5977 $ cacheItem ->expiresAfter ($ annotation ->getTtl ());
60- $ this -> cacheServiceForMethod ->save ($ cacheItem );
78+ $ cacheItemPool ->save ($ cacheItem );
6179
6280 return $ result ;
6381 }
@@ -120,4 +138,15 @@ protected function getCacheKey(\ReflectionMethod $method, $params, Cache $cacheO
120138
121139 return $ cacheKey ;
122140 }
141+
142+ /**
143+ * @param string $label
144+ * @return CacheItemPoolInterface
145+ * @throws \Psr\Container\ContainerExceptionInterface
146+ * @throws \Psr\Container\NotFoundExceptionInterface
147+ */
148+ protected function getCacheService (string $ label ): CacheItemPoolInterface
149+ {
150+ return $ this ->serviceLocatorCache ->get ($ label );
151+ }
123152}
0 commit comments