Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
10582 lgm 1
<?php
2
 
3
if (!defined('BASEPATH'))
4
    exit('No direct script access allowed');
5
 
6
/**
7
 * SmartCache Class
8
 *
9
 * A Smart Caching library for Codeigniter framework. This is written by Sivapatham Nishanthan 
10
 *
11
 * @category	Library
12
 * @author	Sivapatham Nishanthan
13
 * @link	http://thephpcode.com/index.php/help/library_smartcache.html
14
 * @license     MIT
15
 * @version	1.0
16
 */
17
class smartcache {
18
 
19
    function __construct() {
20
 
21
    }
22
 
23
    /**
24
     *
25
     * @param type $fileName : Filename with full path relative to the cache directory in the config file
26
     * @param type $data : Data to cache
27
     * @param type $expire : Time in seconds
28
     * @return type TRUE: cached successfully. FALSE: failed to cache
29
     */
30
    public function save_data($fileName, $data, $expire="") {
31
 
32
        $CI = & get_instance();
33
        $path = $CI->config->load('smartcache');
34
        $cache_path = $CI->config->item('cache_dir');
35
        if (preg_match('/^([0-9a-zA-Z]+\\/)+/', $fileName, $matches) > 0) {
36
            $cache_path .= $matches[0];
37
            if (!is_dir($cache_path)) {
38
                mkdir($cache_path, 0700, TRUE);
39
            }
40
        }
41
        $cache_file = $cache_path . md5($fileName);
42
 
43
 
44
        if (!($fp = @fopen($cache_file, FOPEN_WRITE_CREATE_DESTRUCTIVE))) {
45
            log_message('error', "Unable to write cache file: " . $cache_path);
46
            return FALSE;
47
        }
48
 
49
 
50
		$expire = $CI->config->item('cache_default_expires'); 
51
        if ($expire != "")
52
            $expire = time() + ($expire * 60);
53
        if (flock($fp, LOCK_EX)) {
54
 
55
            fwrite($fp, $expire . "LGM-SMARTCACHE-IDENTIFIER" . $data);
56
 
57
            flock($fp, LOCK_UN);
58
        } else {
59
            log_message('error', "Unable to secure a file lock for file at: " . $cache_path);
60
            return FALSE;
61
        }
62
        fclose($fp);
63
        return TRUE;
64
        //@chmod($cache_path, FILE_WRITE_MODE);
65
    }
66
 
67
    /**
68
     * cache the current output with the provided filename
69
     * @param type $filename The filename with the path to save the cache
70
     * @param type $expire Expiry time in seconds, if not passed cached for unlimited time
71
     */
72
    function save_output($filename, $expire='') {
73
        $CI = & get_instance();
74
        $data = $CI->output->get_output();
75
 
76
 
77
        $this->save_data($filename, $data, $expire);
78
    }
79
 
80
    // --------------------------------------------------------------------
81
 
82
    /**
83
     * Read from the cache
84
     *
85
     * @access	public
86
     * @return	void
87
     */
88
    function get_data($fileName) {
89
        $CI = & get_instance();
90
 
91
        $path = $CI->config->load('smartcache');
92
        $cache_path = $CI->config->item('cache_dir');
93
 
94
        if (preg_match('/^([0-9a-zA-Z]+\/)+/', $fileName, $matches) > 0) {
95
            $cache_path .= $matches[0];
96
        }
97
 
98
        $filepath = $cache_path . md5($fileName);
99
 
100
 
101
 
102
        if (!@file_exists($filepath)) {
103
            return FALSE;
104
        }
105
 
106
        if (!$fp = @fopen($filepath, FOPEN_READ)) {
107
            return FALSE;
108
        }
109
 
110
        flock($fp, LOCK_SH);
111
 
112
        $cache = '';
113
        if (filesize($filepath) > 0) {
114
            $cache = fread($fp, filesize($filepath));
115
        }
116
 
117
        flock($fp, LOCK_UN);
118
        fclose($fp);
119
 
120
        // Strip out the embedded timestamp
121
 
122
        if (!preg_match("/(\d*LGM-SMARTCACHE-IDENTIFIER)/", $cache, $match)) {
123
 
124
            return FALSE;
125
        }
126
 
127
 
128
 
129
        // Has the file expired? If so we'll delete it.
130
        if (trim(str_replace('LGM-SMARTCACHE-IDENTIFIER', '', $match['1'])) != "" && (time() >= trim(str_replace('LGM-SMARTCACHE-IDENTIFIER', '', $match['1'])))) {
131
            if (is_really_writable($cache_path)) {
132
                @unlink($filepath);
133
                return FALSE;
134
            }
135
        }
136
        $cache = str_replace($match['0'], '', $cache);
137
        return $cache;
138
    }
139
 
140
    /**
141
     * Get the cached data to the output buffer, if no cache available returns false
142
     * @param type $filename 
143
     */
144
    function get_to_output($filename) {
145
        $CI = & get_instance();
146
        $data = $this->get_data($filename);
147
        if ($data === FALSE) {
148
            return FALSE;
149
        } else {
150
            $CI->output->set_output($data);
151
            $this->save_data($filename, $data, $expire);
152
            return TRUE;
153
        }
154
    }
155
 
156
    /***
157
     * Deletes the cached data file with the given file name
158
     */
159
    function delete_data($fileName) {
160
        $CI = & get_instance();
161
        $path = $CI->config->load('smartcache');
162
        $cache_path = $CI->config->item('cache_dir');
163
        if (preg_match('/^([0-9a-zA-Z]+\/)+/', $fileName, $matches) > 0) {
164
            $cache_path .= $matches[0];
165
        }
166
        $filepath = $cache_path . md5($fileName);
167
 
168
 
169
        if (!@file_exists($filepath)) {
170
            return FALSE;
171
        }
172
        if (is_really_writable($cache_path)) {
173
            if (!is_dir($filepath))
174
                @unlink($filepath);
175
            return TRUE;
176
        }
177
        else
178
            return FALSE;
179
    }
180
 
181
    /***
182
     * Delete all the cache file in the given folder
183
     * 
184
     * $cache_path : The directory name to delete the cache files
185
     * $initiate : Send FALSE if shouldn't prepend with the config cache directory
186
     */
187
    function delete_all_data($cache_path, $initiate=TRUE) {
188
        if ($initiate) {
189
            $CI = & get_instance();
190
            $path = $CI->config->load('smartcache');
191
            $cache_path = $CI->config->item('cache_dir') . '/' . $cache_path;
192
        }
193
        if (is_really_writable($cache_path)) {
194
            $files = scandir($cache_path);
195
            $ignore = array('.', '..', 'index.html', '.htaccess');
196
            foreach ($files as $filepath) {
197
                if (in_array($filepath, $ignore))
198
                    continue;
199
                if (!is_dir($cache_path . '/' . $filepath)) {
200
                    @unlink($cache_path . '/' . $filepath);
201
                } else {
202
                    $this->delete_all_data($cache_path . '/' . $filepath, FALSE);
203
                }
204
            }
205
            return TRUE;
206
        }
207
        else
208
            return FALSE;
209
    }
210
 
211
}
212
 
213
// END Cache Class
214
 
215
/*
216
* 	Usage
217
* 	-----
218
*
219
*	$data = $this->cache->get_data('filename');
220
*	if($data)
221
*	{
222
*	    $this->output->set_output($data);
223
*	}
224
*	else
225
*	{
226
*	    //do your processing 
227
*	    $data = $this->output->get_output();
228
*	    $this->cache->save_data('filename',10);
229
*	} 
230
*
231
*/
232
/* End of file SimpleCache.php */
233
/* Location: ./application/libary/SimpleCache.php */