RedisDriver::unpackData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Wechat\CacheDriver;
4
5
use Wechat\CacheDriver\BaseDriver;
6
7
/**
8
 * 文件缓存驱动.
9
 *
10
 */
11
class RedisDriver extends BaseDriver
12
{
13
    private $redis;
14
    private $pre;
15
16
    public function __construct()
17
    {
18
        $pre = 'WeixinApi:';
19
        parent::__construct($pre);
20
21
        if (!class_exists('redis')) {
22
            exit('Redis初始化连接失败');
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
23
        }
24
25
        ini_set('default_socket_timeout', 50);//socket连接超时时间;
26
        $this->redis = new \redis();
27
        // 如果用框架 此处最好走配置文件
28
        $host      = '127.0.0.1';
29
        $port      = '6379';
30
        $this->pre = $pre;
31
        $link      = $this->redis->connect($host, $port, 40);
32
33
        if (!$link) {
34
            exit('Redis初始化连接失败');
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
35
        }
36
    }
37
38
    /**
39
     * 根据缓存名获取缓存内容.
40
     *
41
     * @param string $name
42
     *
43
     * @return bool|mixed|string
44
     */
45
    public function _get($name)
46
    {
47
        $name = $this->createFileName($name);
48
        $data = $this->redis->get($name);
49
        if ($data) {
50
            $data = $this->unpackData($data);
51
        }
52
53
        return $data;
54
    }
55
56
    /**
57
     * 根据缓存名 设置缓存值和超时时间.
58
     *
59
     * @param string $name    缓存名
60
     * @param void   $value   缓存值
61
     * @param int    $expires 超时时间
62
     *
63
     * @return boolean;
0 ignored issues
show
Documentation introduced by
The doc-type boolean; could not be parsed: Expected "|" or "end of type", but got ";" at position 7. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
64
     */
65
    public function _set($name, $value, $expires)
66
    {
67
        $name = $this->createFileName($name);
68
        $data = $this->packData($value);
69
        if ($expires === 0) {
70
            return $this->redis->set($name, $data);
71
        }
72
73
        return $this->redis->setex($name, $expires, $data);
74
    }
75
76
    /**
77
     * 数据打包.
78
     *
79
     * @param void $data    缓存值
80
     * @param int  $expires 超时时间
0 ignored issues
show
Bug introduced by
There is no parameter named $expires. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
81
     *
82
     * @return string
83
     */
84
    private function packData($data)
85
    {
86
        return serialize($data);
87
    }
88
89
    /**
90
     * 数据解包.
91
     *
92
     * @param $data
93
     *
94
     * @return mixed
95
     */
96
    private function unpackData($data)
97
    {
98
        return unserialize($data);
99
    }
100
101
    /**
102
     * 创建缓存文件名.
103
     *
104
     * @param string $name 缓存名
105
     *
106
     * @return string
107
     */
108
    private function createFileName($name)
109
    {
110
        return $this->pre . md5($name);
111
    }
112
}
113