<?php | |
include "User.php"; | |
use \User as User; | |
//Connecting to Redis server on localhost | |
function connect(){ | |
include 'config.php'; | |
$redis = new Redis(); | |
$redis->connect('127.0.0.1', 6379); | |
$redis->auth("$REDIS_PASS"); | |
return $redis; | |
} | |
function redis_get($key){ | |
$redis = connect(); | |
return json_decode($redis->get($key)); | |
} | |
function redis_get_user($key) { | |
$redis = connect(); | |
$data = json_decode($redis->get($key)); | |
if ($data && gettype($data) == "object") { | |
$user = new User($data->user_name, $data->first_name, $data->last_name, $data->email, $data->password, $data->organization); | |
return $user; | |
} else { | |
return null; | |
} | |
} | |
function redis_set($key, $data, $timeout=null){ | |
$redis = connect(); | |
$redis->set($key, json_encode($data), $timeout); | |
} | |
function redis_inc_ipdata($ip, $attr, $get=false){ | |
$count = redis_get($ip); | |
if ($count){ | |
if (isset($count->$attr)) $count->$attr = $count->$attr+1; | |
else $count->$attr = 1; | |
}else $count = (object)[$attr=>1]; | |
if (!$get) redis_set($ip, $count, 3600); | |
return $count->$attr; | |
} | |
function redis_delete($key){ | |
$redis = connect(); | |
$redis->del($key); | |
} |