matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 1 | <?php |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 2 | include "User.php"; |
| 3 | use \User as User; |
| 4 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 5 | //Connecting to Redis server on localhost |
| 6 | function connect(){ |
| 7 | include 'config.php'; |
| 8 | $redis = new Redis(); |
| 9 | $redis->connect('127.0.0.1', 6379); |
| 10 | $redis->auth("$REDIS_PASS"); |
| 11 | return $redis; |
| 12 | } |
| 13 | function redis_get($key){ |
| 14 | $redis = connect(); |
| 15 | return json_decode($redis->get($key)); |
| 16 | } |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 17 | |
| 18 | function redis_get_user($key) { |
| 19 | $redis = connect(); |
| 20 | $data = json_decode($redis->get($key)); |
Marc Kupietz | c157611 | 2023-03-06 07:05:47 +0100 | [diff] [blame^] | 21 | if ($data && gettype($data) == "object") { |
| 22 | $user = new User($data->user_name, $data->first_name, $data->last_name, $data->email, $data->password, $data->organization); |
| 23 | return $user; |
| 24 | } else { |
| 25 | return null; |
| 26 | } |
Marc Kupietz | d871d88 | 2023-03-05 18:34:16 +0100 | [diff] [blame] | 27 | } |
| 28 | |
matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 29 | function redis_set($key, $data, $timeout=null){ |
| 30 | $redis = connect(); |
| 31 | $redis->set($key, json_encode($data), $timeout); |
| 32 | } |
| 33 | |
| 34 | function redis_inc_ipdata($ip, $attr, $get=false){ |
| 35 | $count = redis_get($ip); |
| 36 | if ($count){ |
| 37 | if (isset($count->$attr)) $count->$attr = $count->$attr+1; |
| 38 | else $count->$attr = 1; |
| 39 | }else $count = (object)[$attr=>1]; |
| 40 | |
| 41 | if (!$get) redis_set($ip, $count, 3600); |
| 42 | return $count->$attr; |
| 43 | } |
| 44 | |
| 45 | function redis_delete($key){ |
| 46 | $redis = connect(); |
| 47 | $redis->del($key); |
| 48 | } |