From b37d2bc462af918276111d0439592aa445ac6277 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 30 Nov 2012 02:19:35 +0200 Subject: Add CI_Output::delete_cache() (an improved version of PR #609) --- system/core/Output.php | 39 +++++++++++++++++++++++++++++++ user_guide_src/source/changelog.rst | 4 ++-- user_guide_src/source/general/caching.rst | 12 ++++++++-- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/system/core/Output.php b/system/core/Output.php index 7bfde07c7..98deff55c 100644 --- a/system/core/Output.php +++ b/system/core/Output.php @@ -626,6 +626,45 @@ class CI_Output { // -------------------------------------------------------------------- + /** + * Delete cache + * + * @param string $uri URI string + * @return bool + */ + public function delete_cache($uri = '') + { + $CI =& get_instance(); + $cache_path = $CI->config->item('cache_path'); + if ($cache_path === '') + { + $cache_path = APPPATH.'cache/'; + } + + if ( ! is_dir($cache_path)) + { + log_message('error', 'Unable to find cache path: '.$cache_path); + return FALSE; + } + + if (empty($uri)) + { + $uri = $CI->uri->uri_string(); + } + + $cache_path .= md5($CI->config->item('base_url').$CI->config->item('index_page').$uri); + + if ( ! @unlink($cache_path)) + { + log_message('error', 'Unable to delete cache file for '.$uri); + return FALSE; + } + + return TRUE; + } + + // -------------------------------------------------------------------- + /** * Set Cache Header * diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 09c425d0a..6570eb790 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -295,8 +295,8 @@ Release Date: Not Released - Renamed method ``_call_hook()`` to ``call_hook()`` in the :doc:`Hooks Library `. - :doc:`Output Library ` changes include: - Added a second argument to method ``set_content_type()`` that allows setting the document charset as well. - - Added method ``get_content_type()``. - - Added method ``get_header()``. + - Added methods ``get_content_type()`` and ``get_header()``. + - Added method ``delete_cache()``. - ``$config['time_reference']`` now supports all timezone strings supported by PHP. - :doc:`Config Library ` changes include: - Changed ``site_url()`` method to accept an array as well. diff --git a/user_guide_src/source/general/caching.rst b/user_guide_src/source/general/caching.rst index c40f652ac..48385d6c9 100644 --- a/user_guide_src/source/general/caching.rst +++ b/user_guide_src/source/general/caching.rst @@ -56,5 +56,13 @@ If you no longer wish to cache a file you can remove the caching tag and it will no longer be refreshed when it expires. .. note:: Removing the tag will not delete the cache immediately. It will - have to expire normally. If you need to remove it earlier you - will need to manually delete it from your cache directory. \ No newline at end of file + have to expire normally. + +If you need to manually delete the cache, you can use the ``delete_cache()`` +method:: + + // Deletes cache for the currently requested URI + $this->output->delete_cache(); + + // Deletes cache for /foo/bar + $this->output->delete_cache('/foo/bar'); \ No newline at end of file -- cgit v1.2.3-24-g4f1b