summaryrefslogtreecommitdiff
path: root/kohana/libraries/drivers/Cache/Xcache.php
blob: 6254bbb61e33e1c60cabc190d961adfe3d978166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Xcache Cache driver.
 *
 * $Id: Xcache.php 4046 2009-03-05 19:23:29Z Shadowhand $
 *
 * @package    Cache
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class Cache_Xcache_Driver implements Cache_Driver {

	public function __construct()
	{
		if ( ! extension_loaded('xcache'))
			throw new Kohana_Exception('cache.extension_not_loaded', 'xcache');
	}

	public function get($id)
	{
		if (xcache_isset($id))
			return xcache_get($id);

		return NULL;
	}

	public function set($id, $data, array $tags = NULL, $lifetime)
	{
		if ( ! empty($tags))
		{
			Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver');
		}

		return xcache_set($id, $data, $lifetime);
	}

	public function find($tag)
	{
		Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver');
		return FALSE;
	}

	public function delete($id, $tag = FALSE)
	{
		if ($tag !== FALSE)
		{
			Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver');
			return TRUE;
		}
		elseif ($id !== TRUE)
		{
			if (xcache_isset($id))
				return xcache_unset($id);

			return FALSE;
		}
		else
		{
			// Do the login
			$this->auth();
			$result = TRUE;
			for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
			{
				if (xcache_clear_cache(XC_TYPE_VAR, $i) !== NULL)
				{
					$result = FALSE;
					break;
				}
			}

			// Undo the login
			$this->auth(TRUE);
			return $result;
		}

		return TRUE;
	}

	public function delete_expired()
	{
		return TRUE;
	}

	private function auth($reverse = FALSE)
	{
		static $backup = array();

		$keys = array('PHP_AUTH_USER', 'PHP_AUTH_PW');

		foreach ($keys as $key)
		{
			if ($reverse)
			{
				if (isset($backup[$key]))
				{
					$_SERVER[$key] = $backup[$key];
					unset($backup[$key]);
				}
				else
				{
					unset($_SERVER[$key]);
				}
			}
			else
			{
				$value = getenv($key);

				if ( ! empty($value))
				{
					$backup[$key] = $value;
				}

				$_SERVER[$key] = Kohana::config('cache_xcache.'.$key);
			}
		}
	}

} // End Cache Xcache Driver