Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

RedisCache::clean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

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