root/include/class.memcached.php

Revision 211, 5.0 kB (checked in by Nafania, 3 years ago)

--

Line 
1 <?php
2
3 class adv_cache {</span>
4 <span class="code-keyword">
5     var $vars = array();
6     var $var_expires = array();
7     var $is_modified = false;
8
9     var $sql_rowset = array();
10     var $sql_row_pointer = array();
11
12     var $memcache;
13     var $compress;
14     var $cache_prefix;
15
16     /**
17      *
18      * @param string $host - � �� memcached
19      * @param int $port - ����emcached
20      * @param int $compress - [0,1], �� � � �� ��
21      * ���� ���     */
22     function adv_cache($host = '127.0.0.1', $port = 11211, $compress = 0)
23     {
24         global $config;
25
26         $this->memcache = memcache_connect($host, $port) or trigger_error('Cant connect to memcache server', E_USER_ERROR);
27         $this->compress = ($compress) ? MEMCACHE_COMPRESSED : 0;
28         $this->cache_prefix = 'tbdevsz/';
29     }
30
31     /**
32     * Load global cache
33     */
34     function load()
35     {
36         if ( $return = $this->_exists('_global') ) {
37             $this->vars = $return['vars'];
38             $this->var_expires = $return['var_expires'];
39         }
40         else {
41             return false;
42         }
43
44         return true;
45     }
46
47     /**
48     * Unload cache object
49     */
50     function unload()
51     {
52         $this->save();
53         unset($this->vars);
54         unset($this->var_expires);
55         unset($this->sql_rowset);
56         unset($this->sql_row_pointer);
57         $this->vars = array();
58         $this->var_expires = array();
59         $this->sql_rowset = array();
60         $this->sql_row_pointer = array();
61
62         memcache_close($this->memcache);
63     }
64
65     /**
66     * Save modified objects
67     */
68     function save()
69     {
70         if (!$this->is_modified)
71         {
72             return;
73         }
74         $this->put('_global', array('vars' => $this->vars, 'var_expires' => $this->var_expires) );
75         $this->is_modified = false;
76     }
77
78     /**
79     * Tidy cache
80     */
81     function tidy()
82     {
83         return true;
84     }
85
86     /**
87     * Get saved cache object
88     */
89     function get ( $var_name )
90     {
91         if ($var_name[0] == '_')
92         {
93             if ( $return = $this->_exists($var_name) ) {
94                 return $return;
95             }
96             else {
97                 return false;
98             }
99         }
100         else
101         {
102             return ( $return = $this->_exists($var_name)) ? $this->vars[$var_name] : false;
103         }
104     }
105
106     /**
107     * Put data into cache
108     */
109     function put ( $var_name, $var, $ttl = 2592000 ) {
110
111         $ttl = ( $ttl > 2592000 ) ? 2592000 : $ttl;
112
113         if ($var_name[0] == '_')
114         {
115             memcache_set($this->memcache, $this->cache_prefix . 'data' . $var_name, $var, $this->compress, $ttl);
116         }
117         else
118         {
119             $this->vars[$var_name] = $var;
120             $this->var_expires[$var_name] = time() + $ttl;
121             $this->is_modified = true;
122         }
123     }
124
125     /**
126     * Purge cache data
127     */
128     function purge () {
129         memcache_flush($this->memcache);
130     }
131
132     /**
133     * Destroy cache data
134     */
135     function destroy($var_name, $table = '') {
136
137         if ($var_name == 'sql' && !empty($table)) {
138             $table = strtolower($table);
139             $this->purge();
140             //memcache_delete($this->memcache, $this->cache_prefix . 'sql/' . $table);
141         }
142
143         if (!$this->_exists($var_name))
144         {
145             return;
146         }
147
148         if ($var_name[0] == '_')
149         {
150             memcache_delete($this->memcache, $this->cache_prefix . 'data' . $var_name);
151         }
152
153         else if (isset($this->vars[$var_name]))
154         {
155             $this->is_modified = true;
156             unset($this->vars[$var_name]);
157             unset($this->var_expires[$var_name]);
158
159             // We save here to let the following cache hits succeed
160             $this->save();
161         }
162     }
163
164     /**
165     * Check if a given cache entry exist
166     */
167     function _exists($var_name)
168     {
169         if ($var_name[0] == '_')
170         {
171             return memcache_get($this->memcache, $this->cache_prefix . 'data' . $var_name);
172         }
173         else
174         {
175             if (!sizeof($this->vars))
176             {
177                 $this->load();
178             }
179
180             if (!isset($this->var_expires[$var_name]))
181             {
182                 return false;
183             }
184
185             return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]);
186         }
187     }
188
189     /**
190     * Load cached sql query
191     */
192     function sql_load($query)
193     {
194         // Remove extra spaces and tabs
195         $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
196         $query_id = sizeof($this->sql_rowset);
197
198         preg_match('/FROM (.*?)( |$)/si', $query, $matches);
199         $table_name = strtolower(trim($matches[1]));
200
201         $filename = $this->cache_prefix . 'sql/' . $table_name . '_' . md5($query);
202
203         if ( ($this->sql_rowset[$query_id] = memcache_get($this->memcache, $filename)) === false )
204         {
205             return false;
206         }
207
208         $this->sql_row_pointer[$query_id] = 0;
209
210         return $query_id;
211     }
212
213     function sql_save($query, &$query_result, $ttl)
214     {
215
216         global $db;
217
218         $ttl = ( $ttl > 2592000 ) ? 2592000 : $ttl;
219
220         // Remove extra spaces and tabs
221         $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
222
223         preg_match('/FROM (.*?)( |$)/si', $query, $matches);
224         $table_name = strtolower(trim($matches[1]));
225         $filename = $this->cache_prefix . 'sql/' . $table_name . '_' . md5($query);
226
227         $query_id = sizeof($this->sql_rowset);
228         $this->sql_rowset[$query_id] = array();
229         $this->sql_row_pointer[$query_id] = 0;
230
231         while ( $row = $db->sql_fetchrow($query_result) ) {
232             $this->sql_rowset[$query_id][] = $row;
233         }
234         $db->sql_freeresult($query_result);
235
236         memcache_set($this->memcache, $filename, $this->sql_rowset[$query_id], $this->compress, $ttl);
237
238         $query_result = $query_id;
239     }
240
241 }
242
243 ?>
Note: See TracBrowser for help on using the browser.