blob: ec419527879a15dc691a2c100c0debd039d2ef36 [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
Timothy Warren0688ac92012-04-20 10:25:04 -040039 /**
40 * Directory in which to save cache files
41 *
42 * @var string
43 */
Greg Akerbde25d92010-12-21 09:31:21 -060044 protected $_cache_path;
45
Timothy Warren0688ac92012-04-20 10:25:04 -040046 /**
47 * Initialize file-based cache
48 */
Greg Akerbde25d92010-12-21 09:31:21 -060049 public function __construct()
50 {
51 $CI =& get_instance();
52 $CI->load->helper('file');
Greg Akerbde25d92010-12-21 09:31:21 -060053 $path = $CI->config->item('cache_path');
Greg Aker23351dc2011-02-14 00:14:21 -060054 $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Greg Akerbde25d92010-12-21 09:31:21 -060055 }
56
57 // ------------------------------------------------------------------------
58
59 /**
60 * Fetch from cache
61 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030062 * @param mixed unique key id
63 * @return mixed data on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060064 */
65 public function get($id)
66 {
67 if ( ! file_exists($this->_cache_path.$id))
68 {
69 return FALSE;
70 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020071
72 $data = unserialize(read_file($this->_cache_path.$id));
73
Derek Jones37f4b9c2011-07-01 17:56:50 -050074 if (time() > $data['time'] + $data['ttl'])
Greg Akerbde25d92010-12-21 09:31:21 -060075 {
76 unlink($this->_cache_path.$id);
77 return FALSE;
78 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020079
Greg Akerbde25d92010-12-21 09:31:21 -060080 return $data['data'];
81 }
82
83 // ------------------------------------------------------------------------
84
85 /**
86 * Save into cache
87 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030088 * @param string unique key
89 * @param mixed data to store
90 * @param int length of time (in seconds) the cache is valid
91 * - Default is 60 seconds
92 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060093 */
94 public function save($id, $data, $ttl = 60)
Andrey Andreev7d4ea072011-12-25 19:23:50 +020095 {
Greg Akerbde25d92010-12-21 09:31:21 -060096 $contents = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040097 'time' => time(),
98 'ttl' => $ttl,
99 'data' => $data
100 );
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200101
Greg Akerbde25d92010-12-21 09:31:21 -0600102 if (write_file($this->_cache_path.$id, serialize($contents)))
103 {
Yorick Peterse6e09f232012-02-15 18:23:08 +0100104 @chmod($this->_cache_path.$id, 0660);
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200105 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600106 }
107
108 return FALSE;
109 }
110
111 // ------------------------------------------------------------------------
112
113 /**
114 * Delete from Cache
115 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300116 * @param mixed unique identifier of item in cache
117 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600118 */
119 public function delete($id)
120 {
Andrey Andreevb24b0332012-03-26 15:34:39 +0300121 return file_exists($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -0600122 }
123
124 // ------------------------------------------------------------------------
125
126 /**
127 * Clean the Cache
128 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300129 * @return bool false on failure/true on success
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200130 */
Greg Akerbde25d92010-12-21 09:31:21 -0600131 public function clean()
132 {
133 return delete_files($this->_cache_path);
134 }
135
136 // ------------------------------------------------------------------------
137
138 /**
139 * Cache Info
140 *
141 * Not supported by file-based caching
142 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300143 * @param string user/filehits
144 * @return mixed FALSE
Greg Akerbde25d92010-12-21 09:31:21 -0600145 */
146 public function cache_info($type = NULL)
147 {
148 return get_dir_file_info($this->_cache_path);
149 }
150
151 // ------------------------------------------------------------------------
152
153 /**
154 * Get Cache Metadata
155 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300156 * @param mixed key to get cache metadata on
157 * @return mixed FALSE on failure, array on success.
Greg Akerbde25d92010-12-21 09:31:21 -0600158 */
159 public function get_metadata($id)
160 {
161 if ( ! file_exists($this->_cache_path.$id))
162 {
163 return FALSE;
164 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200165
166 $data = unserialize(read_file($this->_cache_path.$id));
167
Greg Akerbde25d92010-12-21 09:31:21 -0600168 if (is_array($data))
169 {
Greg Akerbde25d92010-12-21 09:31:21 -0600170 $mtime = filemtime($this->_cache_path.$id);
171
Ben Edmunds80f4c342011-08-20 14:11:21 -0500172 if ( ! isset($data['data']['ttl']))
Greg Akerbde25d92010-12-21 09:31:21 -0600173 {
174 return FALSE;
175 }
176
177 return array(
Ben Edmunds3db28492011-08-20 14:12:12 -0500178 'expire' => $mtime + $data['data']['ttl'],
179 'mtime' => $mtime
Greg Akerbde25d92010-12-21 09:31:21 -0600180 );
181 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200182
Greg Akerbde25d92010-12-21 09:31:21 -0600183 return FALSE;
184 }
185
186 // ------------------------------------------------------------------------
187
188 /**
189 * Is supported
190 *
191 * In the file driver, check to see that the cache directory is indeed writable
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200192 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300193 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600194 */
195 public function is_supported()
196 {
197 return is_really_writable($this->_cache_path);
198 }
199
Greg Akerbde25d92010-12-21 09:31:21 -0600200}
Greg Akerbde25d92010-12-21 09:31:21 -0600201
202/* End of file Cache_file.php */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300203/* Location: ./system/libraries/Cache/drivers/Cache_file.php */