Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
wants to merge 2 commits into from

3 participants

@appleboy

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');
}
@ckdarby

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

@ckdarby

@narfbg Do you think this functionality should be added?

@narfbg
Owner

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

@ckdarby

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.

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

See the above commit.

@narfbg narfbg closed this
@nonchip nonchip referenced this pull request from a commit in nonchip/CodeIgniter
@narfbg 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 with 71 additions and 18 deletions.
  1. +71 −18 system/core/Output.php
View
89 system/core/Output.php
@@ -5,9 +5,9 @@
* An open source application development framework for PHP 5.1.6 or newer
*
* NOTICE OF LICENSE
- *
+ *
* Licensed under the Open Software License version 3.0
- *
+ *
* This source file is subject to the Open Software License (OSL 3.0) that is
* bundled with this package in the files license.txt / license.rst. It is
* also available through the world wide web at this URL:
@@ -101,14 +101,14 @@ class CI_Output {
* Constructor
*
*/
- function __construct()
+ public function __construct()
{
$this->_zlib_oc = @ini_get('zlib.output_compression');
// Get mime types for later
if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
{
- include APPPATH.'config/'.ENVIRONMENT.'/mimes.php';
+ include APPPATH.'config/'.ENVIRONMENT.'/mimes.php';
}
else
{
@@ -131,7 +131,7 @@ function __construct()
* @access public
* @return string
*/
- function get_output()
+ public function get_output()
{
return $this->final_output;
}
@@ -147,7 +147,7 @@ function get_output()
* @param string
* @return void
*/
- function set_output($output)
+ public function set_output($output)
{
$this->final_output = $output;
@@ -165,7 +165,7 @@ function set_output($output)
* @param string
* @return void
*/
- function append_output($output)
+ public function append_output($output)
{
if ($this->final_output == '')
{
@@ -194,7 +194,7 @@ function append_output($output)
* @param bool
* @return void
*/
- function set_header($header, $replace = TRUE)
+ public function set_header($header, $replace = TRUE)
{
// If zlib.output_compression is enabled it will compress the output,
// but it will not modify the content-length header to compensate for
@@ -220,7 +220,7 @@ function set_header($header, $replace = TRUE)
* @param string extension of the file we're outputting
* @return void
*/
- function set_content_type($mime_type)
+ public function set_content_type($mime_type)
{
if (strpos($mime_type, '/') === FALSE)
{
@@ -256,7 +256,7 @@ function set_content_type($mime_type)
* @param string
* @return void
*/
- function set_status_header($code = 200, $text = '')
+ public function set_status_header($code = 200, $text = '')
{
set_status_header($code, $text);
@@ -272,7 +272,7 @@ function set_status_header($code = 200, $text = '')
* @param bool
* @return void
*/
- function enable_profiler($val = TRUE)
+ public function enable_profiler($val = TRUE)
{
$this->enable_profiler = (is_bool($val)) ? $val : TRUE;
@@ -290,7 +290,7 @@ function enable_profiler($val = TRUE)
* @param array
* @return void
*/
- function set_profiler_sections($sections)
+ public function set_profiler_sections($sections)
{
foreach ($sections as $section => $enable)
{
@@ -309,13 +309,26 @@ function set_profiler_sections($sections)
* @param integer
* @return void
*/
- function cache($time)
+ public function cache($time)
{
$this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;
return $this;
}
+ /**
+ * Delete Cache
+ *
+ * @access public
+ * @param string
+ * @return bool
+ */
+ public function delete_cache($url = "")
+ {
+ $url = trim($url, "/");
+ $this->_delete_cache($url);
+ }
+
// --------------------------------------------------------------------
/**
@@ -333,7 +346,7 @@ function cache($time)
* @param string
* @return mixed
*/
- function _display($output = '')
+ public function _display($output = '')
{
// Note: We use globals because we can't use $CI =& get_instance()
// since this function is sometimes called by the caching mechanism,
@@ -466,11 +479,11 @@ function _display($output = '')
/**
* Write a Cache File
*
- * @access public
+ * @access protected
* @param string
* @return void
*/
- function _write_cache($output)
+ protected function _write_cache($output)
{
$CI =& get_instance();
$path = $CI->config->item('cache_path');
@@ -513,6 +526,46 @@ function _write_cache($output)
log_message('debug', "Cache file written: ".$cache_path);
}
+ /**
+ * Delete a Cache File
+ *
+ * @access public
+ * @param string
+ * @return bool
+ */
+ public function _delete_cache($url)
+ {
+ $CI =& get_instance();
+ $path = $CI->config->item('cache_path');
+
+ $cache_path = ($path == '') ? APPPATH.'cache/' : $path;
+
+ $uri = $CI->config->item('base_url').
+ $CI->config->item('index_page').
+ $url;
+
+ $filepath = $cache_path.md5($uri);
+
+ if ( ! @file_exists($filepath))
+ {
+ return FALSE;
+ }
+
+ if ( ! $fp = @fopen($filepath, FOPEN_READ))
+ {
+ return FALSE;
+ }
+
+ if (is_really_writable($cache_path))
+ {
+ @unlink($filepath);
+ log_message('debug', "Cache file has deleted.");
+ return TRUE;
+ }
+
+ return FALSE;
+ }
+
// --------------------------------------------------------------------
/**
@@ -523,7 +576,7 @@ function _write_cache($output)
* @param object uri class
* @return void
*/
- function _display_cache(&$CFG, &$URI)
+ public function _display_cache(&$CFG, &$URI)
{
$cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');
@@ -583,4 +636,4 @@ function _display_cache(&$CFG, &$URI)
// END Output Class
/* End of file Output.php */
-/* Location: ./system/core/Output.php */
+/* Location: ./system/core/Output.php */
Something went wrong with that request. Please try again.