blob: 5170de821ae049fa2b5e771ead913c7617e22990 [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
Andrey Andreev56454792012-05-17 14:32:19 +030048 *
49 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -040050 */
Greg Akerbde25d92010-12-21 09:31:21 -060051 public function __construct()
52 {
53 $CI =& get_instance();
54 $CI->load->helper('file');
Greg Akerbde25d92010-12-21 09:31:21 -060055 $path = $CI->config->item('cache_path');
Alex Bilbied261b1e2012-06-02 11:12:16 +010056 $this->_cache_path = ($path === '') ? APPPATH.'cache/' : $path;
Greg Akerbde25d92010-12-21 09:31:21 -060057 }
58
59 // ------------------------------------------------------------------------
60
61 /**
62 * Fetch from cache
63 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030064 * @param mixed unique key id
65 * @return mixed data on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060066 */
67 public function get($id)
68 {
69 if ( ! file_exists($this->_cache_path.$id))
70 {
71 return FALSE;
72 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020073
Andrey Andreev0f0b7692012-06-07 14:57:04 +030074 $data = unserialize(file_get_contents($this->_cache_path.$id));
Andrey Andreev7d4ea072011-12-25 19:23:50 +020075
Derek Jones37f4b9c2011-07-01 17:56:50 -050076 if (time() > $data['time'] + $data['ttl'])
Greg Akerbde25d92010-12-21 09:31:21 -060077 {
78 unlink($this->_cache_path.$id);
79 return FALSE;
80 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020081
Greg Akerbde25d92010-12-21 09:31:21 -060082 return $data['data'];
83 }
84
85 // ------------------------------------------------------------------------
86
87 /**
88 * Save into cache
89 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030090 * @param string unique key
91 * @param mixed data to store
92 * @param int length of time (in seconds) the cache is valid
93 * - Default is 60 seconds
94 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060095 */
96 public function save($id, $data, $ttl = 60)
Andrey Andreev7d4ea072011-12-25 19:23:50 +020097 {
Greg Akerbde25d92010-12-21 09:31:21 -060098 $contents = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040099 'time' => time(),
100 'ttl' => $ttl,
101 'data' => $data
102 );
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200103
Greg Akerbde25d92010-12-21 09:31:21 -0600104 if (write_file($this->_cache_path.$id, serialize($contents)))
105 {
Yorick Peterse6e09f232012-02-15 18:23:08 +0100106 @chmod($this->_cache_path.$id, 0660);
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200107 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600108 }
109
110 return FALSE;
111 }
112
113 // ------------------------------------------------------------------------
114
115 /**
116 * Delete from Cache
117 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300118 * @param mixed unique identifier of item in cache
119 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600120 */
121 public function delete($id)
122 {
Andrey Andreevb24b0332012-03-26 15:34:39 +0300123 return file_exists($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -0600124 }
125
126 // ------------------------------------------------------------------------
127
128 /**
129 * Clean the Cache
130 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300131 * @return bool false on failure/true on success
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200132 */
Greg Akerbde25d92010-12-21 09:31:21 -0600133 public function clean()
134 {
135 return delete_files($this->_cache_path);
136 }
137
138 // ------------------------------------------------------------------------
139
140 /**
141 * Cache Info
142 *
143 * Not supported by file-based caching
144 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300145 * @param string user/filehits
146 * @return mixed FALSE
Greg Akerbde25d92010-12-21 09:31:21 -0600147 */
148 public function cache_info($type = NULL)
149 {
150 return get_dir_file_info($this->_cache_path);
151 }
152
153 // ------------------------------------------------------------------------
154
155 /**
156 * Get Cache Metadata
157 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300158 * @param mixed key to get cache metadata on
159 * @return mixed FALSE on failure, array on success.
Greg Akerbde25d92010-12-21 09:31:21 -0600160 */
161 public function get_metadata($id)
162 {
163 if ( ! file_exists($this->_cache_path.$id))
164 {
165 return FALSE;
166 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200167
Andrey Andreev0f0b7692012-06-07 14:57:04 +0300168 $data = unserialize(file_get_contents($this->_cache_path.$id));
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200169
Greg Akerbde25d92010-12-21 09:31:21 -0600170 if (is_array($data))
171 {
Greg Akerbde25d92010-12-21 09:31:21 -0600172 $mtime = filemtime($this->_cache_path.$id);
173
Ben Edmunds80f4c342011-08-20 14:11:21 -0500174 if ( ! isset($data['data']['ttl']))
Greg Akerbde25d92010-12-21 09:31:21 -0600175 {
176 return FALSE;
177 }
178
179 return array(
Ben Edmunds3db28492011-08-20 14:12:12 -0500180 'expire' => $mtime + $data['data']['ttl'],
181 'mtime' => $mtime
Greg Akerbde25d92010-12-21 09:31:21 -0600182 );
183 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200184
Greg Akerbde25d92010-12-21 09:31:21 -0600185 return FALSE;
186 }
187
188 // ------------------------------------------------------------------------
189
190 /**
191 * Is supported
192 *
193 * In the file driver, check to see that the cache directory is indeed writable
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200194 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300195 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600196 */
197 public function is_supported()
198 {
199 return is_really_writable($this->_cache_path);
200 }
201
Greg Akerbde25d92010-12-21 09:31:21 -0600202}
Greg Akerbde25d92010-12-21 09:31:21 -0600203
204/* End of file Cache_file.php */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300205/* Location: ./system/libraries/Cache/drivers/Cache_file.php */