Completed
Push — master ( 94f5e8...a88b9e )
by Lars
01:57 queued 11s
created

AdapterOpCache::setExpired()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.128

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 12
cts 15
cp 0.8
rs 9.36
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 4.128
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 12
    public function __construct($cacheDir = null)
25
    {
26 12
        parent::__construct($cacheDir);
27
28 12
        $this->serializer = new SerializerNo();
29
30 12
        if (self::$hasCompileFileFunction === null) {
31
            /** @noinspection PhpComposerExtensionStubsInspection */
32
            /** @noinspection PhpUsageOfSilenceOperatorInspection */
33
            self::$hasCompileFileFunction = (
34 1
                \function_exists('opcache_compile_file')
35
                &&
36 1
                !empty(@\opcache_get_status())
37
            );
38
        }
39 12
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 18
    public function get(string $key)
45
    {
46 18
        $path = $this->getFileName($key);
47
48 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...
49 18
            \file_exists($path) === false
50
            ||
51 18
            \filesize($path) === 0
52
        ) {
53 10
            return null;
54
        }
55
56
        /** @noinspection PhpIncludeInspection */
57 9
        $data = include $path;
58
59 9
        if (!$data || !$this->validateDataFromCache($data)) {
60
            return null;
61
        }
62
63 9
        if ($this->ttlHasExpired($data['ttl']) === true) {
64 2
            $this->remove($key);
65
66 2
            return null;
67
        }
68
69 8
        return $data['value'];
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 32
    protected function getFileName(string $key): string
76
    {
77 32
        return $this->cacheDir . \DIRECTORY_SEPARATOR . self::CACHE_FILE_PREFIX . $key . '.php';
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 15
    public function setExpired(string $key, $value, int $ttl = 0): bool
84
    {
85
        $item = [
86 15
            'value' => $value,
87 15
            'ttl'   => $ttl ? $ttl + \time() : 0,
88
        ];
89 15
        $content = \var_export($item, true);
90
91 15
        $content = '<?php return ' . $content . ';';
92
93 15
        $cacheFile = $this->getFileName($key);
94
95 15
        $result = $this->writeFile(
96 15
            $cacheFile,
97 15
            $content
98
        );
99
100
        if (
101 15
            $result === true
102
            &&
103 15
            self::$hasCompileFileFunction === true
104
        ) {
105
            // opcache will only compile and cache files older than the script execution start.
106
            // set a date before the script execution date, then opcache will compile and cache the generated file.
107
            /** @noinspection SummerTimeUnsafeTimeManipulationInspection */
108
            \touch($cacheFile, \time() - 86400);
109
110
            /** @noinspection PhpComposerExtensionStubsInspection */
111
            \opcache_invalidate($cacheFile);
112
            /** @noinspection PhpComposerExtensionStubsInspection */
113
            \opcache_compile_file($cacheFile);
114
        }
115
116 15
        return $result;
117
    }
118
}
119