PageRenderTime 98ms CodeModel.GetById 56ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/Cache/Backend/Xcache.php

https://bitbucket.org/goldie/zend-framework1
PHP | 221 lines | 97 code | 17 blank | 107 comment | 13 complexity | d09e7bc368db0e81b33147aa1bda3e12 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Cache
 17 * @subpackage Zend_Cache_Backend
 18 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Xcache.php 23775 2011-03-01 17:25:24Z ralph $
 21 */
 22
 23
 24/**
 25 * @see Zend_Cache_Backend_Interface
 26 */
 27require_once 'Zend/Cache/Backend/Interface.php';
 28
 29/**
 30 * @see Zend_Cache_Backend
 31 */
 32require_once 'Zend/Cache/Backend.php';
 33
 34
 35/**
 36 * @package    Zend_Cache
 37 * @subpackage Zend_Cache_Backend
 38 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 39 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 40 */
 41class Zend_Cache_Backend_Xcache extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
 42{
 43
 44    /**
 45     * Log message
 46     */
 47    const TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND = 'Zend_Cache_Backend_Xcache::clean() : tags are unsupported by the Xcache backend';
 48    const TAGS_UNSUPPORTED_BY_SAVE_OF_XCACHE_BACKEND =  'Zend_Cache_Backend_Xcache::save() : tags are unsupported by the Xcache backend';
 49
 50    /**
 51     * Available options
 52     *
 53     * =====> (string) user :
 54     * xcache.admin.user (necessary for the clean() method)
 55     *
 56     * =====> (string) password :
 57     * xcache.admin.pass (clear, not MD5) (necessary for the clean() method)
 58     *
 59     * @var array available options
 60     */
 61    protected $_options = array(
 62        'user' => null,
 63        'password' => null
 64    );
 65
 66    /**
 67     * Constructor
 68     *
 69     * @param  array $options associative array of options
 70     * @throws Zend_Cache_Exception
 71     * @return void
 72     */
 73    public function __construct(array $options = array())
 74    {
 75        if (!extension_loaded('xcache')) {
 76            Zend_Cache::throwException('The xcache extension must be loaded for using this backend !');
 77        }
 78        parent::__construct($options);
 79    }
 80
 81    /**
 82     * Test if a cache is available for the given id and (if yes) return it (false else)
 83     *
 84     * WARNING $doNotTestCacheValidity=true is unsupported by the Xcache backend
 85     *
 86     * @param  string  $id                     cache id
 87     * @param  boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
 88     * @return string cached datas (or false)
 89     */
 90    public function load($id, $doNotTestCacheValidity = false)
 91    {
 92        if ($doNotTestCacheValidity) {
 93            $this->_log("Zend_Cache_Backend_Xcache::load() : \$doNotTestCacheValidity=true is unsupported by the Xcache backend");
 94        }
 95        $tmp = xcache_get($id);
 96        if (is_array($tmp)) {
 97            return $tmp[0];
 98        }
 99        return false;
100    }
101
102    /**
103     * Test if a cache is available or not (for the given id)
104     *
105     * @param  string $id cache id
106     * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
107     */
108    public function test($id)
109    {
110        if (xcache_isset($id)) {
111            $tmp = xcache_get($id);
112            if (is_array($tmp)) {
113                return $tmp[1];
114            }
115        }
116        return false;
117    }
118
119    /**
120     * Save some string datas into a cache record
121     *
122     * Note : $data is always "string" (serialization is done by the
123     * core not by the backend)
124     *
125     * @param string $data datas to cache
126     * @param string $id cache id
127     * @param array $tags array of strings, the cache record will be tagged by each string entry
128     * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
129     * @return boolean true if no problem
130     */
131    public function save($data, $id, $tags = array(), $specificLifetime = false)
132    {
133        $lifetime = $this->getLifetime($specificLifetime);
134        $result = xcache_set($id, array($data, time()), $lifetime);
135        if (count($tags) > 0) {
136            $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_XCACHE_BACKEND);
137        }
138        return $result;
139    }
140
141    /**
142     * Remove a cache record
143     *
144     * @param  string $id cache id
145     * @return boolean true if no problem
146     */
147    public function remove($id)
148    {
149        return xcache_unset($id);
150    }
151
152    /**
153     * Clean some cache records
154     *
155     * Available modes are :
156     * 'all' (default)  => remove all cache entries ($tags is not used)
157     * 'old'            => unsupported
158     * 'matchingTag'    => unsupported
159     * 'notMatchingTag' => unsupported
160     * 'matchingAnyTag' => unsupported
161     *
162     * @param  string $mode clean mode
163     * @param  array  $tags array of tags
164     * @throws Zend_Cache_Exception
165     * @return boolean true if no problem
166     */
167    public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
168    {
169        switch ($mode) {
170            case Zend_Cache::CLEANING_MODE_ALL:
171                // Necessary because xcache_clear_cache() need basic authentification
172                $backup = array();
173                if (isset($_SERVER['PHP_AUTH_USER'])) {
174                    $backup['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_USER'];
175                }
176                if (isset($_SERVER['PHP_AUTH_PW'])) {
177                    $backup['PHP_AUTH_PW'] = $_SERVER['PHP_AUTH_PW'];
178                }
179                if ($this->_options['user']) {
180                    $_SERVER['PHP_AUTH_USER'] = $this->_options['user'];
181                }
182                if ($this->_options['password']) {
183                    $_SERVER['PHP_AUTH_PW'] = $this->_options['password'];
184                }
185
186                $cnt = xcache_count(XC_TYPE_VAR);
187                for ($i=0; $i < $cnt; $i++) {
188                    xcache_clear_cache(XC_TYPE_VAR, $i);
189                }
190
191                if (isset($backup['PHP_AUTH_USER'])) {
192                    $_SERVER['PHP_AUTH_USER'] = $backup['PHP_AUTH_USER'];
193                    $_SERVER['PHP_AUTH_PW'] = $backup['PHP_AUTH_PW'];
194                }
195                return true;
196                break;
197            case Zend_Cache::CLEANING_MODE_OLD:
198                $this->_log("Zend_Cache_Backend_Xcache::clean() : CLEANING_MODE_OLD is unsupported by the Xcache backend");
199                break;
200            case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
201            case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
202            case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
203                $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND);
204                break;
205            default:
206                Zend_Cache::throwException('Invalid mode for clean() method');
207                break;
208        }
209    }
210
211    /**
212     * Return true if the automatic cleaning is available for the backend
213     *
214     * @return boolean
215     */
216    public function isAutomaticCleaningAvailable()
217    {
218        return false;
219    }
220
221}