Skip to content
This repository

Add new feature: Output class support delete cache file #609

Closed
wants to merge 2 commits into from

3 participants

Bo-Yi Wu Cory Kennedy-Darby Andrey Andreev
Bo-Yi Wu

Hi,

I add new function of output class. We can delete cache file from application/cache directory anytime.

Example:

If you load welcome controller http://localhost/welcome/index

public function index()
{
    $this->output->cache(60);
    $this->load->view('welcome_message');
}

If the file doesn't expired, we can use the following way to delete it according to URL you load.

public function delete()
{
    $this->output->delete_cache('/welcome/index');
}
Cory Kennedy-Darby

Having a hard time trying to come up with the case where I want to specifically delete my cache before the set time & doing it via the code.

I'd also like to point out that this is way out of date, the spacing is odd & this can be accomplished with MY_Output

Cory Kennedy-Darby

@narfbg Do you think this functionality should be added?

Andrey Andreev
Collaborator

IDK, +0. Deffinately outdated though and I'm wondering why is there a need of both delete_cache() and _delete_cache() methods.

Cory Kennedy-Darby

Generally I think he wanted to make this "delete" section being able to be done via code instead of manually doing it.

I guess I could see the advantages of this if you had a migration script that updated some stuff & you wanted the cached files to be removed to reflect the new data.

