├── .gitattributes ├── spark.info ├── config └── autoload.php ├── LICENSE.txt ├── README.md └── helpers └── cache_helper.php /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /spark.info: -------------------------------------------------------------------------------- 1 | name: cache-helper 2 | version: 1.4.2 3 | compatibility: 2.1.3 4 | -------------------------------------------------------------------------------- /config/autoload.php: -------------------------------------------------------------------------------- 1 | load->helper('cache'); 29 | ``` 30 | 31 | ### For spark install 32 | 33 | ```php 34 | $this->load->spark('cache-helper/1.4.2'); 35 | ``` 36 | 37 | ## Function reference 38 | 39 | | Function | Return | Description | 40 | | ----- | ----- | ----- | 41 | | `get_cache_folder()` | string | Returns a string containing the path to the cache directory. (e.g. `'/var/www/application/cache'`) | 42 | | `get_cache_file($uri_string)` | string | Returns a string with the path to the cache file for a specific URI. (e.g. `'/var/www/application/cache/d41d8cd98f00b204e9800998ecf8427e'`) This function does not check for the existence of that cache file, it merely tells you where CodeIgniter would cache that resource. | 43 | | `get_all_cache_files()` | array | Returns an object containing information for every file in the cache directory. Uses the `get_dir_file_info()` function from the [File Helper](http://codeigniter.com/user_guide/helpers/file_helper.html). The returned object will be in that format. | 44 | | `delete_cache($uri_string)` | bool | Attempts to delete the cached file for the specified resource. Returns `TRUE` upon success or if that file does not exist, returns `FALSE` on failure. | 45 | | `delete_all_cache()` | void | Attempts to delete ALL cache files. Does not return anything. | 46 | | `delete_expired_cache()` | void | Attempts to delete all cache files which have expired. Does not return anything. | 47 | | `get_cache_expiration($uri_string)` | int | Attempts to read the expiration time stamp from the cache file for the specified resource. Returns an integer with the time stamp (UNIX time stamp in `time()` format, e.g. `1307158229`) if it could find and parse the file, returns `FALSE` if there is no cache file for the resource or if there was a problem reading/parsing the file. | 48 | 49 | **Notes** 50 | 51 | * The `$uri_string` argument is the URI string of the resource you are looking for (e.g. `'blog/article/123'`). This is designed to be used with CodeIgniter's `uri_string()` method from the [URI Class](http://codeigniter.com/user_guide/libraries/uri.html). 52 | 53 | ## License 54 | 55 | *(This project is released under the MIT license.)* 56 | 57 | Copyright (c) 2012 Steven Benner, http://stevenbenner.com/ 58 | 59 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 60 | 61 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 62 | 63 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 64 | -------------------------------------------------------------------------------- /helpers/cache_helper.php: -------------------------------------------------------------------------------- 1 | config->item('cache_path'); 30 | $cache_path = ($path == '') ? APPPATH . 'cache/' : $path; 31 | 32 | return $cache_path; 33 | } 34 | } 35 | 36 | /** 37 | * Get Cache File 38 | * 39 | * Gets the path to a cache file for the specified uri_string(). 40 | * 41 | * @param string $uri_string Full uri_string() of the target page (e.g. 'blog/comments/123'). 42 | * @return string Path to the cache file. 43 | */ 44 | if ( ! function_exists('get_cache_file')) 45 | { 46 | function get_cache_file($uri_string) 47 | { 48 | $CI =& get_instance(); 49 | 50 | $uri = $CI->config->item('base_url') . 51 | $CI->config->item('index_page') . 52 | $uri_string; 53 | 54 | return get_cache_folder() . md5($uri); 55 | } 56 | } 57 | 58 | /** 59 | * Get All Cache Files 60 | * 61 | * Gets the path to every cache file currently saved. 62 | * 63 | * @return array get_dir_file_info() of cache files. 64 | */ 65 | if ( ! function_exists('get_all_cache_files')) 66 | { 67 | function get_all_cache_files() 68 | { 69 | $CI =& get_instance(); 70 | 71 | $CI->load->helper('file'); 72 | 73 | return get_dir_file_info(get_cache_folder()); 74 | } 75 | } 76 | 77 | 78 | /** 79 | * Delete Cache File 80 | * 81 | * Evicts the output cache for the targeted page. 82 | * 83 | * @param string $uri_string Full uri_string() of the target page (e.g. 'blog/comments/123'). 84 | * @return boolean TRUE if the cache file was removed, FALSE if it was not. 85 | */ 86 | if ( ! function_exists('delete_cache')) 87 | { 88 | function delete_cache($uri_string) 89 | { 90 | $cache_path = get_cache_file($uri_string); 91 | 92 | if (file_exists($cache_path)) 93 | { 94 | return unlink($cache_path); 95 | } 96 | else 97 | { 98 | return TRUE; 99 | } 100 | } 101 | } 102 | 103 | /** 104 | * Delete All Cache 105 | * 106 | * Evicts the output cache for all pages currently cached. 107 | * 108 | * @return void 109 | */ 110 | if ( ! function_exists('delete_all_cache')) 111 | { 112 | function delete_all_cache() 113 | { 114 | $cache_files = get_all_cache_files(); 115 | 116 | foreach ($cache_files as $file) 117 | { 118 | // only delete files with names that are 32 characters in length (MD5) 119 | if (strlen($file['name']) === 32 && is_really_writable($file['server_path'])) 120 | { 121 | @unlink($file['server_path']); 122 | } 123 | } 124 | } 125 | } 126 | 127 | /** 128 | * Delete Expired Cache 129 | * 130 | * Delete all expired cache files. This is a fairly expensive function so it 131 | * should not be called on every hit. 132 | * 133 | * @return void 134 | */ 135 | if ( ! function_exists('delete_expired_cache')) 136 | { 137 | function delete_expired_cache() 138 | { 139 | $CI =& get_instance(); 140 | 141 | $CI->load->helper('file'); 142 | 143 | $files = get_dir_file_info(get_cache_folder()); 144 | 145 | foreach ($files as $file) 146 | { 147 | if (strlen($file['name']) !== 32) 148 | { 149 | continue; 150 | } 151 | 152 | if ( ! $fp = @fopen($file['server_path'], FOPEN_READ)) 153 | { 154 | continue; 155 | } 156 | 157 | flock($fp, LOCK_SH); 158 | 159 | $time_str = ''; 160 | while (($char = fgetc($fp)) !== FALSE) 161 | { 162 | if ($char === 'T') 163 | { 164 | break; 165 | } 166 | $time_str .= $char; 167 | } 168 | 169 | flock($fp, LOCK_UN); 170 | fclose($fp); 171 | 172 | if (ctype_digit($time_str) && time() >= (int)$time_str) 173 | { 174 | if (is_really_writable($file['server_path'])) 175 | { 176 | @unlink($file['server_path']); 177 | } 178 | } 179 | } 180 | } 181 | } 182 | 183 | /** 184 | * Get Cache Expiration 185 | * 186 | * Gets the expiration time for a cache file. Time strings are in time() format. 187 | * 188 | * @param string $uri_string Full uri_string() of the target page. 189 | * @return mixed Time from the cache file or FALSE if there was a problem. 190 | */ 191 | if ( ! function_exists('get_cache_expiration')) 192 | { 193 | function get_cache_expiration($uri_string) 194 | { 195 | $cache_path = get_cache_file($uri_string); 196 | 197 | if ( ! @file_exists($cache_path)) 198 | { 199 | return FALSE; 200 | } 201 | 202 | if ( ! $fp = @fopen($cache_path, FOPEN_READ)) 203 | { 204 | return FALSE; 205 | } 206 | 207 | flock($fp, LOCK_SH); 208 | 209 | $time_str = ''; 210 | while (($char = fgetc($fp)) !== FALSE) 211 | { 212 | if ($char === 'T') 213 | { 214 | break; 215 | } 216 | $time_str .= $char; 217 | } 218 | 219 | flock($fp, LOCK_UN); 220 | fclose($fp); 221 | 222 | if (ctype_digit($time_str)) 223 | { 224 | return (int)$time_str; 225 | } 226 | else 227 | { 228 | return FALSE; 229 | } 230 | } 231 | } 232 | 233 | /* End of file cache_helper.php */ 234 | /* Location: ./application/helpers/cache_helper.php */ --------------------------------------------------------------------------------