Completed
Push — master ( eff571...d844b5 )
by Joao
29s
created

RedisCacheEngine::release()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace ByJG\Cache\Engine;
4
5
use ByJG\Cache\CacheEngineInterface;
6
use Psr\Log\NullLogger;
7
8
class RedisCacheEngine implements CacheEngineInterface
9
{
10
11
    /**
12
     *
13
     * @var \Redis
14
     */
15
    protected $redis = null;
16
17
    protected $logger = null;
18
19
    protected $server = null;
20
21
    protected $password = null;
22
23 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...
24
    {
25
        $this->server = $server;
26
        if (is_null($server)) {
27
            $this->server = '127.0.0.1:6379';
28
        }
29
30
        $this->password = $password;
31
32
        $this->logger = $logger;
33
        if (is_null($logger)) {
34
            $this->logger = new NullLogger();
35
        }
36
    }
37
38
    protected function lazyLoadRedisServer()
39
    {
40
        if (is_null($this->redis)) {
41
            $this->redis = new \Redis();
42
            $data = explode(":", $this->server);
43
            $this->redis->connect($data[0], isset($data[1]) ? $data[1] : 6379);
44
45
            if (!empty($this->password)) {
46
                $this->redis->auth($this->password);
47
            }
48
            $this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE);
49
50
            $this->redis->info('redis_version');
51
        }
52
    }
53
54
    /**
55
     * @param string $key The object KEY
56
     * @param int $ttl IGNORED IN MEMCACHED.
57
     * @return object Description
58
     */
59
    public function get($key, $ttl = 0)
60
    {
61
        $this->lazyLoadRedisServer();
62
63
        $value = $this->redis->get($key);
64
        $this->logger->info("[Redis Cache] Get '$key' result ");
65
66
        return ($value === false ? null : $value);
67
    }
68
69
    /**
70
     * @param string $key The object Key
71
     * @param object $object The object to be cached
72
     * @param int $ttl The time to live in seconds of this objects
73
     * @return bool If the object is successfully posted
74
     */
75
    public function set($key, $object, $ttl = 0)
76
    {
77
        $this->lazyLoadRedisServer();
78
79
        $this->redis->set($key, $object, $ttl);
80
        $this->logger->info("[Redis Cache] Set '$key' result ");
81
82
        return true;
83
    }
84
85
    /**
86
     * Unlock resource
87
     * @param string $key
88
     */
89
    public function release($key)
90
    {
91
        $this->lazyLoadRedisServer();
92
93
        $this->redis->delete($key);
94
    }
95
96
    /**
97
     *
98
     * @param string $key
99
     * @param string $str
100
     * @return bool
101
     */
102
    public function append($key, $str)
103
    {
104
        $this->lazyLoadRedisServer();
105
106
        $this->logger->info("[Redis Cache] Append '$key' in Memcached");
107
        return $this->redis->append($key, $str);
108
    }
109
110
    /**
111
     * Lock resource before set it.
112
     * @param string $key
113
     */
114
    public function lock($key)
115
    {
116
        $this->lazyLoadRedisServer();
117
118
        return;
119
    }
120
121
    /**
122
     * UnLock resource after set it
123
     * @param string $key
124
     */
125
    public function unlock($key)
126
    {
127
        $this->lazyLoadRedisServer();
128
129
        return;
130
    }
131
132
    public function isAvailable()
133
    {
134
        if (!class_exists('\Redis')) {
135
            return false;
136
        }
137
138
        try {
139
            $this->lazyLoadRedisServer();
140
            return true;
141
        } catch (\Exception $ex) {
142
            return false;
143
        }
144
    }
145
}
146