blob: c0be0def4b27b4874c031fec4808b307b0062fce [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
28// ------------------------------------------------------------------------
29
30/**
Andrey Andreev7d4ea072011-12-25 19:23:50 +020031 * CodeIgniter Memcached Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060032 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Andrey Andreev7d4ea072011-12-25 19:23:50 +020037 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060038 */
39
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010040class CI_Cache_file extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060041
42 protected $_cache_path;
43
44 /**
45 * Constructor
46 */
47 public function __construct()
48 {
49 $CI =& get_instance();
50 $CI->load->helper('file');
Greg Akerbde25d92010-12-21 09:31:21 -060051 $path = $CI->config->item('cache_path');
Greg Aker23351dc2011-02-14 00:14:21 -060052 $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Greg Akerbde25d92010-12-21 09:31:21 -060053 }
54
55 // ------------------------------------------------------------------------
56
57 /**
58 * Fetch from cache
59 *
60 * @param mixed unique key id
61 * @return mixed data on success/false on failure
62 */
63 public function get($id)
64 {
65 if ( ! file_exists($this->_cache_path.$id))
66 {
67 return FALSE;
68 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020069
70 $data = unserialize(read_file($this->_cache_path.$id));
71
Derek Jones37f4b9c2011-07-01 17:56:50 -050072 if (time() > $data['time'] + $data['ttl'])
Greg Akerbde25d92010-12-21 09:31:21 -060073 {
74 unlink($this->_cache_path.$id);
75 return FALSE;
76 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020077
Greg Akerbde25d92010-12-21 09:31:21 -060078 return $data['data'];
79 }
80
81 // ------------------------------------------------------------------------
82
83 /**
84 * Save into cache
85 *
86 * @param string unique key
87 * @param mixed data to store
Andrey Andreev7d4ea072011-12-25 19:23:50 +020088 * @param int length of time (in seconds) the cache is valid
Greg Akerbde25d92010-12-21 09:31:21 -060089 * - Default is 60 seconds
90 * @return boolean true on success/false on failure
91 */
92 public function save($id, $data, $ttl = 60)
Andrey Andreev7d4ea072011-12-25 19:23:50 +020093 {
Greg Akerbde25d92010-12-21 09:31:21 -060094 $contents = array(
95 'time' => time(),
Andrey Andreev7d4ea072011-12-25 19:23:50 +020096 'ttl' => $ttl,
Greg Akerbde25d92010-12-21 09:31:21 -060097 'data' => $data
98 );
Andrey Andreev7d4ea072011-12-25 19:23:50 +020099
Greg Akerbde25d92010-12-21 09:31:21 -0600100 if (write_file($this->_cache_path.$id, serialize($contents)))
101 {
Yorick Peterse6e09f232012-02-15 18:23:08 +0100102 @chmod($this->_cache_path.$id, 0660);
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200103 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600104 }
105
106 return FALSE;
107 }
108
109 // ------------------------------------------------------------------------
110
111 /**
112 * Delete from Cache
113 *
114 * @param mixed unique identifier of item in cache
115 * @return boolean true on success/false on failure
116 */
117 public function delete($id)
118 {
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200119 return (file_exists($this->_cache_path.$id)) ? unlink($this->_cache_path.$id) : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -0600120 }
121
122 // ------------------------------------------------------------------------
123
124 /**
125 * Clean the Cache
126 *
127 * @return boolean false on failure/true on success
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200128 */
Greg Akerbde25d92010-12-21 09:31:21 -0600129 public function clean()
130 {
131 return delete_files($this->_cache_path);
132 }
133
134 // ------------------------------------------------------------------------
135
136 /**
137 * Cache Info
138 *
139 * Not supported by file-based caching
140 *
141 * @param string user/filehits
142 * @return mixed FALSE
143 */
144 public function cache_info($type = NULL)
145 {
146 return get_dir_file_info($this->_cache_path);
147 }
148
149 // ------------------------------------------------------------------------
150
151 /**
152 * Get Cache Metadata
153 *
154 * @param mixed key to get cache metadata on
155 * @return mixed FALSE on failure, array on success.
156 */
157 public function get_metadata($id)
158 {
159 if ( ! file_exists($this->_cache_path.$id))
160 {
161 return FALSE;
162 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200163
164 $data = unserialize(read_file($this->_cache_path.$id));
165
Greg Akerbde25d92010-12-21 09:31:21 -0600166 if (is_array($data))
167 {
Greg Akerbde25d92010-12-21 09:31:21 -0600168 $mtime = filemtime($this->_cache_path.$id);
169
Ben Edmunds80f4c342011-08-20 14:11:21 -0500170 if ( ! isset($data['data']['ttl']))
Greg Akerbde25d92010-12-21 09:31:21 -0600171 {
172 return FALSE;
173 }
174
175 return array(
Ben Edmunds3db28492011-08-20 14:12:12 -0500176 'expire' => $mtime + $data['data']['ttl'],
177 'mtime' => $mtime
Greg Akerbde25d92010-12-21 09:31:21 -0600178 );
179 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200180
Greg Akerbde25d92010-12-21 09:31:21 -0600181 return FALSE;
182 }
183
184 // ------------------------------------------------------------------------
185
186 /**
187 * Is supported
188 *
189 * In the file driver, check to see that the cache directory is indeed writable
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200190 *
Greg Akerbde25d92010-12-21 09:31:21 -0600191 * @return boolean
192 */
193 public function is_supported()
194 {
195 return is_really_writable($this->_cache_path);
196 }
197
198 // ------------------------------------------------------------------------
199}
200// End Class
201
202/* End of file Cache_file.php */
Jeroen van der Gulika3a8b612011-09-16 13:39:30 +0200203/* Location: ./system/libraries/Cache/drivers/Cache_file.php */