Completed
Push — master ( a1970e...6c5d6a )
by Oleg
04:15
created

RedisDriver::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php /** MicroRedisDriver */
2
3
namespace Micro\Cache\Drivers;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * Class RedisDriver
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Cache\Driver
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class RedisDriver extends CacheDriver
20
{
21
    /** @var \Redis $driver driver redis */
22
    protected $driver;
23
24
25
    /**
26
     * Constructor
27
     *
28
     * @access public
29
     *
30
     * @param array $config config array
31
     *
32
     * @result void
33
     * @throws Exception
34
     */
35
    public function __construct(array $config = [])
36
    {
37
        parent::__construct($config);
38
39
        if (!$this->check()) {
40
            throw new Exception('Redis not installed on system');
41
        }
42
        $this->driver = new \Redis;
43
44
        try {
45
            if (!empty($config['socket_type']) && $config['socket_type'] === 'unix') {
46
                $result = $this->driver->connect($config['socket']);
47
            } else {
48
                $result = $this->driver->connect($config['host'], $config['port'], $config['duration']);
49
            }
50
        } catch (\RedisException $e) {
0 ignored issues
show
Bug introduced by
The class RedisException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
51
            throw new Exception((string)$e);
52
        }
53
54
        if (!$result) {
55
            throw new Exception('Redis configuration failed');
56
        }
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function check()
63
    {
64
        return extension_loaded('redis') && $this->driver->ping();
65
    }
66
67
    /**
68
     * Destructor
69
     *
70
     * @access public
71
     * @result void
72
     */
73
    public function __destruct()
74
    {
75
        if ($this->driver) {
76
            $this->driver->close();
77
        }
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function set($name, $value, $duration = 0)
84
    {
85
        return $duration ? $this->driver->setex($name, $duration, $value) : $this->driver->set($name, $value);
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function delete($name)
92
    {
93
        return ($this->driver->delete($name) !== 1) ?: true;
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function clean()
100
    {
101
        return $this->driver->flushDB();
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function info()
108
    {
109
        return $this->driver->info();
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function getMeta($id)
116
    {
117
        if ($value = $this->get($id)) {
118
            return ['expire' => time() + $this->driver->ttl($id), 'data' => $value];
119
        }
120
121
        return false;
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function get($name)
128
    {
129
        return $this->driver->get($name);
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function increment($name, $offset = 1)
136
    {
137
        return $this->driver->incrBy($name, $offset);
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function decrement($name, $offset = 1)
144
    {
145
        return $this->driver->decrBy($name, $offset);
146
    }
147
} 
148