Completed
Push — master ( 28797f...652832 )
by Alexander
01:44
created

Dependency::markAsReusable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Yiisoft\Cache\Dependency;
3
4
use Yiisoft\Cache\CacheInterface;
5
6
/**
7
 * Dependency is the base class for cache dependency classes.
8
 *
9
 * Child classes should override its {@see generateDependencyData()} for generating
10
 * the actual dependency data.
11
 *
12
 * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
13
 */
14
abstract class Dependency
15
{
16
    /**
17
     * @var mixed the dependency data that is saved in cache and later is compared with the
18
     * latest dependency data
19
     */
20
    protected $data;
21
22
    /**
23
     * @var bool whether this dependency is reusable or not. True value means that dependent
24
     * data for this cache dependency will be generated only once per request. This allows you
25
     * to use the same cache dependency for multiple separate cache calls while generating the same
26
     * page without an overhead of re-evaluating dependency data each time. Defaults to false.
27
     */
28
    protected $reusable = false;
29
30
    /**
31
     * @var array static storage of cached data for reusable dependencies.
32
     */
33
    private static $reusableData = [];
34
35
    /**
36
     * Changes dependecy behavior so dependent data for this cache dependency will be generated only once per request.
37
     *  This allows you to use the same cache dependency for multiple separate cache calls while generating the same
38
      * page without an overhead of re-evaluating dependency data each time.
39
     */
40
    public function markAsReusable(): void
41
    {
42
        $this->reusable = true;
43
    }
44
45
    /**
46
     * Evaluates the dependency by generating and saving the data related with dependency.
47
     * This method is invoked by cache before writing data into it.
48
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
49
     */
50
    public function evaluateDependency(CacheInterface $cache): void
51
    {
52
        if ($this->reusable) {
53
            $hash = $this->generateReusableHash();
54
            if (!\array_key_exists($hash, self::$reusableData)) {
55
                self::$reusableData[$hash] = $this->generateDependencyData($cache);
56
            }
57
            $this->data = self::$reusableData[$hash];
58
        } else {
59
            $this->data = $this->generateDependencyData($cache);
60
        }
61
    }
62
63
    /**
64
     * Checks whether the dependency is changed.
65
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
66
     * @return bool whether the dependency has changed.
67
     */
68
    public function isChanged(CacheInterface $cache): bool
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
80
        return $data !== $this->data;
81
    }
82
83
    /**
84
     * Resets all cached data for reusable dependencies.
85
     */
86
    public static function resetReusableData(): void
87
    {
88
        self::$reusableData = [];
89
    }
90
91
    /**
92
     * Generates a unique hash that can be used for retrieving reusable dependency data.
93
     * @return string a unique hash value for this cache dependency.
94
     * @see reusable
95
     */
96
    protected function generateReusableHash(): string
97
    {
98
        $data = $this->data;
99
        $this->data = null;  // https://github.com/yiisoft/yii2/issues/3052
100
        $key = sha1(serialize($this));
101
        $this->data = $data;
102
        return $key;
103
    }
104
105
    /**
106
     * Generates the data needed to determine if dependency is changed.
107
     * Derived classes should override this method to generate the actual dependency data.
108
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
109
     * @return mixed the data needed to determine if dependency has been changed.
110
     */
111
    abstract protected function generateDependencyData(CacheInterface $cache);
112
}
113