PageRenderTime 38ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/www/system/library/Zend/Cache/Frontend/Function.php

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