22
33namespace OpenClassrooms \UseCase \Application \Services \Proxy \Strategies \Impl \Cache ;
44
5- use OpenClassrooms \Cache \Cache \Cache ;
65use OpenClassrooms \UseCase \Application \Services \Proxy \Strategies \Impl \DTO \ProxyStrategyResponseDTO ;
76use OpenClassrooms \UseCase \Application \Services \Proxy \Strategies \Requestors \Cache \CacheProxyStrategyRequest ;
87use OpenClassrooms \UseCase \Application \Services \Proxy \Strategies \Requestors \PostExecuteProxyStrategy ;
98use OpenClassrooms \UseCase \Application \Services \Proxy \Strategies \Requestors \PreExecuteProxyStrategy ;
109use OpenClassrooms \UseCase \Application \Services \Proxy \Strategies \Requestors \ProxyStrategy ;
1110use OpenClassrooms \UseCase \Application \Services \Proxy \Strategies \Requestors \ProxyStrategyRequest ;
1211use OpenClassrooms \UseCase \Application \Services \Proxy \Strategies \Responders \ProxyStrategyResponse ;
12+ use Psr \Cache \CacheItemInterface ;
13+ use Psr \Cache \CacheItemPoolInterface ;
1314
1415/**
1516 * @author Romain Kuzniak <romain.kuzniak@turn-it-up.org>
1617 */
1718class CacheProxyStrategy implements PreExecuteProxyStrategy, PostExecuteProxyStrategy
1819{
19-
20- /**
21- * @var mixed
22- */
23- private $ data ;
24-
2520 /**
26- * @var Cache
21+ * @var CacheItemPoolInterface
2722 */
2823 private $ cache ;
2924
3025 /**
3126 * @var bool
3227 */
33- private $ postExecute = true ;
28+ private $ postExecute ;
3429
3530 /**
3631 * @return string
@@ -46,28 +41,14 @@ public function getType()
4641 public function preExecute (ProxyStrategyRequest $ proxyStrategyRequest )
4742 {
4843 /** @var CacheProxyStrategyRequest $proxyStrategyRequest */
49- $ this -> data = $ this -> cache ->fetchWithNamespace (
44+ $ item = $ this ->fetchWithNamespace (
5045 $ proxyStrategyRequest ->getId (),
5146 $ proxyStrategyRequest ->getNamespaceId ()
5247 );
5348
54- if ($ this ->responseIsInCache ()) {
55- $ stopExecution = true ;
56- $ this ->postExecute = false ;
57- } else {
58- $ stopExecution = false ;
59- }
60- $ response = new ProxyStrategyResponseDTO ($ this ->data , $ stopExecution );
61-
62- return $ response ;
63- }
49+ $ this ->postExecute = !$ item ->isHit ();
6450
65- /**
66- * @return bool
67- */
68- private function responseIsInCache ()
69- {
70- return $ this ->data ;
51+ return new ProxyStrategyResponseDTO ($ item ->get (), $ item ->isHit ());
7152 }
7253
7354 /**
@@ -76,17 +57,51 @@ private function responseIsInCache()
7657 public function postExecute (ProxyStrategyRequest $ proxyStrategyRequest )
7758 {
7859 /** @var CacheProxyStrategyRequest $proxyStrategyRequest */
79- $ saved = $ this -> cache ->saveWithNamespace (
60+ $ item = $ this ->saveWithNamespace (
8061 $ proxyStrategyRequest ->getId (),
8162 $ proxyStrategyRequest ->getData (),
8263 $ proxyStrategyRequest ->getNamespaceId (),
8364 $ proxyStrategyRequest ->getLifeTime ()
8465 );
85- $ response = new ProxyStrategyResponseDTO ($ saved , false );
8666
87- return $ response ;
67+ return new ProxyStrategyResponseDTO ( $ item -> get (), false ) ;
8868 }
8969
70+ private function fetchWithNamespace (string $ id , string $ namespace = null ): CacheItemInterface
71+ {
72+ if ($ namespace !== null ) {
73+ $ namespaceId = $ this ->cache ->getItem ($ namespace );
74+
75+ $ id = ((string ) $ namespaceId ->get ()) . $ id ;
76+ }
77+
78+ return $ this ->cache ->getItem ($ id );
79+ }
80+
81+ private function saveWithNamespace (string $ id , mixed $ data , string $ namespace = null , int $ lifetime = null ): CacheItemInterface
82+ {
83+ if ($ namespace !== null ) {
84+ $ namespaceId = $ this ->cache ->getItem ($ namespace );
85+
86+ if (!$ namespaceId ->isHit ()) {
87+ $ namespaceId ->set ($ namespace . '_ ' . random_int (0 , 10000 ));
88+ $ namespaceId ->expiresAfter (604800 ); // 7 days
89+
90+ $ this ->cache ->save ($ namespaceId );
91+ }
92+
93+ $ id = ((string ) $ namespaceId ->get ()) . $ id ;
94+ }
95+
96+ $ item = $ this ->cache ->getItem ($ id );
97+ $ item ->set ($ data )->expiresAfter ($ lifetime );
98+
99+ $ this ->cache ->save ($ item );
100+
101+ return $ item ;
102+ }
103+
104+
90105 /**
91106 * @return boolean
92107 */
@@ -95,7 +110,7 @@ public function isPostExecute()
95110 return $ this ->postExecute ;
96111 }
97112
98- public function setCache (Cache $ cache )
113+ public function setCache (CacheItemPoolInterface $ cache )
99114 {
100115 $ this ->cache = $ cache ;
101116 }
0 commit comments