Initial Kohana install
[speedfreak] / Server / system / libraries / drivers / Cache / Xcache.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * Xcache Cache driver.
4  *
5  * $Id: Xcache.php 4046 2009-03-05 19:23:29Z Shadowhand $
6  *
7  * @package    Cache
8  * @author     Kohana Team
9  * @copyright  (c) 2007-2008 Kohana Team
10  * @license    http://kohanaphp.com/license.html
11  */
12 class Cache_Xcache_Driver implements Cache_Driver {
13
14         public function __construct()
15         {
16                 if ( ! extension_loaded('xcache'))
17                         throw new Kohana_Exception('cache.extension_not_loaded', 'xcache');
18         }
19
20         public function get($id)
21         {
22                 if (xcache_isset($id))
23                         return xcache_get($id);
24
25                 return NULL;
26         }
27
28         public function set($id, $data, array $tags = NULL, $lifetime)
29         {
30                 if ( ! empty($tags))
31                 {
32                         Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver');
33                 }
34
35                 return xcache_set($id, $data, $lifetime);
36         }
37
38         public function find($tag)
39         {
40                 Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver');
41                 return FALSE;
42         }
43
44         public function delete($id, $tag = FALSE)
45         {
46                 if ($tag !== FALSE)
47                 {
48                         Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver');
49                         return TRUE;
50                 }
51                 elseif ($id !== TRUE)
52                 {
53                         if (xcache_isset($id))
54                                 return xcache_unset($id);
55
56                         return FALSE;
57                 }
58                 else
59                 {
60                         // Do the login
61                         $this->auth();
62                         $result = TRUE;
63                         for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
64                         {
65                                 if (xcache_clear_cache(XC_TYPE_VAR, $i) !== NULL)
66                                 {
67                                         $result = FALSE;
68                                         break;
69                                 }
70                         }
71
72                         // Undo the login
73                         $this->auth(TRUE);
74                         return $result;
75                 }
76
77                 return TRUE;
78         }
79
80         public function delete_expired()
81         {
82                 return TRUE;
83         }
84
85         private function auth($reverse = FALSE)
86         {
87                 static $backup = array();
88
89                 $keys = array('PHP_AUTH_USER', 'PHP_AUTH_PW');
90
91                 foreach ($keys as $key)
92                 {
93                         if ($reverse)
94                         {
95                                 if (isset($backup[$key]))
96                                 {
97                                         $_SERVER[$key] = $backup[$key];
98                                         unset($backup[$key]);
99                                 }
100                                 else
101                                 {
102                                         unset($_SERVER[$key]);
103                                 }
104                         }
105                         else
106                         {
107                                 $value = getenv($key);
108
109                                 if ( ! empty($value))
110                                 {
111                                         $backup[$key] = $value;
112                                 }
113
114                                 $_SERVER[$key] = Kohana::config('cache_xcache.'.$key);
115                         }
116                 }
117         }
118
119 } // End Cache Xcache Driver