Dependency.php 3.58 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8 9
namespace yii\caching;

Qiang Xue committed
10
/**
Qiang Xue committed
11
 * Dependency is the base class for cache dependency classes.
Qiang Xue committed
12
 *
Qiang Xue committed
13 14
 * Child classes should override its [[generateDependencyData()]] for generating
 * the actual dependency data.
Qiang Xue committed
15 16
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
17
 * @since 2.0
Qiang Xue committed
18
 */
Qiang Xue committed
19
abstract class Dependency extends \yii\base\Object
Qiang Xue committed
20
{
21 22 23 24 25 26 27 28 29 30 31 32
    /**
     * @var mixed the dependency data that is saved in cache and later is compared with the
     * latest dependency data.
     */
    public $data;
    /**
     * @var boolean whether this dependency is reusable or not. True value means that dependent
     * data for this cache dependency will be generated only once per request. This allows you
     * to use the same cache dependency for multiple separate cache calls while generating the same
     * page without an overhead of re-evaluating dependency data each time. Defaults to false.
     */
    public $reusable = false;
33

34 35 36 37
    /**
     * @var array static storage of cached data for reusable dependencies.
     */
    private static $_reusableData = [];
Qiang Xue committed
38

39

40 41 42 43 44 45 46
    /**
     * Evaluates the dependency by generating and saving the data related with dependency.
     * This method is invoked by cache before writing data into it.
     * @param Cache $cache the cache component that is currently evaluating this dependency
     */
    public function evaluateDependency($cache)
    {
47 48 49 50
        if ($this->reusable) {
            $hash = $this->generateReusableHash();
            if (!array_key_exists($hash, self::$_reusableData)) {
                self::$_reusableData[$hash] = $this->generateDependencyData($cache);
51
            }
52 53 54
            $this->data = self::$_reusableData[$hash];
        } else {
            $this->data = $this->generateDependencyData($cache);
55 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 62 63 64
     * @return boolean whether the dependency has changed.
     */
    public function getHasChanged($cache)
    {
65 66 67 68
        if ($this->reusable) {
            $hash = $this->generateReusableHash();
            if (!array_key_exists($hash, self::$_reusableData)) {
                self::$_reusableData[$hash] = $this->generateDependencyData($cache);
69
            }
70 71 72
            $data = self::$_reusableData[$hash];
        } else {
            $data = $this->generateDependencyData($cache);
73
        }
74
        return $data !== $this->data;
75
    }
76

77 78 79 80 81 82 83
    /**
     * Resets all cached data for reusable dependencies.
     */
    public static function resetReusableData()
    {
        self::$_reusableData = [];
    }
Qiang Xue committed
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98
    /**
     * Generates a unique hash that can be used for retrieving reusable dependency data.
     * @return string a unique hash value for this cache dependency.
     * @see reusable
     */
    protected function generateReusableHash()
    {
        $data = $this->data;
        $this->data = null;  // https://github.com/yiisoft/yii2/issues/3052
        $key = sha1(serialize($this));
        $this->data = $data;
        return $key;
    }

99 100 101
    /**
     * Generates the data needed to determine if dependency has been changed.
     * Derived classes should override this method to generate the actual dependency data.
102
     * @param Cache $cache the cache component that is currently evaluating this dependency
103 104 105
     * @return mixed the data needed to determine if dependency has been changed.
     */
    abstract protected function generateDependencyData($cache);
Zander Baldwin committed
106
}