Dependency.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. abstract class Dependency extends \yii\base\BaseObject
  20. {
  21. /**
  22. * @var mixed the dependency data that is saved in cache and later is compared with the
  23. * latest dependency data.
  24. */
  25. public $data;
  26. /**
  27. * @var bool whether this dependency is reusable or not. True value means that dependent
  28. * data for this cache dependency will be generated only once per request. This allows you
  29. * to use the same cache dependency for multiple separate cache calls while generating the same
  30. * page without an overhead of re-evaluating dependency data each time. Defaults to false.
  31. */
  32. public $reusable = false;
  33. /**
  34. * @var array static storage of cached data for reusable dependencies.
  35. */
  36. private static $_reusableData = [];
  37. /**
  38. * Evaluates the dependency by generating and saving the data related with dependency.
  39. * This method is invoked by cache before writing data into it.
  40. * @param CacheInterface $cache the cache component that is currently evaluating this dependency
  41. */
  42. public function evaluateDependency($cache)
  43. {
  44. if ($this->reusable) {
  45. $hash = $this->generateReusableHash();
  46. if (!array_key_exists($hash, self::$_reusableData)) {
  47. self::$_reusableData[$hash] = $this->generateDependencyData($cache);
  48. }
  49. $this->data = self::$_reusableData[$hash];
  50. } else {
  51. $this->data = $this->generateDependencyData($cache);
  52. }
  53. }
  54. /**
  55. * Returns a value indicating whether the dependency has changed.
  56. * @deprecated since version 2.0.11. Will be removed in version 2.1. Use [[isChanged()]] instead.
  57. * @param CacheInterface $cache the cache component that is currently evaluating this dependency
  58. * @return bool whether the dependency has changed.
  59. */
  60. public function getHasChanged($cache)
  61. {
  62. return $this->isChanged($cache);
  63. }
  64. /**
  65. * Checks whether the dependency is changed.
  66. * @param CacheInterface $cache the cache component that is currently evaluating this dependency
  67. * @return bool whether the dependency has changed.
  68. * @since 2.0.11
  69. */
  70. public function isChanged($cache)
  71. {
  72. if ($this->reusable) {
  73. $hash = $this->generateReusableHash();
  74. if (!array_key_exists($hash, self::$_reusableData)) {
  75. self::$_reusableData[$hash] = $this->generateDependencyData($cache);
  76. }
  77. $data = self::$_reusableData[$hash];
  78. } else {
  79. $data = $this->generateDependencyData($cache);
  80. }
  81. return $data !== $this->data;
  82. }
  83. /**
  84. * Resets all cached data for reusable dependencies.
  85. */
  86. public static function resetReusableData()
  87. {
  88. self::$_reusableData = [];
  89. }
  90. /**
  91. * Generates a unique hash that can be used for retrieving reusable dependency data.
  92. * @return string a unique hash value for this cache dependency.
  93. * @see reusable
  94. */
  95. protected function generateReusableHash()
  96. {
  97. $data = $this->data;
  98. $this->data = null; // https://github.com/yiisoft/yii2/issues/3052
  99. $key = sha1(serialize($this));
  100. $this->data = $data;
  101. return $key;
  102. }
  103. /**
  104. * Generates the data needed to determine if dependency is changed.
  105. * Derived classes should override this method to generate the actual dependency data.
  106. * @param CacheInterface $cache the cache component that is currently evaluating this dependency
  107. * @return mixed the data needed to determine if dependency has been changed.
  108. */
  109. abstract protected function generateDependencyData($cache);
  110. }