source: pro-violet-viettel/sourcecode/system/libraries/Cache/drivers/Cache_file.php

Last change on this file was 289, checked in by dungnv, 11 years ago
File size: 4.0 KB
Line 
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package             CodeIgniter
8 * @author              ExpressionEngine Dev Team
9 * @copyright   Copyright (c) 2006 - 2012 EllisLab, Inc.
10 * @license             http://codeigniter.com/user_guide/license.html
11 * @link                http://codeigniter.com
12 * @since               Version 2.0
13 * @filesource 
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Memcached Caching Class
20 *
21 * @package             CodeIgniter
22 * @subpackage  Libraries
23 * @category    Core
24 * @author              ExpressionEngine Dev Team
25 * @link               
26 */
27
28class CI_Cache_file extends CI_Driver {
29
30        protected $_cache_path;
31
32        /**
33         * Constructor
34         */
35        public function __construct()
36        {
37                $CI =& get_instance();
38                $CI->load->helper('file');
39               
40                $path = $CI->config->item('cache_path');
41       
42                $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path;
43        }
44
45        // ------------------------------------------------------------------------
46
47        /**
48         * Fetch from cache
49         *
50         * @param       mixed           unique key id
51         * @return      mixed           data on success/false on failure
52         */
53        public function get($id)
54        {
55                if ( ! file_exists($this->_cache_path.$id))
56                {
57                        return FALSE;
58                }
59               
60                $data = read_file($this->_cache_path.$id);
61                $data = unserialize($data);
62               
63                if (time() >  $data['time'] + $data['ttl'])
64                {
65                        unlink($this->_cache_path.$id);
66                        return FALSE;
67                }
68               
69                return $data['data'];
70        }
71
72        // ------------------------------------------------------------------------
73
74        /**
75         * Save into cache
76         *
77         * @param       string          unique key
78         * @param       mixed           data to store
79         * @param       int                     length of time (in seconds) the cache is valid
80         *                                              - Default is 60 seconds
81         * @return      boolean         true on success/false on failure
82         */
83        public function save($id, $data, $ttl = 60)
84        {               
85                $contents = array(
86                                'time'          => time(),
87                                'ttl'           => $ttl,                       
88                                'data'          => $data
89                        );
90               
91                if (write_file($this->_cache_path.$id, serialize($contents)))
92                {
93                        @chmod($this->_cache_path.$id, 0777);
94                        return TRUE;                   
95                }
96
97                return FALSE;
98        }
99
100        // ------------------------------------------------------------------------
101
102        /**
103         * Delete from Cache
104         *
105         * @param       mixed           unique identifier of item in cache
106         * @return      boolean         true on success/false on failure
107         */
108        public function delete($id)
109        {
110                return unlink($this->_cache_path.$id);
111        }
112
113        // ------------------------------------------------------------------------
114
115        /**
116         * Clean the Cache
117         *
118         * @return      boolean         false on failure/true on success
119         */     
120        public function clean()
121        {
122                return delete_files($this->_cache_path);
123        }
124
125        // ------------------------------------------------------------------------
126
127        /**
128         * Cache Info
129         *
130         * Not supported by file-based caching
131         *
132         * @param       string  user/filehits
133         * @return      mixed   FALSE
134         */
135        public function cache_info($type = NULL)
136        {
137                return get_dir_file_info($this->_cache_path);
138        }
139
140        // ------------------------------------------------------------------------
141
142        /**
143         * Get Cache Metadata
144         *
145         * @param       mixed           key to get cache metadata on
146         * @return      mixed           FALSE on failure, array on success.
147         */
148        public function get_metadata($id)
149        {
150                if ( ! file_exists($this->_cache_path.$id))
151                {
152                        return FALSE;
153                }
154
155                $data = read_file($this->_cache_path.$id);
156                $data = unserialize($data);
157
158                if (is_array($data))
159                {
160                        $mtime = filemtime($this->_cache_path.$id);
161
162                        if ( ! isset($data['ttl']))
163                        {
164                                return FALSE;
165                        }
166
167                        return array(
168                                'expire'        => $mtime + $data['ttl'],
169                                'mtime'         => $mtime
170                        );
171                }
172
173                return FALSE;
174        }
175
176        // ------------------------------------------------------------------------
177
178        /**
179         * Is supported
180         *
181         * In the file driver, check to see that the cache directory is indeed writable
182         *
183         * @return boolean
184         */
185        public function is_supported()
186        {
187                return is_really_writable($this->_cache_path);
188        }
189
190        // ------------------------------------------------------------------------
191}
192// End Class
193
194/* End of file Cache_file.php */
195/* Location: ./system/libraries/Cache/drivers/Cache_file.php */
Note: See TracBrowser for help on using the repository browser.