blob: 29898040a08d21bfa2de6e1ebf764adde9a18c5f [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Jonesf4a4bd82011-10-20 12:18:42 -05006 *
7 * NOTICE OF LICENSE
Andrey Andreev7d4ea072011-12-25 19:23:50 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
Greg Akerbde25d92010-12-21 09:31:21 -060018 *
19 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Greg Akerbde25d92010-12-21 09:31:21 -060023 * @link http://codeigniter.com
24 * @since Version 2.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020025 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060026 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -060028
Greg Akerbde25d92010-12-21 09:31:21 -060029/**
Andrey Andreevc9195a72012-06-12 03:49:03 +030030 * CodeIgniter File Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060031 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Andrey Andreev7d4ea072011-12-25 19:23:50 +020036 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060037 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010038class CI_Cache_file extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060039
Timothy Warren0688ac92012-04-20 10:25:04 -040040 /**
41 * Directory in which to save cache files
42 *
43 * @var string
44 */
Greg Akerbde25d92010-12-21 09:31:21 -060045 protected $_cache_path;
46
Timothy Warren0688ac92012-04-20 10:25:04 -040047 /**
48 * Initialize file-based cache
Andrey Andreev56454792012-05-17 14:32:19 +030049 *
50 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -040051 */
Greg Akerbde25d92010-12-21 09:31:21 -060052 public function __construct()
53 {
54 $CI =& get_instance();
55 $CI->load->helper('file');
Greg Akerbde25d92010-12-21 09:31:21 -060056 $path = $CI->config->item('cache_path');
Alex Bilbied261b1e2012-06-02 11:12:16 +010057 $this->_cache_path = ($path === '') ? APPPATH.'cache/' : $path;
Greg Akerbde25d92010-12-21 09:31:21 -060058 }
59
60 // ------------------------------------------------------------------------
61
62 /**
63 * Fetch from cache
64 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020065 * @param string $id Cache ID
66 * @return mixed Data on success, FALSE on failure
Greg Akerbde25d92010-12-21 09:31:21 -060067 */
68 public function get($id)
69 {
Andrey Andreev43d7fa72014-01-09 17:29:45 +020070 $data = $this->_get($id);
71 return is_array($data) ? $data['data'] : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060072 }
73
74 // ------------------------------------------------------------------------
75
76 /**
77 * Save into cache
78 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020079 * @param string $id Cache ID
80 * @param mixed $data Data to store
81 * @param int $ttl Time to live in seconds
82 * @param bool $raw Whether to store the raw value (unused)
83 * @return bool TRUE on success, FALSE on failure
Greg Akerbde25d92010-12-21 09:31:21 -060084 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +020085 public function save($id, $data, $ttl = 60, $raw = FALSE)
Andrey Andreev7d4ea072011-12-25 19:23:50 +020086 {
Greg Akerbde25d92010-12-21 09:31:21 -060087 $contents = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040088 'time' => time(),
89 'ttl' => $ttl,
90 'data' => $data
91 );
Andrey Andreev7d4ea072011-12-25 19:23:50 +020092
Greg Akerbde25d92010-12-21 09:31:21 -060093 if (write_file($this->_cache_path.$id, serialize($contents)))
94 {
Andrey Andreev45965742014-08-27 20:40:11 +030095 chmod($this->_cache_path.$id, 0640);
Andrey Andreev7d4ea072011-12-25 19:23:50 +020096 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -060097 }
98
99 return FALSE;
100 }
101
102 // ------------------------------------------------------------------------
103
104 /**
105 * Delete from Cache
106 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300107 * @param mixed unique identifier of item in cache
108 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600109 */
110 public function delete($id)
111 {
Andrey Andreevb24b0332012-03-26 15:34:39 +0300112 return file_exists($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -0600113 }
114
115 // ------------------------------------------------------------------------
116
117 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200118 * Increment a raw value
119 *
120 * @param string $id Cache ID
121 * @param int $offset Step/value to add
122 * @return New value on success, FALSE on failure
123 */
124 public function increment($id, $offset = 1)
125 {
126 $data = $this->_get($id);
127
Andrey Andreev5f0799a2014-07-31 13:32:41 +0300128 if ($data === FALSE)
129 {
130 $data = array('data' => 0, 'ttl' => 60);
131 }
132 elseif ( ! is_int($data['data']))
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200133 {
134 return FALSE;
135 }
136
137 $new_value = $data['data'] + $offset;
138 return $this->save($id, $new_value, $data['ttl'])
139 ? $new_value
140 : FALSE;
141 }
142
143 // ------------------------------------------------------------------------
144
145 /**
146 * Decrement a raw value
147 *
148 * @param string $id Cache ID
149 * @param int $offset Step/value to reduce by
150 * @return New value on success, FALSE on failure
151 */
152 public function decrement($id, $offset = 1)
153 {
154 $data = $this->_get($id);
155
Andrey Andreev5f0799a2014-07-31 13:32:41 +0300156 if ($data === FALSE)
157 {
158 $data = array('data' => 0, 'ttl' => 60);
159 }
160 elseif ( ! is_int($data['data']))
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200161 {
162 return FALSE;
163 }
164
165 $new_value = $data['data'] - $offset;
166 return $this->save($id, $new_value, $data['ttl'])
167 ? $new_value
168 : FALSE;
169 }
170
171 // ------------------------------------------------------------------------
172
173 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600174 * Clean the Cache
175 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300176 * @return bool false on failure/true on success
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200177 */
Greg Akerbde25d92010-12-21 09:31:21 -0600178 public function clean()
179 {
vlakoff83c344e2013-03-04 14:30:08 +0100180 return delete_files($this->_cache_path, FALSE, TRUE);
Greg Akerbde25d92010-12-21 09:31:21 -0600181 }
182
183 // ------------------------------------------------------------------------
184
185 /**
186 * Cache Info
187 *
188 * Not supported by file-based caching
189 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300190 * @param string user/filehits
191 * @return mixed FALSE
Greg Akerbde25d92010-12-21 09:31:21 -0600192 */
193 public function cache_info($type = NULL)
194 {
195 return get_dir_file_info($this->_cache_path);
196 }
197
198 // ------------------------------------------------------------------------
199
200 /**
201 * Get Cache Metadata
202 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300203 * @param mixed key to get cache metadata on
204 * @return mixed FALSE on failure, array on success.
Greg Akerbde25d92010-12-21 09:31:21 -0600205 */
206 public function get_metadata($id)
207 {
208 if ( ! file_exists($this->_cache_path.$id))
209 {
210 return FALSE;
211 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200212
Andrey Andreev0f0b7692012-06-07 14:57:04 +0300213 $data = unserialize(file_get_contents($this->_cache_path.$id));
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200214
Greg Akerbde25d92010-12-21 09:31:21 -0600215 if (is_array($data))
216 {
Greg Akerbde25d92010-12-21 09:31:21 -0600217 $mtime = filemtime($this->_cache_path.$id);
218
Burak Erdem0b48dd42012-02-06 17:28:33 +0200219 if ( ! isset($data['ttl']))
Greg Akerbde25d92010-12-21 09:31:21 -0600220 {
221 return FALSE;
222 }
223
224 return array(
Burak Erdem0b48dd42012-02-06 17:28:33 +0200225 'expire' => $mtime + $data['ttl'],
Ben Edmunds3db28492011-08-20 14:12:12 -0500226 'mtime' => $mtime
Greg Akerbde25d92010-12-21 09:31:21 -0600227 );
228 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200229
Greg Akerbde25d92010-12-21 09:31:21 -0600230 return FALSE;
231 }
232
233 // ------------------------------------------------------------------------
234
235 /**
236 * Is supported
237 *
238 * In the file driver, check to see that the cache directory is indeed writable
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200239 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300240 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600241 */
242 public function is_supported()
243 {
244 return is_really_writable($this->_cache_path);
245 }
246
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200247 // ------------------------------------------------------------------------
248
249 /**
250 * Get all data
251 *
252 * Internal method to get all the relevant data about a cache item
253 *
254 * @param string $id Cache ID
255 * @return mixed Data array on success or FALSE on failure
256 */
257 protected function _get($id)
258 {
259 if ( ! file_exists($this->_cache_path.$id))
260 {
261 return FALSE;
262 }
263
264 $data = unserialize(file_get_contents($this->_cache_path.$id));
265
266 if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl'])
267 {
268 unlink($this->_cache_path.$id);
269 return FALSE;
270 }
271
272 return $data;
273 }
274
Greg Akerbde25d92010-12-21 09:31:21 -0600275}
Greg Akerbde25d92010-12-21 09:31:21 -0600276
277/* End of file Cache_file.php */
Andrey Andreevc9195a72012-06-12 03:49:03 +0300278/* Location: ./system/libraries/Cache/drivers/Cache_file.php */