PageRenderTime 27ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/framework/vendor/zend/Zend/Cache/Frontend/Function.php

http://zoop.googlecode.com/
PHP | 132 lines | 52 code | 10 blank | 70 comment | 9 complexity | 63fa7996920024f902a7781cb1be954c 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_Frontend
 18 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Function.php 20379 2010-01-18 14:40:57Z mabe $
 21 */
 22
 23
 24/**
 25 * @see Zend_Cache_Core
 26 */
 27require_once 'Zend/Cache/Core.php';
 28
 29
 30/**
 31 * @package    Zend_Cache
 32 * @subpackage Zend_Cache_Frontend
 33 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 34 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 35 */
 36class Zend_Cache_Frontend_Function extends Zend_Cache_Core
 37{
 38    /**
 39     * This frontend specific options
 40     *
 41     * ====> (boolean) cache_by_default :
 42     * - if true, function calls will be cached by default
 43     *
 44     * ====> (array) cached_functions :
 45     * - an array of function names which will be cached (even if cache_by_default = false)
 46     *
 47     * ====> (array) non_cached_functions :
 48     * - an array of function names which won't be cached (even if cache_by_default = true)
 49     *
 50     * @var array options
 51     */
 52    protected $_specificOptions = array(
 53        'cache_by_default' => true,
 54        'cached_functions' => array(),
 55        'non_cached_functions' => array()
 56    );
 57
 58    /**
 59     * Constructor
 60     *
 61     * @param  array $options Associative array of options
 62     * @return void
 63     */
 64    public function __construct(array $options = array())
 65    {
 66        while (list($name, $value) = each($options)) {
 67            $this->setOption($name, $value);
 68        }
 69        $this->setOption('automatic_serialization', true);
 70    }
 71
 72    /**
 73     * Main method : call the specified function or get the result from cache
 74     *
 75     * @param  string $name             Function name
 76     * @param  array  $parameters       Function parameters
 77     * @param  array  $tags             Cache tags
 78     * @param  int    $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
 79     * @param  int   $priority         integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
 80     * @return mixed Result
 81     */
 82    public function call($name, $parameters = array(), $tags = array(), $specificLifetime = false, $priority = 8)
 83    {
 84        $cacheBool1 = $this->_specificOptions['cache_by_default'];
 85        $cacheBool2 = in_array($name, $this->_specificOptions['cached_functions']);
 86        $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_functions']);
 87        $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
 88        if (!$cache) {
 89            // We do not have not cache
 90            return call_user_func_array($name, $parameters);
 91        }
 92
 93        $id = $this->_makeId($name, $parameters);
 94        if ( ($rs = $this->load($id)) && isset($rs[0], $rs[1])) {
 95            // A cache is available
 96            $output = $rs[0];
 97            $return = $rs[1];
 98        } else {
 99            // A cache is not available (or not valid for this frontend)
100            ob_start();
101            ob_implicit_flush(false);
102            $return = call_user_func_array($name, $parameters);
103            $output = ob_get_contents();
104            ob_end_clean();
105            $data = array($output, $return);
106            $this->save($data, $id, $tags, $specificLifetime, $priority);
107        }
108
109        echo $output;
110        return $return;
111    }
112
113    /**
114     * Make a cache id from the function name and parameters
115     *
116     * @param  string $name       Function name
117     * @param  array  $parameters Function parameters
118     * @throws Zend_Cache_Exception
119     * @return string Cache id
120     */
121    private function _makeId($name, $parameters)
122    {
123        if (!is_string($name)) {
124            Zend_Cache::throwException('Incorrect function name');
125        }
126        if (!is_array($parameters)) {
127            Zend_Cache::throwException('parameters argument must be an array');
128        }
129        return md5($name . serialize($parameters));
130    }
131
132}