Completed
Push — master ( 48ba4b...58336c )
by Lars
02:26 queued 12s
created

AdapterOpCache::setExpired()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0879

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 14
cts 17
cp 0.8235
rs 9.296
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 4.0879
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * AdapterOpCache: PHP-OPcache
9
 *
10
 * OPcache improves PHP performance by storing precompiled script bytecode
11
 * in shared memory, thereby removing the need for PHP to load and
12
 * parse scripts on each request.
13
 */
14
class AdapterOpCache extends AdapterFileSimple
15
{
16
    /**
17
     * @var bool
18
     */
19
    private static $hasCompileFileFunction;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 11
    public function __construct($cacheDir = null)
25
    {
26 11
        parent::__construct($cacheDir);
27
28 11
        $this->serializer = new SerializerNo();
29
30 11
        if (self::$hasCompileFileFunction === null) {
31
            /** @noinspection PhpComposerExtensionStubsInspection */
32 1
            self::$hasCompileFileFunction = \function_exists('opcache_compile_file') && !empty(\opcache_get_status());
33
        }
34 11
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 6
    public function get(string $key)
40
    {
41 6
        $path = $this->getFileName($key);
42
43 View Code Duplication
        if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44 6
        \file_exists($path) === false
45
        ||
46 6
        \filesize($path) === 0
47
    ) {
48 1
            return null;
49
        }
50
51
        /** @noinspection PhpIncludeInspection */
52 6
        $data = include $path;
53
54 6
        if (!$data || !$this->validateDataFromCache($data)) {
55
            return null;
56
        }
57
58 6
        if ($this->ttlHasExpired($data['ttl']) === true) {
59 2
            $this->remove($key);
60
61 2
            return null;
62
        }
63
64 5
        return $data['value'];
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 8
    protected function getFileName(string $key): string
71
    {
72 8
        return $this->cacheDir . \DIRECTORY_SEPARATOR . self::CACHE_FILE_PREFIX . $key . '.php';
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 6
    public function setExpired(string $key, $value, int $ttl = 0): bool
79
    {
80
        $item = [
81 6
            'value' => $value,
82 6
            'ttl'   => $ttl ? $ttl + \time() : 0,
83
        ];
84 6
        $content = \var_export($item, true);
85
86
        $content = '<?php 
87 6
    return ' . $content . ';
88
    ';
89
90 6
        $cacheFile = $this->getFileName($key);
91
92 6
        $result = (bool) \file_put_contents(
93 6
        $cacheFile,
94 6
        $content,
95 6
        0,
96 6
        $this->getContext()
97
    );
98
99
        if (
100 6
        $result === true
101
        &&
102 6
        self::$hasCompileFileFunction === true
103
    ) {
104
            // opcache will only compile and cache files older than the script execution start.
105
            // set a date before the script execution date, then opcache will compile and cache the generated file.
106
            /** @noinspection SummerTimeUnsafeTimeManipulationInspection */
107
            \touch($cacheFile, \time() - 86400);
108
109
            /** @noinspection PhpComposerExtensionStubsInspection */
110
            \opcache_invalidate($cacheFile);
111
            /** @noinspection PhpComposerExtensionStubsInspection */
112
            \opcache_compile_file($cacheFile);
113
        }
114
115 6
        return $result;
116
    }
117
}
118