1212namespace CodeIgniter ;
1313
1414use Closure ;
15+ use CodeIgniter \Cache \ResponseCache ;
1516use CodeIgniter \Debug \Timer ;
1617use CodeIgniter \Events \Events ;
1718use CodeIgniter \Exceptions \FrameworkException ;
@@ -84,7 +85,7 @@ class CodeIgniter
8485 /**
8586 * Current request.
8687 *
87- * @var CLIRequest|IncomingRequest|Request| null
88+ * @var CLIRequest|IncomingRequest|null
8889 */
8990 protected $ request ;
9091
@@ -127,6 +128,8 @@ class CodeIgniter
127128 * Cache expiration time
128129 *
129130 * @var int seconds
131+ *
132+ * @deprecated 4.4.0 Moved to ResponseCache::$ttl. No longer used.
130133 */
131134 protected static $ cacheTTL = 0 ;
132135
@@ -175,13 +178,20 @@ class CodeIgniter
175178 */
176179 protected int $ bufferLevel ;
177180
181+ /**
182+ * Web Page Caching
183+ */
184+ protected ResponseCache $ pageCache ;
185+
178186 /**
179187 * Constructor.
180188 */
181189 public function __construct (App $ config )
182190 {
183191 $ this ->startTime = microtime (true );
184192 $ this ->config = $ config ;
193+
194+ $ this ->pageCache = Services::responsecache ();
185195 }
186196
187197 /**
@@ -330,7 +340,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
330340 );
331341 }
332342
333- static :: $ cacheTTL = 0 ;
343+ $ this -> pageCache -> setTtl ( 0 ) ;
334344 $ this ->bufferLevel = ob_get_level ();
335345
336346 $ this ->startBenchmark ();
@@ -463,7 +473,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
463473 return $ possibleResponse ;
464474 }
465475
466- if ($ possibleResponse instanceof Request ) {
476+ if ($ possibleResponse instanceof IncomingRequest || $ possibleResponse instanceof CLIRequest ) {
467477 $ this ->request = $ possibleResponse ;
468478 }
469479 }
@@ -517,9 +527,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
517527 // Cache it without the performance metrics replaced
518528 // so that we can have live speed updates along the way.
519529 // Must be run after filters to preserve the Response headers.
520- if (static ::$ cacheTTL > 0 ) {
521- $ this ->cachePage ($ cacheConfig );
522- }
530+ $ this ->pageCache ->make ($ this ->request , $ this ->response );
523531
524532 // Update the performance metrics
525533 $ body = $ this ->response ->getBody ();
@@ -603,9 +611,11 @@ protected function startBenchmark()
603611 * Sets a Request object to be used for this request.
604612 * Used when running certain tests.
605613 *
614+ * @param CLIRequest|IncomingRequest $request
615+ *
606616 * @return $this
607617 */
608- public function setRequest (Request $ request )
618+ public function setRequest ($ request )
609619 {
610620 $ this ->request = $ request ;
611621
@@ -674,27 +684,11 @@ protected function forceSecureAccess($duration = 31_536_000)
674684 */
675685 public function displayCache (Cache $ config )
676686 {
677- if ($ cachedResponse = cache ()->get ($ this ->generateCacheName ($ config ))) {
678- $ cachedResponse = unserialize ($ cachedResponse );
679- if (! is_array ($ cachedResponse ) || ! isset ($ cachedResponse ['output ' ]) || ! isset ($ cachedResponse ['headers ' ])) {
680- throw new Exception ('Error unserializing page cache ' );
681- }
682-
683- $ headers = $ cachedResponse ['headers ' ];
684- $ output = $ cachedResponse ['output ' ];
685-
686- // Clear all default headers
687- foreach (array_keys ($ this ->response ->headers ()) as $ key ) {
688- $ this ->response ->removeHeader ($ key );
689- }
690-
691- // Set cached headers
692- foreach ($ headers as $ name => $ value ) {
693- $ this ->response ->setHeader ($ name , $ value );
694- }
687+ if ($ cachedResponse = $ this ->pageCache ->get ($ this ->request , $ this ->response )) {
688+ $ this ->response = $ cachedResponse ;
695689
696690 $ this ->totalTime = $ this ->benchmark ->getElapsedTime ('total_execution ' );
697- $ output = $ this ->displayPerformanceMetrics ($ output );
691+ $ output = $ this ->displayPerformanceMetrics ($ cachedResponse -> getBody () );
698692 $ this ->response ->setBody ($ output );
699693
700694 return $ this ->response ;
@@ -705,6 +699,8 @@ public function displayCache(Cache $config)
705699
706700 /**
707701 * Tells the app that the final output should be cached.
702+ *
703+ * @deprecated 4.4.0 Moved to ResponseCache::setTtl(). to No longer used.
708704 */
709705 public static function cache (int $ time )
710706 {
@@ -716,6 +712,8 @@ public static function cache(int $time)
716712 * full-page caching for very high performance.
717713 *
718714 * @return bool
715+ *
716+ * @deprecated 4.4.0 No longer used.
719717 */
720718 public function cachePage (Cache $ config )
721719 {
@@ -741,6 +739,8 @@ public function getPerformanceStats(): array
741739
742740 /**
743741 * Generates the cache name to use for our full-page caching.
742+ *
743+ * @deprecated 4.4.0 No longer used.
744744 */
745745 protected function generateCacheName (Cache $ config ): string
746746 {
0 commit comments