Completed
Push — master ( 02200f...aeec26 )
by Alexander
01:24
created

Dependency::generateReusableHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
ccs 6
cts 6
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Yiisoft\Cache\Dependencies;
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 [[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
    public $data;
21
    /**
22
     * @var bool whether this dependency is reusable or not. True value means that dependent
23
     * data for this cache dependency will be generated only once per request. This allows you
24
     * to use the same cache dependency for multiple separate cache calls while generating the same
25
     * page without an overhead of re-evaluating dependency data each time. Defaults to false.
26
     */
27
    public $reusable = false;
28
29
    /**
30
     * @var array static storage of cached data for reusable dependencies.
31
     */
32
    private static $reusableData = [];
33
34
    /**
35
     * Evaluates the dependency by generating and saving the data related with dependency.
36
     * This method is invoked by cache before writing data into it.
37
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
38
     */
39 5
    public function evaluateDependency(CacheInterface $cache): void
40
    {
41 5
        if ($this->reusable) {
42
            $hash = $this->generateReusableHash();
43
            if (!\array_key_exists($hash, self::$reusableData)) {
44
                self::$reusableData[$hash] = $this->generateDependencyData($cache);
45
            }
46
            $this->data = self::$reusableData[$hash];
47
        } else {
48 5
            $this->data = $this->generateDependencyData($cache);
49
        }
50
    }
51
52
    /**
53
     * Checks whether the dependency is changed.
54
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
55
     * @return bool whether the dependency has changed.
56
     */
57 1
    public function isChanged(CacheInterface $cache): bool
58
    {
59 1
        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 1
            $data = $this->generateDependencyData($cache);
67
        }
68
69 1
        return $data !== $this->data;
70
    }
71
72
    /**
73
     * Resets all cached data for reusable dependencies.
74
     */
75 1
    public static function resetReusableData(): void
76
    {
77 1
        self::$reusableData = [];
78
    }
79
80
    /**
81
     * Generates a unique hash that can be used for retrieving reusable dependency data.
82
     * @return string a unique hash value for this cache dependency.
83
     * @see reusable
84
     */
85 1
    protected function generateReusableHash(): string
86
    {
87 1
        $data = $this->data;
88 1
        $this->data = null;  // https://github.com/yiisoft/yii2/issues/3052
89 1
        $key = sha1(serialize($this));
90 1
        $this->data = $data;
91 1
        return $key;
92
    }
93
94
    /**
95
     * Generates the data needed to determine if dependency is changed.
96
     * Derived classes should override this method to generate the actual dependency data.
97
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
98
     * @return mixed the data needed to determine if dependency has been changed.
99
     */
100
    abstract protected function generateDependencyData(CacheInterface $cache);
101
}
102