matheusfillipe | abd513e | 2021-05-11 03:29:11 -0300 | [diff] [blame] | 1 | <?php |
| 2 | //Connecting to Redis server on localhost |
| 3 | function connect(){ |
| 4 | include 'config.php'; |
| 5 | $redis = new Redis(); |
| 6 | $redis->connect('127.0.0.1', 6379); |
| 7 | $redis->auth("$REDIS_PASS"); |
| 8 | return $redis; |
| 9 | } |
| 10 | function redis_get($key){ |
| 11 | $redis = connect(); |
| 12 | return json_decode($redis->get($key)); |
| 13 | } |
| 14 | function redis_set($key, $data, $timeout=null){ |
| 15 | $redis = connect(); |
| 16 | $redis->set($key, json_encode($data), $timeout); |
| 17 | } |
| 18 | |
| 19 | function redis_inc_ipdata($ip, $attr, $get=false){ |
| 20 | $count = redis_get($ip); |
| 21 | if ($count){ |
| 22 | if (isset($count->$attr)) $count->$attr = $count->$attr+1; |
| 23 | else $count->$attr = 1; |
| 24 | }else $count = (object)[$attr=>1]; |
| 25 | |
| 26 | if (!$get) redis_set($ip, $count, 3600); |
| 27 | return $count->$attr; |
| 28 | } |
| 29 | |
| 30 | function redis_delete($key){ |
| 31 | $redis = connect(); |
| 32 | $redis->del($key); |
| 33 | } |
| 34 | ?> |