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.
17 * @subpackage Zend_Cache_Frontend
18 * @copyright Copyright (c) 2005-2009 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 16541 2009-07-07 06:59:03Z bkarwin $
25 * @see Zend_Cache_Core
27 require_once 'Zend/Cache/Core.php';
32 * @subpackage Zend_Cache_Frontend
33 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
36 class Zend_Cache_Frontend_Function
extends Zend_Cache_Core
39 * This frontend specific options
41 * ====> (boolean) cache_by_default :
42 * - if true, function calls will be cached by default
44 * ====> (array) cached_functions :
45 * - an array of function names which will be cached (even if cache_by_default = false)
47 * ====> (array) non_cached_functions :
48 * - an array of function names which won't be cached (even if cache_by_default = true)
52 protected $_specificOptions = array(
53 'cache_by_default' => true,
54 'cached_functions' => array(),
55 'non_cached_functions' => array()
61 * @param array $options Associative array of options
64 public function __construct(array $options = array())
66 while (list($name, $value) = each($options)) {
67 $this->setOption($name, $value);
69 $this->setOption('automatic_serialization', true);
73 * Main method : call the specified function or get the result from cache
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
82 public function call($name, $parameters = array(), $tags = array(), $specificLifetime = false, $priority = 8)
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));
89 // We do not have not cache
90 return call_user_func_array($name, $parameters);
92 $id = $this->_makeId($name, $parameters);
93 if ($this->test($id)) {
94 // A cache is available
95 $result = $this->load($id);
99 // A cache is not available
101 ob_implicit_flush(false);
102 $return = call_user_func_array($name, $parameters);
103 $output = ob_get_contents();
105 $data = array($output, $return);
106 $this->save($data, $id, $tags, $specificLifetime, $priority);
113 * Make a cache id from the function name and parameters
115 * @param string $name Function name
116 * @param array $parameters Function parameters
117 * @throws Zend_Cache_Exception
118 * @return string Cache id
120 private function _makeId($name, $parameters)
122 if (!is_string($name)) {
123 Zend_Cache
::throwException('Incorrect function name');
125 if (!is_array($parameters)) {
126 Zend_Cache
::throwException('parameters argument must be an array');
128 return md5($name . serialize($parameters));