blob: dd27aa90e25ac3b8f3068fe23fe8d5783e7cc4d7 [file] [log] [blame]
Andrey Andreev7d4ea072011-12-25 19:23:50 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
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 */
27
Greg Akerbde25d92010-12-21 09:31:21 -060028/**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020029 * CodeIgniter Memcached Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060030 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Andrey Andreev7d4ea072011-12-25 19:23:50 +020035 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060036 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010037class CI_Cache_file extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060038
39 protected $_cache_path;
40
Greg Akerbde25d92010-12-21 09:31:21 -060041 public function __construct()
42 {
43 $CI =& get_instance();
44 $CI->load->helper('file');
Greg Akerbde25d92010-12-21 09:31:21 -060045 $path = $CI->config->item('cache_path');
Greg Aker23351dc2011-02-14 00:14:21 -060046 $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Greg Akerbde25d92010-12-21 09:31:21 -060047 }
48
49 // ------------------------------------------------------------------------
50
51 /**
52 * Fetch from cache
53 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030054 * @param mixed unique key id
55 * @return mixed data on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060056 */
57 public function get($id)
58 {
59 if ( ! file_exists($this->_cache_path.$id))
60 {
61 return FALSE;
62 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020063
64 $data = unserialize(read_file($this->_cache_path.$id));
65
Derek Jones37f4b9c2011-07-01 17:56:50 -050066 if (time() > $data['time'] + $data['ttl'])
Greg Akerbde25d92010-12-21 09:31:21 -060067 {
68 unlink($this->_cache_path.$id);
69 return FALSE;
70 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020071
Greg Akerbde25d92010-12-21 09:31:21 -060072 return $data['data'];
73 }
74
75 // ------------------------------------------------------------------------
76
77 /**
78 * Save into cache
79 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030080 * @param string unique key
81 * @param mixed data to store
82 * @param int length of time (in seconds) the cache is valid
83 * - Default is 60 seconds
84 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060085 */
86 public function save($id, $data, $ttl = 60)
Andrey Andreev7d4ea072011-12-25 19:23:50 +020087 {
Greg Akerbde25d92010-12-21 09:31:21 -060088 $contents = array(
89 'time' => time(),
Andrey Andreev7d4ea072011-12-25 19:23:50 +020090 'ttl' => $ttl,
Greg Akerbde25d92010-12-21 09:31:21 -060091 'data' => $data
92 );
Andrey Andreev7d4ea072011-12-25 19:23:50 +020093
Greg Akerbde25d92010-12-21 09:31:21 -060094 if (write_file($this->_cache_path.$id, serialize($contents)))
95 {
Yorick Peterse6e09f232012-02-15 18:23:08 +010096 @chmod($this->_cache_path.$id, 0660);
Andrey Andreev7d4ea072011-12-25 19:23:50 +020097 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -060098 }
99
100 return FALSE;
101 }
102
103 // ------------------------------------------------------------------------
104
105 /**
106 * Delete from Cache
107 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300108 * @param mixed unique identifier of item in cache
109 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600110 */
111 public function delete($id)
112 {
Andrey Andreevb24b0332012-03-26 15:34:39 +0300113 return file_exists($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -0600114 }
115
116 // ------------------------------------------------------------------------
117
118 /**
119 * Clean the Cache
120 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300121 * @return bool false on failure/true on success
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200122 */
Greg Akerbde25d92010-12-21 09:31:21 -0600123 public function clean()
124 {
125 return delete_files($this->_cache_path);
126 }
127
128 // ------------------------------------------------------------------------
129
130 /**
131 * Cache Info
132 *
133 * Not supported by file-based caching
134 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300135 * @param string user/filehits
136 * @return mixed FALSE
Greg Akerbde25d92010-12-21 09:31:21 -0600137 */
138 public function cache_info($type = NULL)
139 {
140 return get_dir_file_info($this->_cache_path);
141 }
142
143 // ------------------------------------------------------------------------
144
145 /**
146 * Get Cache Metadata
147 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300148 * @param mixed key to get cache metadata on
149 * @return mixed FALSE on failure, array on success.
Greg Akerbde25d92010-12-21 09:31:21 -0600150 */
151 public function get_metadata($id)
152 {
153 if ( ! file_exists($this->_cache_path.$id))
154 {
155 return FALSE;
156 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200157
158 $data = unserialize(read_file($this->_cache_path.$id));
159
Greg Akerbde25d92010-12-21 09:31:21 -0600160 if (is_array($data))
161 {
Greg Akerbde25d92010-12-21 09:31:21 -0600162 $mtime = filemtime($this->_cache_path.$id);
163
Ben Edmunds80f4c342011-08-20 14:11:21 -0500164 if ( ! isset($data['data']['ttl']))
Greg Akerbde25d92010-12-21 09:31:21 -0600165 {
166 return FALSE;
167 }
168
169 return array(
Ben Edmunds3db28492011-08-20 14:12:12 -0500170 'expire' => $mtime + $data['data']['ttl'],
171 'mtime' => $mtime
Greg Akerbde25d92010-12-21 09:31:21 -0600172 );
173 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200174
Greg Akerbde25d92010-12-21 09:31:21 -0600175 return FALSE;
176 }
177
178 // ------------------------------------------------------------------------
179
180 /**
181 * Is supported
182 *
183 * In the file driver, check to see that the cache directory is indeed writable
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200184 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300185 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600186 */
187 public function is_supported()
188 {
189 return is_really_writable($this->_cache_path);
190 }
191
Greg Akerbde25d92010-12-21 09:31:21 -0600192}
Greg Akerbde25d92010-12-21 09:31:21 -0600193
194/* End of file Cache_file.php */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300195/* Location: ./system/libraries/Cache/drivers/Cache_file.php */