Apcu::clear()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 19
ccs 0
cts 13
cp 0
rs 9.5555
cc 5
nc 5
nop 0
crap 30
1
<?php
2
3
namespace WebStream\Cache\Driver;
4
5
use WebStream\Container\Container;
0 ignored issues
show
Bug introduced by
The type WebStream\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use WebStream\DI\Injector;
0 ignored issues
show
Bug introduced by
The type WebStream\DI\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
/**
9
 * Apcu
10
 * @author Ryuichi Tanaka
11
 * @since 2015/07/05
12
 * @version 0.7
13
 */
14
class Apcu implements ICache
15
{
16
    use injector;
17
18
    /**
19
     * @var Container キャッシュ依存コンテナ
20
     */
21
    private Container $cacheContainer;
22
23
    /**
24
     * @var string キャッシュ接頭辞
25
     */
26
    private string $cachePrefix;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function __construct(Container $cacheContainer)
32
    {
33
        $this->cacheContainer = $cacheContainer;
34
        $this->cachePrefix = $this->cacheContainer->cachePrefix . '.';
35
        if (!empty($this->cacheContainer->classPrefix)) {
36
            $this->cachePrefix .= $this->cacheContainer->classPrefix . '.';
37
        }
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function add($key, $value, $ttl = 0, $overwrite = false): bool
44
    {
45
        if (!$this->isAvailableCacheLibrary()) {
46
            return false;
47
        }
48
        $key = $this->cachePrefix . $key;
49
50
        $result = $overwrite ? $this->cacheContainer->driver->delegate("apcu_store", [$key, $value, $ttl]) :
51
            $this->cacheContainer->driver->delegate("apcu_add", [$key, $value, $ttl]);
52
        $this->logger->info("Execute cache save: " . $key);
53
54
        return $result;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function get($key)
61
    {
62
        if (!$this->isAvailableCacheLibrary()) {
63
            return null;
64
        }
65
        $key = $this->cachePrefix . $key;
66
        $value = $this->cacheContainer->driver->delegate("apcu_fetch", [$key]);
67
68
        if ($value !== false) {
69
            $this->logger->info("Execute cache read: " . $key);
70
        } else {
71
            $this->logger->warn("Failed to read cache: " . $key);
72
            $value = null;
73
        }
74
75
        return $value;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function delete($key): bool
82
    {
83
        if (!$this->isAvailableCacheLibrary()) {
84
            return false;
85
        }
86
        $key = $this->cachePrefix . $key;
87
88
        if ($this->cacheContainer->driver->delegate("apcu_delete", [$key])) {
89
            $this->logger->info("Execute cache cleared: " . $key);
90
            return true;
91
        } else {
92
            $this->logger->warn("Failed to clear cache: " . $key);
93
            return false;
94
        }
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function clear(): bool
101
    {
102
        if (!$this->isAvailableCacheLibrary()) {
103
            return false;
104
        }
105
106
        if (class_exists('\APCUIterator')) {
107
            $obj = new \APCUIterator('/^' . $this->cachePrefix . '/', APC_ITER_KEY);
108
            if ($this->cacheContainer->driver->delegate("apcu_delete", [$obj])) {
109
                $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*");
110
                return true;
111
            }
112
        } elseif ($this->cacheContainer->driver->delegate("apcu_clear_cache")) {
113
            $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*");
114
            return true;
115
        }
116
117
        $this->logger->warn("Failed to clear all cache: " . $this->cachePrefix . "*");
118
        return false;
119
    }
120
121
    /**
122
     * キャッシュライブラリが使用可能か検査する
123
     * @return bool 使用可能でtrue
124
     */
125
    private function isAvailableCacheLibrary(): bool
126
    {
127
        if ($this->cacheContainer->available) {
128
            return true;
129
        }
130
131
        $this->logger->warn("APCu cache library is unavailable.");
132
133
        return false;
134
    }
135
}
136