Completed
Branch master (6f5719)
by Ryuichi
03:47 queued 02:31
created

Redis::isAvailableCacheLibrary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 5
cp 0.6
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2.2559
1
<?php
2
namespace WebStream\Cache\Driver;
3
4
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...
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
7
/**
8
 * Redis
9
 * @author Ryuichi Tanaka
10
 * @since 2015/07/09
11
 * @version 0.7
12
 */
13
class Redis implements ICache
14
{
15
    use injector;
16
17
    /**
18
     * @var Container キャッシュ依存コンテナ
19
     */
20
    private $cacheContainer;
21
22
    /**
23
     * @var string キャッシュ接頭辞
24
     */
25
    private $cachePrefix;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function __construct(Container $cacheContainer)
31
    {
32
        $this->cacheContainer = $cacheContainer;
33
        $this->cachePrefix = $this->cacheContainer->cachePrefix . '.' . $this->cacheContainer->classPrefix . '.';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 4
    public function add($key, $value, $ttl = 0, $overwrite = false): bool
40
    {
41 4
        if (!$this->isAvailableCacheLibrary()) {
42
            return false;
43
        }
44
45 4
        if (is_array($value)) {
46
            $value = json_encode($value);
47
        }
48
49 4
        $result = false;
50 4
        if ($ttl > 0) {
51
            if ($overwrite) {
52
                $result = $this->cacheContainer->driver->setEx($key, $ttl, $value);
53
            } else {
54
                $result = $this->cacheContainer->driver->set($key, $value, ['nx', 'ex' => $ttl]);
55
            }
56
        } else {
57 4
            if ($overwrite) {
58 3
                $result = $this->cacheContainer->driver->set($key, $value);
59
            } else {
60 1
                $result = $this->cacheContainer->driver->setNx($key, $value);
61
            }
62
        }
63
64 4
        $this->logger->info("Execute cache save: " . $key);
65
66 4
        return $result;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 4
    public function get($key)
73
    {
74 4
        if (!$this->isAvailableCacheLibrary()) {
75
            return null;
76
        }
77
78 4
        $result = $this->cacheContainer->driver->get($key);
79 4
        $this->logger->info("Execute cache read: " . $key);
80
81 4
        if ($result !== false) {
82 2
            $this->logger->info("Execute cache read: " . $key);
83
        } else {
84 2
            $this->logger->warn("Failed to read cache: " . $key);
85 2
            $result = null;
86
        }
87
88 4
        return $result;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 1
    public function delete($key): bool
95
    {
96 1
        if (!$this->isAvailableCacheLibrary()) {
97
            return false;
98
        }
99
100 1
        if ($this->cacheContainer->driver->delete($key) > 0) {
101 1
            $this->logger->info("Execute cache cleared: " . $key);
102 1
            return true;
103
        } else {
104
            $this->logger->warn("Failed to clear cache: " . $key);
105
            return false;
106
        }
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 1
    public function clear(): bool
113
    {
114 1
        if (!$this->isAvailableCacheLibrary()) {
115
            return false;
116
        }
117
118 1
        $it = null;
119 1
        $result = 1;
120 1
        $this->cacheContainer->driver->setOption($this->cacheContainer->redisOptPrefix, "");
121 1
        while ($keys = $this->cacheContainer->driver->scan($it, "*")) {
122 1
            $result *= $this->cacheContainer->driver->delete($keys);
123
        }
124 1
        $this->cacheContainer->driver->setOption($this->cacheContainer->redisOptPrefix, $this->cachePrefix);
125
126 1
        if ($result > 0) {
127 1
            $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*");
128 1
            return true;
129
        } else {
130
            $this->logger->warn("Failed to clear all cache: " . $this->cachePrefix . "*");
131
            return false;
132
        }
133
    }
134
135
    /**
136
     * キャッシュライブラリが使用可能か検査する
137
     * @return bool 使用可能でtrue
138
     */
139 4
    private function isAvailableCacheLibrary(): bool
140
    {
141 4
        if ($this->cacheContainer->available) {
142 4
            return true;
143
        }
144
145
        $this->logger->warn("Redis cache library is unavailable.");
146
147
        return false;
148
    }
149
}
150