RedisCache::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 6
Ratio 24 %

Code Coverage

Tests 8
CRAP Score 6.9683

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 25
ccs 8
cts 14
cp 0.5714
rs 8.439
cc 5
eloc 16
nc 6
nop 4
crap 6.9683
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Jenner
5
 * Date: 2015/8/20
6
 * Time: 15:14
7
 */
8
9
namespace Jenner\SimpleFork\Cache;
10
11
12
/**
13
 * redis cache
14
 *
15
 * @package Jenner\SimpleFork\Cache
16
 */
17
class RedisCache implements CacheInterface
18
{
19
20
    /**
21
     * @var \Redis
22
     */
23
    protected $redis;
24
25
    protected $prefix;
26
27
    /**
28
     * @param string $host
29
     * @param int $port
30
     * @param int $database
31
     * @param string $prefix
32
     */
33 3
    public function __construct(
34
        $host = '127.0.0.1',
35
        $port = 6379,
36
        $database = 0,
37
        $prefix = 'simple-fork'
38
    )
39
    {
40 3
        $this->redis = new \Redis();
41 3
        $connection_result = $this->redis->connect($host, $port);
42 3
        if (!$connection_result) {
43
            throw new \RuntimeException('can not connect to the redis server');
44
        }
45
46 3 View Code Duplication
        if ($database != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
47
            $select_result = $this->redis->select($database);
48
            if (!$select_result) {
49
                throw new \RuntimeException('can not select the database');
50
            }
51
        }
52
53 3
        if (empty($prefix)) {
54
            throw new \InvalidArgumentException('prefix can not be empty');
55
        }
56 3
        $this->prefix = $prefix;
57 3
    }
58
59
    /**
60
     * close redis connection
61
     */
62
    public function __destruct()
63
    {
64
        $this->close();
65
    }
66
67
    /**
68
     * close the connection
69
     */
70 3
    public function close()
71
    {
72 3
        $this->redis->close();
73 3
    }
74
75
    /**
76
     * get var
77
     *
78
     * @param $key
79
     * @param null $default
80
     * @return bool|string|null
81
     */
82 3
    public function get($key, $default = null)
83
    {
84 3
        $result = $this->redis->hGet($this->prefix, $key);
85 3
        if ($result !== false) return $result;
86
87 3
        return $default;
88
    }
89
90
    /**
91
     * set var
92
     *
93
     * @param $key
94
     * @param null $value
95
     * @return boolean
96
     */
97 3
    public function set($key, $value)
98
    {
99 3
        return $this->redis->hSet($this->prefix, $key, $value);
100
    }
101
102
    /**
103
     * has var ?
104
     *
105
     * @param $key
106
     * @return bool
107
     */
108 3
    public function has($key)
109
    {
110 3
        return $this->redis->hExists($this->prefix, $key);
111
    }
112
113
    /**
114
     * delete var
115
     *
116
     * @param $key
117
     * @return bool
118
     */
119 3
    public function delete($key)
120
    {
121 3
        if ($this->redis->hDel($this->prefix, $key) > 0) {
122 3
            return true;
123
        }
124
        return false;
125
    }
126
}