Redis.php
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace Weasy\External\Utils;
class Redis
{
private static $instance;
private $redis;
private $config = [
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'timeout' => 0,
];
static public function getInstance($config = [])
{
if (!self::$instance instanceof self) {
self::$instance = new self($config);
}
return self::$instance;
}
public function __construct($config = [])
{
if (!empty($config)) {
$this->config = $config;
}
$this->redis = new \Redis();
$this->redis->connect($this->config['host'], $this->config['port'], $this->config['timeout']);
$this->redis->auth($this->config['password']);
}
/**
* Redis server
*
* @return \Redis
*/
public function server()
{
return $this->redis;
}
/**
* 在名称为key的list左边(头)添加值为value的 元素
*
* @param $key
* @param mixed ...$value
*
* @return bool|int
*/
public function lPush($key, ...$value)
{
return $this->redis->lPush($key, $value);
}
/**
* 在名称为key的list右边(尾)添加值为value的 元素
*
* @param $key
* @param mixed ...$value
*
* @return bool|int
*/
public function rPush($key, ...$value)
{
return $this->redis->rPush($key, $value);
}
/**
* 输出名称为key的list左(头)起的第一个元素,删除该元素
*
* @param $key
*
* @return bool|mixed
*/
public function lPop($key)
{
return $this->redis->lPop($key);
}
/**
* 输出名称为key的list右(尾)起起的第一个元素,删除该元素
*
* @param $key
*
* @return bool|mixed
*/
public function rPop($key)
{
return $this->redis->rPop($key);
}
/**
* 返回名称为key的list中start至end之间的元素(end为 -1 ,返回所有)
*
* @param $key
* @param $start
* @param $end
*
* @return array
*/
public function lrange($key, $start, $end)
{
return $this->redis->lrange($key, $start, $end);
}
/**
* @param $key
* @param $value
* @param int $timeout
*
* @return bool
*/
public function set($key, $value, $timeout = 0)
{
return $this->redis->set($key, $value, $timeout);
}
/**
* @param $key
*
* @return bool|mixed|string
*/
public function get($key)
{
return $this->redis->get($key);
}
/**
* @param $key
* @param mixed ...$otherKeys
*
* @return int
*/
public function del($key, ...$otherKeys)
{
return $this->redis->del($key, $otherKeys);
}
/**
* @param $key
*
* @return bool|int
*/
public function exists($key)
{
return $this->redis->exists($key);
}
}