-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredisDB.php
101 lines (84 loc) · 2.49 KB
/
redisDB.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
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
<?php
class redisDB extends Redis {
private $hostRedis = '127.0.0.1';
private $port = 6379;
private $ttl = 604800; /* time to live = 1 minggu */
private $db = 1; /* db redis */
/* Koneksi ke redis */
public function openRedis() {
$this->connect($this->hostRedis, $this->port);
try {
$pingRedis = $this->ping();
} catch (Throwable $e) {
$e->getMessage();
}
if (isset($e)) {
return false;
} else {
return true;
}
}
/* tutup koneksi */
public function closeRedis() {
$this->close();
}
/* mencari key redis */
public function FindRedis($keyredis) {
try {
$check = $this->openRedis();
if ($check != false) {
$this->SELECT($this->db);
$resultRedis = $this->keys($keyredis);
$this->closeRedis();
}
return $resultRedis;
} catch (Throwable $e) {
$e->getMessage();
}
}
/* mendapatkan value dari redis key */
public function GetDatafromKeys($keyredis) {
$findRedis = $this->FindRedis($keyredis);
if (count($findRedis) > 0) {
$this->openRedis();
$this->SELECT($this->db);
$arrData = $this->get($keyredis);
$this->closeRedis();
} else {
$arrData = null;
}
return $arrData;
}
/* menyimpan value ke redis */
public function InsertDataToKey($keyredis, $value) {
try {
$check = $this->openRedis();
if ($check != false) {
$this->SELECT($this->db);
$this->setex($keyredis, $this->ttl, $value);
$this->closeRedis();
return true;
} else {
return false;
}
} catch (Throwable $e) {
throw $e;
return false;
}
}
/* menghapus value redis */
public function RemoveRedis($keyredis) {
try {
$check = $this->openRedis();
if ($check != false) {
$this->SELECT($this->db);
$this->del($keyredis);
$this->closeRedis();
$message = "success";
}
} catch (Throwable $e) {
throw $e;
}
return $message;
}
}