blob: 8c99c5ef3e313c8c53929d72bef9e697a4fc58a6 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 {
Yorick Peterse6e09f232012-02-15 18:23:08 +010095 @chmod($this->_cache_path.$id, 0660);
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
128 if ($data === FALSE OR ! is_int($data['data']))
129 {
130 return FALSE;
131 }
132
133 $new_value = $data['data'] + $offset;
134 return $this->save($id, $new_value, $data['ttl'])
135 ? $new_value
136 : FALSE;
137 }
138
139 // ------------------------------------------------------------------------
140
141 /**
142 * Decrement a raw value
143 *
144 * @param string $id Cache ID
145 * @param int $offset Step/value to reduce by
146 * @return New value on success, FALSE on failure
147 */
148 public function decrement($id, $offset = 1)
149 {
150 $data = $this->_get($id);
151
152 if ($data === FALSE OR ! is_int($data['data']))
153 {
154 return FALSE;
155 }
156
157 $new_value = $data['data'] - $offset;
158 return $this->save($id, $new_value, $data['ttl'])
159 ? $new_value
160 : FALSE;
161 }
162
163 // ------------------------------------------------------------------------
164
165 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600166 * Clean the Cache
167 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300168 * @return bool false on failure/true on success
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200169 */
Greg Akerbde25d92010-12-21 09:31:21 -0600170 public function clean()
171 {
vlakoff83c344e2013-03-04 14:30:08 +0100172 return delete_files($this->_cache_path, FALSE, TRUE);
Greg Akerbde25d92010-12-21 09:31:21 -0600173 }
174
175 // ------------------------------------------------------------------------
176
177 /**
178 * Cache Info
179 *
180 * Not supported by file-based caching
181 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300182 * @param string user/filehits
183 * @return mixed FALSE
Greg Akerbde25d92010-12-21 09:31:21 -0600184 */
185 public function cache_info($type = NULL)
186 {
187 return get_dir_file_info($this->_cache_path);
188 }
189
190 // ------------------------------------------------------------------------
191
192 /**
193 * Get Cache Metadata
194 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300195 * @param mixed key to get cache metadata on
196 * @return mixed FALSE on failure, array on success.
Greg Akerbde25d92010-12-21 09:31:21 -0600197 */
198 public function get_metadata($id)
199 {
200 if ( ! file_exists($this->_cache_path.$id))
201 {
202 return FALSE;
203 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200204
Andrey Andreev0f0b7692012-06-07 14:57:04 +0300205 $data = unserialize(file_get_contents($this->_cache_path.$id));
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200206
Greg Akerbde25d92010-12-21 09:31:21 -0600207 if (is_array($data))
208 {
Greg Akerbde25d92010-12-21 09:31:21 -0600209 $mtime = filemtime($this->_cache_path.$id);
210
Burak Erdem0b48dd42012-02-06 17:28:33 +0200211 if ( ! isset($data['ttl']))
Greg Akerbde25d92010-12-21 09:31:21 -0600212 {
213 return FALSE;
214 }
215
216 return array(
Burak Erdem0b48dd42012-02-06 17:28:33 +0200217 'expire' => $mtime + $data['ttl'],
Ben Edmunds3db28492011-08-20 14:12:12 -0500218 'mtime' => $mtime
Greg Akerbde25d92010-12-21 09:31:21 -0600219 );
220 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200221
Greg Akerbde25d92010-12-21 09:31:21 -0600222 return FALSE;
223 }
224
225 // ------------------------------------------------------------------------
226
227 /**
228 * Is supported
229 *
230 * In the file driver, check to see that the cache directory is indeed writable
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200231 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300232 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600233 */
234 public function is_supported()
235 {
236 return is_really_writable($this->_cache_path);
237 }
238
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200239 // ------------------------------------------------------------------------
240
241 /**
242 * Get all data
243 *
244 * Internal method to get all the relevant data about a cache item
245 *
246 * @param string $id Cache ID
247 * @return mixed Data array on success or FALSE on failure
248 */
249 protected function _get($id)
250 {
251 if ( ! file_exists($this->_cache_path.$id))
252 {
253 return FALSE;
254 }
255
256 $data = unserialize(file_get_contents($this->_cache_path.$id));
257
258 if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl'])
259 {
260 unlink($this->_cache_path.$id);
261 return FALSE;
262 }
263
264 return $data;
265 }
266
Greg Akerbde25d92010-12-21 09:31:21 -0600267}
Greg Akerbde25d92010-12-21 09:31:21 -0600268
269/* End of file Cache_file.php */
Andrey Andreevc9195a72012-06-12 03:49:03 +0300270/* Location: ./system/libraries/Cache/drivers/Cache_file.php */