Update Readme + update test x redis con classe wrapper FUNZIONA GEOADD!!
This commit is contained in:
@@ -28,6 +28,28 @@ Internet (RED) --|--> LB02.eqn --|--> GREEN --|--> FE02 --|--> DB
|
||||
in aggiunta
|
||||
- RED: server REDIS per persistenza "di breve durata" (redis01)
|
||||
|
||||
### LB01/02/03
|
||||
|
||||
### FE01/02/03
|
||||
|
||||
Sono macchine Ubuntu 14.04 LTS (andranno aggiornate a 18.04 LTS, saltiamo la 16.04)
|
||||
|
||||
Per far funzionare Redis si è scelto un wrapper minimale che non ha dipendenze con le librerie installabili sulla 14.04:
|
||||
|
||||
- https://github.com/ziogas/PHP-Redis-implementation
|
||||
- copiato in locale ed inserito script...
|
||||
|
||||
### Redis01
|
||||
|
||||
Installato server redis. poiché con ubuntu LST era vecchio (vers 3.0) abbiamo aggiornato a ultima release con ppa alternativi:
|
||||
|
||||
|
||||
add-apt-repository ppa:chris-lea/redis-server
|
||||
apt-get update
|
||||
apt-get upgrade
|
||||
|
||||
### DB
|
||||
|
||||
|
||||
## Comandi utili
|
||||
|
||||
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,323 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Raw redis wrapper, all the commands are passed as-is
|
||||
* More information and usage examples could be found on https://github.com/ziogas/PHP-Redis-implementation
|
||||
*
|
||||
* Based on http://redis.io/topics/protocol
|
||||
*/
|
||||
class redis_cli
|
||||
{
|
||||
const INTEGER = ':';
|
||||
const INLINE = '+';
|
||||
const BULK = '$';
|
||||
const MULTIBULK = '*';
|
||||
const ERROR = '-';
|
||||
const NL = "\r\n";
|
||||
|
||||
private $handle = false;
|
||||
private $host;
|
||||
private $port;
|
||||
private $silent_fail;
|
||||
|
||||
private $commands = array();
|
||||
|
||||
//Timeout for stream, 30 seconds
|
||||
private $timeout = 30;
|
||||
|
||||
//Timeout for socket connection
|
||||
private $connect_timeout = 3;
|
||||
|
||||
//Use this with extreme caution
|
||||
private $force_reconnect = false;
|
||||
|
||||
//Error handling, debug info
|
||||
private $last_used_command = '';
|
||||
|
||||
//Error handling function, use set_error_function method ()
|
||||
private $error_function = null;
|
||||
|
||||
public function __construct($host = false, $port = false, $silent_fail = false, $timeout = 60)
|
||||
{
|
||||
if ($host && $port) {
|
||||
$this->connect($host, $port, $silent_fail, $timeout);
|
||||
}
|
||||
}
|
||||
|
||||
//Main method to establish connection
|
||||
public function connect($host = '127.0.0.1', $port = 6379, $silent_fail = false, $timeout = 60)
|
||||
{
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
$this->silent_fail = $silent_fail;
|
||||
$this->timeout = $timeout;
|
||||
|
||||
if ($silent_fail) {
|
||||
$this->handle = @fsockopen($host, $port, $errno, $errstr, $this->connect_timeout);
|
||||
|
||||
if (!$this->handle) {
|
||||
$this->handle = false;
|
||||
}
|
||||
} else {
|
||||
$this->handle = fsockopen($host, $port, $errno, $errstr, $this->connect_timeout);
|
||||
}
|
||||
|
||||
if (is_resource($this->handle)) {
|
||||
stream_set_timeout($this->handle, $this->timeout);
|
||||
}
|
||||
}
|
||||
|
||||
public function reconnect()
|
||||
{
|
||||
$this->__destruct();
|
||||
$this->connect($this->host, $this->port, $this->silent_fail);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if (is_resource($this->handle)) {
|
||||
fclose($this->handle);
|
||||
}
|
||||
}
|
||||
|
||||
//Returns all commands array
|
||||
public function commands()
|
||||
{
|
||||
return $this->commands;
|
||||
}
|
||||
|
||||
//Used to push single command to queue
|
||||
public function cmd()
|
||||
{
|
||||
if (!$this->handle) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$args = func_get_args();
|
||||
$rlen = count($args);
|
||||
|
||||
$output = '*'. $rlen . self::NL;
|
||||
|
||||
foreach ($args as $arg) {
|
||||
$output .= '$'. strlen($arg) . self::NL . $arg . self::NL;
|
||||
}
|
||||
|
||||
$this->commands[] = $output;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
//Used to push many commands at once, almost always for setting something
|
||||
public function set()
|
||||
{
|
||||
if (!$this->handle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//Total size of commands
|
||||
$size = $this->exec();
|
||||
$response = array();
|
||||
|
||||
for ($i=0; $i<$size; $i++) {
|
||||
$response[] = $this->get_response();
|
||||
}
|
||||
|
||||
if ($this->force_reconnect) {
|
||||
$this->reconnect();
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
//Used to get command response
|
||||
public function get($line = false)
|
||||
{
|
||||
if (!$this->handle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$return = false;
|
||||
|
||||
if ($this->exec()) {
|
||||
$return = $this->get_response();
|
||||
|
||||
if ($this->force_reconnect) {
|
||||
$this->reconnect();
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
//Used to get length of the returned array. Most useful with `Keys` command
|
||||
public function get_len()
|
||||
{
|
||||
if (!$this->handle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$return = null;
|
||||
|
||||
if ($this->exec()) {
|
||||
$char = fgetc($this->handle);
|
||||
|
||||
if ($char == self::BULK) {
|
||||
$return = sizeof($this->bulk_response());
|
||||
} elseif ($char == self::MULTIBULK) {
|
||||
$return = sizeof($this->multibulk_response());
|
||||
}
|
||||
|
||||
if ($this->force_reconnect) {
|
||||
$this->reconnect();
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
//Forces to reconnect after every get() or set(). Use this with extreme caution
|
||||
public function set_force_reconnect($flag)
|
||||
{
|
||||
$this->force_reconnect = $flag;
|
||||
return $this;
|
||||
}
|
||||
|
||||
//Used to parse single command single response
|
||||
private function get_response()
|
||||
{
|
||||
$return = false;
|
||||
|
||||
$char = fgetc($this->handle);
|
||||
|
||||
switch ($char) {
|
||||
case self::INLINE:
|
||||
$return = $this->inline_response();
|
||||
break;
|
||||
case self::INTEGER:
|
||||
$return = $this->integer_response();
|
||||
break;
|
||||
case self::BULK:
|
||||
$return = $this->bulk_response();
|
||||
break;
|
||||
case self::MULTIBULK:
|
||||
$return = $this->multibulk_response();
|
||||
break;
|
||||
case self::ERROR:
|
||||
$return = $this->error_response();
|
||||
break;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
//For inline responses only
|
||||
private function inline_response()
|
||||
{
|
||||
return trim(fgets($this->handle));
|
||||
}
|
||||
|
||||
//For integer responses only
|
||||
private function integer_response()
|
||||
{
|
||||
return ( int ) trim(fgets($this->handle));
|
||||
}
|
||||
|
||||
//For error responses only
|
||||
private function error_response()
|
||||
{
|
||||
$error = fgets($this->handle);
|
||||
|
||||
if ($this->error_function) {
|
||||
call_user_func($this->error_function, $error .'('. $this->last_used_command .')');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//For bulk responses only
|
||||
private function bulk_response()
|
||||
{
|
||||
$return = trim(fgets($this->handle));
|
||||
|
||||
if ($return === '-1') {
|
||||
$return = null;
|
||||
} else {
|
||||
$return = $this->read_bulk_response($return);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
//For multibulk responses only
|
||||
private function multibulk_response()
|
||||
{
|
||||
$size = trim(fgets($this->handle));
|
||||
$return = false;
|
||||
|
||||
if ($size === '-1') {
|
||||
$return = null;
|
||||
} else {
|
||||
$return = array();
|
||||
|
||||
for ($i = 0; $i < $size; $i++) {
|
||||
$return[] = $this->get_response();
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
//Sends command to the redis
|
||||
private function exec()
|
||||
{
|
||||
$size = sizeof($this->commands);
|
||||
|
||||
if ($size < 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->error_function) {
|
||||
$this->last_used_command = str_replace(self::NL, '\\r\\n', implode(';', $this->commands));
|
||||
}
|
||||
|
||||
$command = implode(self::NL, $this->commands) . self::NL;
|
||||
fwrite($this->handle, $command);
|
||||
|
||||
$this->commands = array();
|
||||
return $size;
|
||||
}
|
||||
|
||||
//Bulk response reader
|
||||
private function read_bulk_response($tmp)
|
||||
{
|
||||
$response = null;
|
||||
|
||||
$read = 0;
|
||||
$size = ((strlen($tmp) > 1 && substr($tmp, 0, 1) === self::BULK) ? substr($tmp, 1) : $tmp);
|
||||
|
||||
while ($read < $size) {
|
||||
$diff = $size - $read;
|
||||
|
||||
$block_size = $diff > 8192 ? 8192 : $diff;
|
||||
|
||||
$chunk = fread($this->handle, $block_size);
|
||||
|
||||
if ($chunk !== false) {
|
||||
$chunkLen = strlen($chunk);
|
||||
$read += $chunkLen;
|
||||
$response .= $chunk;
|
||||
} else {
|
||||
fseek($this->handle, $read);
|
||||
}
|
||||
}
|
||||
|
||||
fgets($this->handle);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function set_error_function($func)
|
||||
{
|
||||
$this->error_function = $func;
|
||||
}
|
||||
}
|
||||
+14
-78
@@ -1,28 +1,19 @@
|
||||
<h1>EQN</h1>
|
||||
Testing procedure Redis per EQN...
|
||||
<?php
|
||||
#Caricamento oggetto redis server: occhio alla sintassi x nostra versione php
|
||||
#PredisAutoloader::register();
|
||||
require "Predis/Autoloader.php";
|
||||
Predis\Autoloader::register();
|
||||
|
||||
try {
|
||||
$redis = new Predis\Client();
|
||||
// Connessione server remoto
|
||||
$redis = new Predis\Client(array(
|
||||
"scheme" => "tcp",
|
||||
#"host" => "172.16.1.80",
|
||||
"host" => "redis01",
|
||||
"port" => 6379
|
||||
));
|
||||
require 'redis.php';
|
||||
|
||||
function redis_error($error) {
|
||||
throw new error($error);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
die($e->getMessage());
|
||||
}
|
||||
?>
|
||||
Server inizializzato, proseguono test
|
||||
<hr/>
|
||||
<?php
|
||||
|
||||
$redis = new redis_cli ();
|
||||
$redis->connect("redis01");
|
||||
$redis->set_error_function('redis_error');
|
||||
|
||||
echo "Server inizializzato, proseguono test<hr/>";
|
||||
|
||||
#------------------------------------------------------
|
||||
# VALORI DI CONFIGURAZIONE (portare su file ext!!!)
|
||||
#------------------------------------------------------
|
||||
@@ -39,12 +30,12 @@ $nBlockRec = 10;
|
||||
# definizione funzioni helper x gestione REDIS dell'oggetto (UserID = numero, DTime in intero fino ai minuti yyMMddHHmm)
|
||||
function salvaPosizione($redis, $tblPos, $tblLast, $User_ID, $Lat, $Lon, $DTime) {
|
||||
# salvo dataora verifica
|
||||
$redis->zadd( $tblLast, $DTime, $User_ID );
|
||||
$redis->cmd('ZADD', $tblLast, $DTime, $User_ID )
|
||||
// # salvo posizione Geolocalizzata
|
||||
// $redis->geoadd( $tblPos, $Lat, $Lon, $User_ID )
|
||||
->cmd('GEOADD',$tblPos, $Lat, $Lon, $User_ID )
|
||||
->set();
|
||||
}
|
||||
|
||||
# $predis->zadd( 'myset', [ "one" => 1, "uno" => 1, "two" => 2, "three" => 3 ] )
|
||||
|
||||
|
||||
|
||||
@@ -68,59 +59,4 @@ salvaPosizione($redis, $tblPos, $tblLast, $uID, $cLat, $cLon, $currDT);
|
||||
|
||||
|
||||
|
||||
// $server = gethostname();
|
||||
// $adesso = date("F j, Y \a\t g:ia");
|
||||
// $redis->set($server,$adesso);
|
||||
// $redis->incr($server."_count");
|
||||
|
||||
// $redis->hset("taxi_car", "brand", "Toyota");
|
||||
// $redis->hset("taxi_car", "model", "Yaris");
|
||||
// $redis->hset("taxi_car", "license number", "RO-01-PHP");
|
||||
// $redis->hset("taxi_car", "year of fabrication", 2010);
|
||||
// $redis->hset("taxi_car", "nr_starts", 0);
|
||||
|
||||
// $valore = $redis->get($server);
|
||||
|
||||
// echo ($redis->exists($server)) ? "Valore salvato: ".$valore : "Prego salvare valore...";
|
||||
|
||||
// $taxi_car = $redis->hgetall("taxi_car");
|
||||
// echo "<hr/>";
|
||||
// echo "All info about taxi car";
|
||||
// echo "<pre>";
|
||||
// var_dump($taxi_car);
|
||||
// echo "</pre>";
|
||||
|
||||
// echo "<hr/>";
|
||||
// $list = "PHP Frameworks List";
|
||||
// $redis->rpush($list, "Symfony 2");
|
||||
// $redis->rpush($list, "Symfony 1.4");
|
||||
// $redis->lpush($list, "Zend Framework");
|
||||
|
||||
// echo "Number of frameworks in list: " . $redis->llen($list) . "<br>";
|
||||
|
||||
// $arList = $redis->lrange($list, 0, -1);
|
||||
// echo "<pre>";
|
||||
// print_r($arList);
|
||||
// echo "</pre>";
|
||||
|
||||
// // the last entry in the list
|
||||
// echo $redis->rpop($list) . "<br>";
|
||||
|
||||
// // the first entry in the list
|
||||
// echo $redis->lpop($list) . "<br>";
|
||||
|
||||
// echo "<hr/>";
|
||||
// // set the expiration for next week
|
||||
// $redis->set("expire in 1 week", "I have data for a week");
|
||||
// $redis->expireat("expire in 1 week", strtotime("+1 week"));
|
||||
// $ttl = $redis->ttl("expire in 1 week"); // will be 604800 seconds
|
||||
|
||||
// // set the expiration for one hour
|
||||
// $redis->set("expire in 1 hour", "I have data for an hour");
|
||||
// $redis->expire("expire in 1 hour", 3600);
|
||||
// $ttl = $redis->ttl("expire in 1 hour"); // will be 3600 seconds
|
||||
|
||||
// // never expires
|
||||
// $redis->set("never expire", "I want to leave forever!");
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user