Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Xcache.php  2007-11-19 14:47:59Z demongloom $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Xcache cache driver
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Cache
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.0
30
 * @version     $Revision: $
31
 * @author      Dmitry Bakaleinik (dima@snaiper.net)
32
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
33
 * @author      Jonathan H. Wage <jonwage@gmail.com>
34
 */
35
class Doctrine_Cache_Xcache extends Doctrine_Cache_Driver
36
{
37
    /**
38
     * constructor
39
     *
40
     * @param array $options        associative array of cache driver options
41
     */
42
    public function __construct($options = array())
43
    {
44
        if ( ! extension_loaded('xcache') ) {
45
            throw new Doctrine_Cache_Exception('In order to use Xcache driver, the xcache extension must be loaded.');
46
        }
47
 
48
        parent::__construct($options);
49
    }
50
 
51
    /**
52
     * Test if a cache record exists for the passed id
53
     *
54
     * @param string $id cache id
55
     * @return mixed  Returns either the cached data or false
56
     */
57
    protected function _doFetch($id, $testCacheValidity = true)
58
    {
59
        return $this->_doContains($id) ? xcache_get($id) : false;
60
    }
61
 
62
    /**
63
     * Test if a cache is available or not (for the given id)
64
     *
65
     * @param string $id cache id
66
     * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
67
     */
68
    protected function _doContains($id)
69
    {
70
        return xcache_isset($id);
71
    }
72
 
73
    /**
74
     * Save a cache record directly. This method is implemented by the cache
75
     * drivers and used in Doctrine_Cache_Driver::save()
76
     *
77
     * @param string $id        cache id
78
     * @param string $data      data to cache
79
     * @param int $lifeTime     if != false, set a specific lifetime for this cache record (null => infinite lifeTime)
80
     * @return boolean true if no problem
81
     */
82
    protected function _doSave($id, $data, $lifeTime = false)
83
    {
84
        return xcache_set($id, $data, $lifeTime);
85
    }
86
 
87
    /**
88
     * Remove a cache record directly. This method is implemented by the cache
89
     * drivers and used in Doctrine_Cache_Driver::delete()
90
     *
91
     * @param string $id cache id
92
     * @return boolean true if no problem
93
     */
94
    protected function _doDelete($id)
95
    {
96
        return xcache_unset($id);
97
    }
98
 
99
    /**
100
     * Fetch an array of all keys stored in cache
101
     *
102
     * @return array Returns the array of cache keys
103
     */
104
    protected function _getCacheKeys()
105
    {
106
        $this->checkAuth();
107
        $keys = array();
108
        for ($i = 0, $count = xcache_count(XC_TYPE_VAR); $i < $count; $i++) {
109
            $entries = xcache_list(XC_TYPE_VAR, $i);
110
            if (is_array($entries['cache_list'])) {
111
                foreach ($entries['cache_list'] as $entry) {
112
                    $keys[] = $entry['name'];
113
                }
114
            }
115
        }
116
        return $keys;
117
    }
118
 
119
    /**
120
     * Checks that xcache.admin.enable_auth is Off
121
     *
122
     * @throws Doctrine_Cache_Exception When xcache.admin.enable_auth is On
123
     * @return void
124
     */
125
    protected function checkAuth()
126
    {
127
        if (ini_get('xcache.admin.enable_auth')) {
128
            throw new Doctrine_Cache_Exception('To use all features of Doctrine_Cache_Xcache, you must set "xcache.admin.enable_auth" to "Off" in your php.ini.');
129
        }
130
    }
131
}