Passed
Push — master ( 074e22...c7a4af )
by Oleg
03:48
created

RedisDriver::getMeta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php /** MicroRedisDriver */
2
3
namespace Micro\Cache\Driver;
4
5
use Micro\Base\Exception;
6
use Micro\Base\IContainer;
7
8
/**
9
 * Class RedisDriver
10
 *
11
 * @author Oleg Lunegov <[email protected]>
12
 * @link https://github.com/linpax/microphp-framework
13
 * @copyright Copyright &copy; 2013 Oleg Lunegov
14
 * @license /LICENSE
15
 * @package Micro
16
 * @subpackage Cache\Driver
17
 * @version 1.0
18
 * @since 1.0
19
 */
20
class RedisDriver extends BaseCacheDriver
21
{
22
    /** @var \Redis $driver driver redis */
23
    protected $driver;
24
25
26
    /**
27
     * Constructor
28
     *
29
     * @access public
30
     *
31
     * @param IContainer $container
32
     * @param array $config config array
33
     *
34
     * @result void
35
     * @throws Exception
36
     */
37
    public function __construct(IContainer $container, array $config = [])
38
    {
39
        parent::__construct($container, $config);
40
41
        if (!$this->check()) {
42
            throw new Exception('Redis not installed on system');
43
        }
44
        $this->driver = new \Redis;
45
46
        try {
47
            if (!empty($config['socket_type']) && $config['socket_type'] === 'unix') {
48
                $result = $this->driver->connect($config['socket']);
49
            } else {
50
                $result = $this->driver->connect($config['host'], $config['port'], $config['duration']);
51
            }
52
        } 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...
53
            throw new Exception((string)$e);
54
        }
55
56
        if (!$result) {
57
            throw new Exception('Redis configuration failed');
58
        }
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function check()
65
    {
66
        return extension_loaded('redis');
67
    }
68
69
    /**
70
     * Destructor
71
     *
72
     * @access public
73
     * @result void
74
     */
75
    public function __destruct()
76
    {
77
        if ($this->driver) {
78
            $this->driver->close();
79
        }
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85
    public function set($name, $value, $duration = 0)
86
    {
87
        return $duration ? $this->driver->setex($name, $duration, $value) : $this->driver->set($name, $value);
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function delete($name)
94
    {
95
        return ($this->driver->delete($name) !== 1) ?: true;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function clean()
102
    {
103
        return $this->driver->flushDB();
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109
    public function info()
110
    {
111
        return $this->driver->info();
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public function getMeta($id)
118
    {
119
        if ($value = $this->get($id)) {
120
            return ['expire' => time() + $this->driver->ttl($id), 'data' => $value];
121
        }
122
123
        return false;
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129
    public function get($name)
130
    {
131
        return $this->driver->get($name);
132
    }
133
134
    /**
135
     * @inheritdoc
136
     */
137
    public function increment($name, $offset = 1)
138
    {
139
        return $this->driver->incrBy($name, $offset);
140
    }
141
142
    /**
143
     * @inheritdoc
144
     */
145
    public function decrement($name, $offset = 1)
146
    {
147
        return $this->driver->decrBy($name, $offset);
148
    }
149
} 
150