Andrey Andreev narfbg referenced this pull request from a commit
Andrey Andreev narfbg Add CI_Output::delete_cache()
(an improved version of PR #609)
b37d2bc
Andrey Andreev
Collaborator

See the above commit.

Kyra Zimmer nonchip referenced this pull request from a commit in nonchip/CodeIgniter
Andrey Andreev narfbg Add CI_Output::delete_cache()
(an improved version of PR #609)
5445ad6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 71 additions and 18 deletions. Show diff stats Hide diff stats

  1. +71 18 system/core/Output.php
89 system/core/Output.php
@@ -5,9 +5,9 @@
5 5 * An open source application development framework for PHP 5.1.6 or newer
6 6 *
7 7 * NOTICE OF LICENSE
8   - *
  8 + *
9 9 * Licensed under the Open Software License version 3.0
10   - *
  10 + *
11 11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 12 * bundled with this package in the files license.txt / license.rst. It is
13 13 * also available through the world wide web at this URL:
@@ -101,14 +101,14 @@ class CI_Output {
101 101 * Constructor
102 102 *
103 103 */
104   - function __construct()
  104 + public function __construct()
105 105 {
106 106 $this->_zlib_oc = @ini_get('zlib.output_compression');
107 107
108 108 // Get mime types for later
109 109 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
110 110 {
111   - include APPPATH.'config/'.ENVIRONMENT.'/mimes.php';
  111 + include APPPATH.'config/'.ENVIRONMENT.'/mimes.php';
112 112 }
113 113 else
114 114 {
@@ -131,7 +131,7 @@ function __construct()
131 131 * @access public
132 132 * @return string
133 133 */
134   - function get_output()
  134 + public function get_output()
135 135 {
136 136 return $this->final_output;
137 137 }
@@ -147,7 +147,7 @@ function get_output()
147 147 * @param string
148 148 * @return void
149 149 */
150   - function set_output($output)
  150 + public function set_output($output)
151 151 {
152 152 $this->final_output = $output;
153 153
@@ -165,7 +165,7 @@ function set_output($output)
165 165 * @param string
166 166 * @return void
167 167 */
168   - function append_output($output)
  168 + public function append_output($output)
169 169 {
170 170 if ($this->final_output == '')
171 171 {
@@ -194,7 +194,7 @@ function append_output($output)
194 194 * @param bool
195 195 * @return void
196 196 */
197   - function set_header($header, $replace = TRUE)
  197 + public function set_header($header, $replace = TRUE)
198 198 {
199 199 // If zlib.output_compression is enabled it will compress the output,
200 200 // but it will not modify the content-length header to compensate for
@@ -220,7 +220,7 @@ function set_header($header, $replace = TRUE)
220 220 * @param string extension of the file we're outputting
221 221 * @return void
222 222 */
223   - function set_content_type($mime_type)
  223 + public function set_content_type($mime_type)
224 224 {
225 225 if (strpos($mime_type, '/') === FALSE)
226 226 {
@@ -256,7 +256,7 @@ function set_content_type($mime_type)
256 256 * @param string
257 257 * @return void
258 258 */
259   - function set_status_header($code = 200, $text = '')
  259 + public function set_status_header($code = 200, $text = '')
260 260 {
261 261 set_status_header($code, $text);
262 262
@@ -272,7 +272,7 @@ function set_status_header($code = 200, $text = '')
272 272 * @param bool
273 273 * @return void
274 274 */
275   - function enable_profiler($val = TRUE)
  275 + public function enable_profiler($val = TRUE)
276 276 {
277 277 $this->enable_profiler = (is_bool($val)) ? $val : TRUE;
278 278
@@ -290,7 +290,7 @@ function enable_profiler($val = TRUE)
290 290 * @param array
291 291 * @return void
292 292 */
293   - function set_profiler_sections($sections)
  293 + public function set_profiler_sections($sections)
294 294 {
295 295 foreach ($sections as $section => $enable)
296 296 {
@@ -309,13 +309,26 @@ function set_profiler_sections($sections)
309 309 * @param integer
310 310 * @return void
311 311 */
312   - function cache($time)
  312 + public function cache($time)
313 313 {
314 314 $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
315 315
316 316 return $this;
317 317 }
318 318
  319 + /**
  320 + * Delete Cache
  321 + *
  322 + * @access public
  323 + * @param string
  324 + * @return bool
  325 + */
  326 + public function delete_cache($url = "")
  327 + {
  328 + $url = trim($url, "/");
  329 + $this->_delete_cache($url);
  330 + }
  331 +
319 332 // --------------------------------------------------------------------
320 333
321 334 /**
@@ -333,7 +346,7 @@ function cache($time)
333 346 * @param string
334 347 * @return mixed
335 348 */
336   - function _display($output = '')
  349 + public function _display($output = '')
337 350 {
338 351 // Note: We use globals because we can't use $CI =& get_instance()
339 352 // since this function is sometimes called by the caching mechanism,
@@ -466,11 +479,11 @@ function _display($output = '')
466 479 /**
467 480 * Write a Cache File
468 481 *
469   - * @access public
  482 + * @access protected
470 483 * @param string
471 484 * @return void
472 485 */
473   - function _write_cache($output)
  486 + protected function _write_cache($output)
474 487 {
475 488 $CI =& get_instance();
476 489 $path = $CI->config->item('cache_path');
@@ -513,6 +526,46 @@ function _write_cache($output)
513 526 log_message('debug', "Cache file written: ".$cache_path);
514 527 }
515 528
  529 + /**
  530 + * Delete a Cache File
  531 + *
  532 + * @access public
  533 + * @param string
  534 + * @return bool
  535 + */
  536 + public function _delete_cache($url)
  537 + {
  538 + $CI =& get_instance();
  539 + $path = $CI->config->item('cache_path');
  540 +
  541 + $cache_path = ($path == '') ? APPPATH.'cache/' : $path;
  542 +
  543 + $uri = $CI->config->item('base_url').
  544 + $CI->config->item('index_page').
  545 + $url;
  546 +
  547 + $filepath = $cache_path.md5($uri);
  548 +
  549 + if ( ! @file_exists($filepath))
  550 + {
  551 + return FALSE;
  552 + }
  553 +
  554 + if ( ! $fp = @fopen($filepath, FOPEN_READ))
  555 + {
  556 + return FALSE;
  557 + }
  558 +
  559 + if (is_really_writable($cache_path))
  560 + {
  561 + @unlink($filepath);
  562 + log_message('debug', "Cache file has deleted.");
  563 + return TRUE;
  564 + }
  565 +
  566 + return FALSE;
  567 + }
  568 +
516 569 // --------------------------------------------------------------------
517 570
518 571 /**
@@ -523,7 +576,7 @@ function _write_cache($output)
523 576 * @param object uri class
524 577 * @return void
525 578 */
526   - function _display_cache(&$CFG, &$URI)
  579 + public function _display_cache(&$CFG, &$URI)
527 580 {
528 581 $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
529 582
@@ -583,4 +636,4 @@ function _display_cache(&$CFG, &$URI)
583 636 // END Output Class
584 637
585 638 /* End of file Output.php */
586   -/* Location: ./system/core/Output.php */
  639 +/* Location: ./system/core/Output.php */

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.