Dependency.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\caching;
  8. /**
  9. * Dependency is the base class for cache dependency classes.
  10. *
  11. * Child classes should override its [[generateDependencyData()]] for generating
  12. * the actual dependency data.
  13. *
  14. * @author Qiang Xue <[email protected]>
  15. * @since 2.0
  16. */
  17. abstract class Dependency extends \yii\base\Object
  18. {
  19. /**
  20. * @var mixed the dependency data that is saved in cache and later is compared with the
  21. * latest dependency data.
  22. */
  23. public $data;
  24. /**
  25. * @var boolean whether this dependency is reusable or not. True value means that dependent
  26. * data for this cache dependency will be generated only once per request. This allows you
  27. * to use the same cache dependency for multiple separate cache calls while generating the same
  28. * page without an overhead of re-evaluating dependency data each time. Defaults to false.
  29. */
  30. public $reusable = false;
  31. /**
  32. * @var array static storage of cached data for reusable dependencies.
  33. */
  34. private static $_reusableData = [];
  35. /**
  36. * @var string a unique hash value for this cache dependency.
  37. */
  38. private $_hash;
  39. /**
  40. * Evaluates the dependency by generating and saving the data related with dependency.
  41. * This method is invoked by cache before writing data into it.
  42. * @param Cache $cache the cache component that is currently evaluating this dependency
  43. */
  44. public function evaluateDependency($cache)
  45. {
  46. if (!$this->reusable) {
  47. $this->data = $this->generateDependencyData($cache);
  48. } else {
  49. if ($this->_hash === null) {
  50. $this->_hash = sha1(serialize($this));
  51. }
  52. if (!array_key_exists($this->_hash, self::$_reusableData)) {
  53. self::$_reusableData[$this->_hash] = $this->generateDependencyData($cache);
  54. }
  55. $this->data = self::$_reusableData[$this->_hash];
  56. }
  57. }
  58. /**
  59. * Returns a value indicating whether the dependency has changed.
  60. * @param Cache $cache the cache component that is currently evaluating this dependency
  61. * @return boolean whether the dependency has changed.
  62. */
  63. public function getHasChanged($cache)
  64. {
  65. if (!$this->reusable) {
  66. return $this->generateDependencyData($cache) !== $this->data;
  67. } else {
  68. if ($this->_hash === null) {
  69. $this->_hash = sha1(serialize($this));
  70. }
  71. if (!array_key_exists($this->_hash, self::$_reusableData)) {
  72. self::$_reusableData[$this->_hash] = $this->generateDependencyData($cache);
  73. }
  74. return self::$_reusableData[$this->_hash] !== $this->data;
  75. }
  76. }
  77. /**
  78. * Resets all cached data for reusable dependencies.
  79. */
  80. public static function resetReusableData()
  81. {
  82. self::$_reusableData = [];
  83. }
  84. /**
  85. * Generates the data needed to determine if dependency has been changed.
  86. * Derived classes should override this method to generate the actual dependency data.
  87. * @param Cache $cache the cache component that is currently evaluating this dependency
  88. * @return mixed the data needed to determine if dependency has been changed.
  89. */
  90. abstract protected function generateDependencyData($cache);
  91. }