Ticket #331: Cache_Xcache.php

File Cache_Xcache.php, 1.8 kB (added by alexsancho, 12 months ago)

Xcache Cache Driver

Line 
1<?php defined('SYSPATH') or die('No direct script access.');
2/**
3 * xcache-based Cache driver.
4 *
5 * @package    Cache
6 * @author     Kohana Team
7 * @copyright  (c) 2007-2008 Kohana Team
8 * @license    http://kohanaphp.com/license.html
9 */
10class Cache_Xcache_Driver implements Cache_Driver {
11
12    public function __construct()
13    {
14        if ( ! extension_loaded('xcache'))
15            throw new Kohana_Exception('cache.extension_not_loaded', 'xcache');
16    }
17
18    public function get($id)
19    {
20        if (xcache_isset($id))
21            return xcache_get($id);
22           
23        return false;
24    }
25
26    public function set($id, $data, $tags, $expiration)
27    {
28        count($tags) and Log::add('error', 'Cache: tags are unsupported by the Xcache driver');
29
30        return xcache_set($id, $data, $expiration);
31    }
32
33    public function find($tag)
34    {
35        return false;
36    }
37
38    public function delete($id, $tag = false)
39    {
40        if ($id === true)
41        {
42            return apc_clear_cache('user');
43        }
44        elseif ($tag == FALSE)
45        {
46            $result = true;
47            $this->auth();
48            for($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
49            {
50                if ( ! xcache_clear_cache(XC_TYPE_VAR, $i))
51                {
52                    $result = false;
53                    break;
54                }
55            }
56           
57            $this->auth(true);
58            return $result;
59        }
60        else
61        {
62            return true;
63        }
64    }
65
66    public function delete_expired()
67    {
68        return true;
69    }
70
71    private function auth($reverse = false)
72    {
73        static $backup = array();
74   
75        $keys = array('PHP_AUTH_USER', 'PHP_AUTH_PW');
76       
77        foreach ($keys as $key)
78        {
79            if ($reverse)
80            {
81                if (isset($backup[$key]))
82                {
83                    $_SERVER[$key] = $backup[$key];
84                    unset($backup[$key]);
85                }
86                else
87                {
88                    unset($_SERVER[$key]);
89                }
90            }
91            else
92            {
93                $value = env($key);
94               
95                if ( ! empty($value))
96                {
97                    $backup[$key] = $value;
98                }
99                $varName = '__' . $key;
100                $_SERVER[$key] = Config::item('cache_xcache.'.$varName);
101            }
102        }
103    }
104
105} // End Cache Xcache Driver