-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicRedis.php
60 lines (49 loc) · 1.49 KB
/
BasicRedis.php
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
<?php
use Predis\Client as RedisClient;
use Dotenv\Dotenv as Config;
Config::createMutable(__DIR__, '.env')->load();
class BasicRedis {
/**
* @var array
*/
private $connections;
/**
* @var RedisClient
*/
private $activeConnection;
function __construct() {
}
public function addConnection($name, $host, $port = 6379, $setActive = true, $scheme = 'tcp'){
$this->connections[$name] = new RedisClient([
'scheme' => $scheme,
'host' => $host,
'port' => $port,
], ['parameters' => ['database' => 10]]);
if($setActive){
$this->setActiveConnection($this->connections[$name]);
}
}
/**
* @param $key
* @return mixed
*/
public function getKey($key){
return $this->getActiveConnection()->get($key);
}
public function incrementValue($key, $incrementBy = 1){
return $this->getActiveConnection()->incrby($key, $incrementBy);
}
public function setKey($key, $value, $timeout){
$this->getActiveConnection()->setex($key, $timeout, $value);
$this->getActiveConnection()->expire($key, $timeout);
}
public function getActiveConnection(): RedisClient{
return $this->activeConnection;
}
public function getConnectionByName($name): ?RedisClient{
return $this->connections[$name] ?? null;
}
public function setActiveConnection($connection){
$this->activeConnection = $connection;
}
}