You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 line
3.6KB

  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 <qiang.xue@gmail.com>
  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. * Evaluates the dependency by generating and saving the data related with dependency.
  37. * This method is invoked by cache before writing data into it.
  38. * @param Cache $cache the cache component that is currently evaluating this dependency
  39. */
  40. public function evaluateDependency($cache)
  41. {
  42. if ($this->reusable) {
  43. $hash = $this->generateReusableHash();
  44. if (!array_key_exists($hash, self::$_reusableData)) {
  45. self::$_reusableData[$hash] = $this->generateDependencyData($cache);
  46. }
  47. $this->data = self::$_reusableData[$hash];
  48. } else {
  49. $this->data = $this->generateDependencyData($cache);
  50. }
  51. }
  52. /**
  53. * Returns a value indicating whether the dependency has changed.
  54. * @param Cache $cache the cache component that is currently evaluating this dependency
  55. * @return boolean whether the dependency has changed.
  56. */
  57. public function getHasChanged($cache)
  58. {
  59. if ($this->reusable) {
  60. $hash = $this->generateReusableHash();
  61. if (!array_key_exists($hash, self::$_reusableData)) {
  62. self::$_reusableData[$hash] = $this->generateDependencyData($cache);
  63. }
  64. $data = self::$_reusableData[$hash];
  65. } else {
  66. $data = $this->generateDependencyData($cache);
  67. }
  68. return $data !== $this->data;
  69. }
  70. /**
  71. * Resets all cached data for reusable dependencies.
  72. */
  73. public static function resetReusableData()
  74. {
  75. self::$_reusableData = [];
  76. }
  77. /**
  78. * Generates a unique hash that can be used for retrieving reusable dependency data.
  79. * @return string a unique hash value for this cache dependency.
  80. * @see reusable
  81. */
  82. protected function generateReusableHash()
  83. {
  84. $data = $this->data;
  85. $this->data = null; // https://github.com/yiisoft/yii2/issues/3052
  86. $key = sha1(serialize($this));
  87. $this->data = $data;
  88. return $key;
  89. }
  90. /**
  91. * Generates the data needed to determine if dependency has been changed.
  92. * Derived classes should override this method to generate the actual dependency data.
  93. * @param Cache $cache the cache component that is currently evaluating this dependency
  94. * @return mixed the data needed to determine if dependency has been changed.
  95. */
  96. abstract protected function generateDependencyData($cache);
  97. }