Test Failed
Push — master ( d844b5...1ec8b0 )
by Joao
34s
created

RedisCacheEngine::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace ByJG\Cache\Psr16;
4
5
use Psr\Log\NullLogger;
6
7
class RedisCacheEngine extends BaseCacheEngine
8
{
9
10
    /**
11
     *
12
     * @var \Redis
13
     */
14
    protected $redis = null;
15
16
    protected $logger = null;
17
18
    protected $server = null;
19
20
    protected $password = null;
21
22 View Code Duplication
    public function __construct($server = null, $password = null, $logger = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
23
    {
24
        $this->server = $server;
25
        if (is_null($server)) {
26
            $this->server = '127.0.0.1:6379';
27
        }
28
29
        $this->password = $password;
30
31
        $this->logger = $logger;
32
        if (is_null($logger)) {
33
            $this->logger = new NullLogger();
34
        }
35
    }
36
37
    protected function lazyLoadRedisServer()
38
    {
39
        if (is_null($this->redis)) {
40
            $this->redis = new \Redis();
41
            $data = explode(":", $this->server);
42
            $this->redis->connect($data[0], isset($data[1]) ? $data[1] : 6379);
43
44
            if (!empty($this->password)) {
45
                $this->redis->auth($this->password);
46
            }
47
            $this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE);
48
49
            $this->redis->info('redis_version');
50
        }
51
    }
52
53
    protected function fixKey($key) {
54
        return "cache:$key";
55
    }
56
57
    /**
58
     * @param string $key The object KEY
59
     * @param int $default IGNORED IN MEMCACHED.
60
     * @return mixed Description
61
     */
62 View Code Duplication
    public function get($key, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
63
    {
64
        $this->lazyLoadRedisServer();
65
66
        $value = $this->redis->get($this->fixKey($key));
67
        $this->logger->info("[Redis Cache] Get '$key' result ");
68
69
        return ($value === false ? $default : unserialize($value));
70
    }
71
72
    /**
73
     * @param string $key The object Key
74
     * @param object $value The object to be cached
75
     * @param int $ttl The time to live in seconds of this objects
76
     * @return bool If the object is successfully posted
77
     */
78 View Code Duplication
    public function set($key, $value, $ttl = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
79
    {
80
        $this->lazyLoadRedisServer();
81
82
        $this->redis->set($this->fixKey($key), serialize($value), $ttl);
83
        $this->logger->info("[Redis Cache] Set '$key' result ");
84
85
        return true;
86
    }
87
88
    public function delete($key)
89
    {
90
        $this->lazyLoadRedisServer();
91
92
        $this->redis->delete($this->fixKey($key));
93
94
        return true;
95
    }
96
97
    public function clear()
98
    {
99
        $keys = $this->redis->keys('cache:*');
100
        foreach ((array)$keys as $key) {
101
            if (preg_match('/^cache\:(?<key>.*)/', $key, $matches)) {
102
                $this->delete($matches['key']);
103
            }
104
        }
105
    }
106
107
    public function has($key)
108
    {
109
        return $this->redis->exists($this->fixKey($key));
110
    }
111
112
    public function isAvailable()
113
    {
114
        if (!class_exists('\Redis')) {
115
            return false;
116
        }
117
118
        try {
119
            $this->lazyLoadRedisServer();
120
            return true;
121
        } catch (\Exception $ex) {
122
            return false;
123
        }
124
    }
125
}
126