-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathredis_functions.php
More file actions
95 lines (85 loc) · 2.94 KB
/
Copy pathredis_functions.php
File metadata and controls
95 lines (85 loc) · 2.94 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
<?php
namespace CluebotNG;
/*
* Copyright (C) 2015 Jacobi Carter and Chris Breneman
*
* This file is part of ClueBot NG.
*
* ClueBot NG is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* ClueBot NG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ClueBot NG. If not, see <http://www.gnu.org/licenses/>.
*/
class KeyValueStore
{
private static $client = null;
private static function connect()
{
global $logger;
try {
if (self::$client === null || !self::$client->ping()) {
$redis = new \Redis();
$redis->pconnect(Config::$cb_redis_host, Config::$cb_redis_port, 1);
$redis->auth(Config::$cb_redis_pass);
$redis->select(Config::$cb_redis_db);
self::$client = $redis;
}
} catch (\RedisException $e) {
$logger->warning('Redis connection failed: ' . $e->getMessage());
self::$client = null;
}
}
private static function executeGet($key)
{
global $logger;
self::connect();
if (self::$client !== null) {
try {
return self::$client->get($key);
} catch (\RedisException $e) {
$logger->warning('Redis get operation failed: ' . $e->getMessage());
}
}
return null;
}
private static function executeSet($key, $value, $ttl)
{
global $logger;
self::connect();
if (self::$client !== null) {
try {
return self::$client->set($key, $value, $ttl);
} catch (\RedisException $e) {
$logger->warning('Redis set operation failed: ' . $e->getMessage());
}
}
}
public static function getLastRevertTime($page_title, $user)
{
$key = 'cbng:last_reverted:' . hash('sha256', $page_title . ':' . $user);
$value = self::executeGet($key);
return ($value !== false && $value !== null) ? (int) $value : null;
}
public static function saveRevertTime($page_title, $user)
{
$key = 'cbng:last_reverted:' . hash('sha256', $page_title . ':' . $user);
self::executeSet($key, time(), 24 * 60 * 60);
}
public static function getLastHttpEventId()
{
$value = self::executeGet('cbng:http_feed_last_id');
return ($value !== false) ? $value : null;
}
public static function saveLastHttpEventId($id)
{
self::executeSet('cbng:http_feed_last_id', $id, 10 * 60);
}
}