PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Cache/Frontend/Function.php

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