Dependency.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Object
  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 Cache $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. */
  58. public function getHasChanged($cache)
  59. {
  60. return $this->isChanged($cache);
  61. }
  62. /**
  63. * Checks whether the dependency is changed
  64. * @param Cache $cache the cache component that is currently evaluating this dependency
  65. * @return bool whether the dependency has changed.
  66. * @since 2.0.11
  67. */
  68. public function isChanged($cache)
  69. {
  70. if ($this->reusable) {
  71. $hash = $this->generateReusableHash();
  72. if (!array_key_exists($hash, self::$_reusableData)) {
  73. self::$_reusableData[$hash] = $this->generateDependencyData($cache);
  74. }
  75. $data = self::$_reusableData[$hash];
  76. } else {
  77. $data = $this->generateDependencyData($cache);
  78. }
  79. return $data !== $this->data;
  80. }
  81. /**
  82. * Resets all cached data for reusable dependencies.
  83. */
  84. public static function resetReusableData()
  85. {
  86. self::$_reusableData = [];
  87. }
  88. /**
  89. * Generates a unique hash that can be used for retrieving reusable dependency data.
  90. * @return string a unique hash value for this cache dependency.
  91. * @see reusable
  92. */
  93. protected function generateReusableHash()
  94. {
  95. $data = $this->data;
  96. $this->data = null; // https://github.com/yiisoft/yii2/issues/3052
  97. $key = sha1(serialize($this));
  98. $this->data = $data;
  99. return $key;
  100. }
  101. /**
  102. * Generates the data needed to determine if dependency is changed.
  103. * Derived classes should override this method to generate the actual dependency data.
  104. * @param Cache $cache the cache component that is currently evaluating this dependency
  105. * @return mixed the data needed to determine if dependency has been changed.
  106. */
  107. abstract protected function generateDependencyData($cache);
  108. }