blob: 769bd5a2613525884f462ef0e27fa940df1052e3 [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 Andreevb24b0332012-03-26 15:34:39 +030065 * @param mixed unique key id
66 * @return mixed data on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060067 */
68 public function get($id)
69 {
70 if ( ! file_exists($this->_cache_path.$id))
71 {
72 return FALSE;
73 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020074
Andrey Andreev0f0b7692012-06-07 14:57:04 +030075 $data = unserialize(file_get_contents($this->_cache_path.$id));
Andrey Andreev7d4ea072011-12-25 19:23:50 +020076
Andrey Andreev838a9d62012-12-03 14:37:47 +020077 if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl'])
Greg Akerbde25d92010-12-21 09:31:21 -060078 {
79 unlink($this->_cache_path.$id);
80 return FALSE;
81 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020082
Greg Akerbde25d92010-12-21 09:31:21 -060083 return $data['data'];
84 }
85
86 // ------------------------------------------------------------------------
87
88 /**
89 * Save into cache
90 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030091 * @param string unique key
92 * @param mixed data to store
93 * @param int length of time (in seconds) the cache is valid
94 * - Default is 60 seconds
95 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060096 */
97 public function save($id, $data, $ttl = 60)
Andrey Andreev7d4ea072011-12-25 19:23:50 +020098 {
Greg Akerbde25d92010-12-21 09:31:21 -060099 $contents = array(
Timothy Warren0688ac92012-04-20 10:25:04 -0400100 'time' => time(),
101 'ttl' => $ttl,
102 'data' => $data
103 );
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200104
Greg Akerbde25d92010-12-21 09:31:21 -0600105 if (write_file($this->_cache_path.$id, serialize($contents)))
106 {
Yorick Peterse6e09f232012-02-15 18:23:08 +0100107 @chmod($this->_cache_path.$id, 0660);
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200108 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600109 }
110
111 return FALSE;
112 }
113
114 // ------------------------------------------------------------------------
115
116 /**
117 * Delete from Cache
118 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300119 * @param mixed unique identifier of item in cache
120 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600121 */
122 public function delete($id)
123 {
Andrey Andreevb24b0332012-03-26 15:34:39 +0300124 return file_exists($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -0600125 }
126
127 // ------------------------------------------------------------------------
128
129 /**
130 * Clean the Cache
131 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300132 * @return bool false on failure/true on success
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200133 */
Greg Akerbde25d92010-12-21 09:31:21 -0600134 public function clean()
135 {
vlakoff83c344e2013-03-04 14:30:08 +0100136 return delete_files($this->_cache_path, FALSE, TRUE);
Greg Akerbde25d92010-12-21 09:31:21 -0600137 }
138
139 // ------------------------------------------------------------------------
140
141 /**
142 * Cache Info
143 *
144 * Not supported by file-based caching
145 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300146 * @param string user/filehits
147 * @return mixed FALSE
Greg Akerbde25d92010-12-21 09:31:21 -0600148 */
149 public function cache_info($type = NULL)
150 {
151 return get_dir_file_info($this->_cache_path);
152 }
153
154 // ------------------------------------------------------------------------
155
156 /**
157 * Get Cache Metadata
158 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300159 * @param mixed key to get cache metadata on
160 * @return mixed FALSE on failure, array on success.
Greg Akerbde25d92010-12-21 09:31:21 -0600161 */
162 public function get_metadata($id)
163 {
164 if ( ! file_exists($this->_cache_path.$id))
165 {
166 return FALSE;
167 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200168
Andrey Andreev0f0b7692012-06-07 14:57:04 +0300169 $data = unserialize(file_get_contents($this->_cache_path.$id));
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200170
Greg Akerbde25d92010-12-21 09:31:21 -0600171 if (is_array($data))
172 {
Greg Akerbde25d92010-12-21 09:31:21 -0600173 $mtime = filemtime($this->_cache_path.$id);
174
Burak Erdem0b48dd42012-02-06 17:28:33 +0200175 if ( ! isset($data['ttl']))
Greg Akerbde25d92010-12-21 09:31:21 -0600176 {
177 return FALSE;
178 }
179
180 return array(
Burak Erdem0b48dd42012-02-06 17:28:33 +0200181 'expire' => $mtime + $data['ttl'],
Ben Edmunds3db28492011-08-20 14:12:12 -0500182 'mtime' => $mtime
Greg Akerbde25d92010-12-21 09:31:21 -0600183 );
184 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200185
Greg Akerbde25d92010-12-21 09:31:21 -0600186 return FALSE;
187 }
188
189 // ------------------------------------------------------------------------
190
191 /**
192 * Is supported
193 *
194 * In the file driver, check to see that the cache directory is indeed writable
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200195 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300196 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600197 */
198 public function is_supported()
199 {
200 return is_really_writable($this->_cache_path);
201 }
202
Greg Akerbde25d92010-12-21 09:31:21 -0600203}
Greg Akerbde25d92010-12-21 09:31:21 -0600204
205/* End of file Cache_file.php */
Andrey Andreevc9195a72012-06-12 03:49:03 +0300206/* Location: ./system/libraries/Cache/drivers/Cache_file.php */