Project

General

Profile

1
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
require_once('./MYDB.php');
5

    
6
class PromoteCharts {
7
	private $log;
8
	private $db;
9
	private $cache = null; //predis
10
	public function __construct(){
11
		
12
		global $redis_host;
13
		global $redis_port;
14
		global $redis_scheme;
15
		global $host;
16

    
17
	$this->log = Logger::getLogger(__CLASS__);
18
         Logger::configure('./js/log4php/log4php.xml');	
19

    
20
 if(class_exists("Predis\Client")){
21
                
22
                        try {
23
//todo changed timeout from 0 to -1 
24
                        $this->cache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_host,"port" => $redis_port,"read_write_timeout" => -1));
25
                                $this->log->info("redis host: ".$redis_host." and redis port: ".$redis_port);
26
                        } catch(Exception $e) {
27
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
28
                                $this->cache = null; 
29
                        }
30
                }
31
        else{
32
            $this->log->info("cache does not exist"); //predis
33
                        exit;
34
        }
35
	}
36

    
37

    
38
public function replace() 
39
{
40
		global $redis_host;
41
		global $redis_port;
42
		global $redis_scheme;
43
        $this->cache = new Predis\Client(array("scheme" => $redis_scheme, "host" => $redis_host, "port" => $redis_port));
44
	$this->cache->connect(); //predis
45

    
46

    
47
//now promote all other entries
48
 $keys = $this->cache->keys("*");
49

    
50
 $this->log->info("Promoting data queries...");
51
 $this->log->info("*******************************************");
52
  for($x=0;$x<count($keys);$x++) {
53
                        $this->log->info("Looking up for key: ".$keys[$x]);
54
if (strpos($keys[$x],'STATS')===false) 
55
{  
56
try
57
{
58
$query = $this->cache->hget($keys[$x],'query');
59
$persistent= $this->cache->hget($keys[$x],'persistent');
60
if($query!=null)
61
{             $this->log->info("going to replace the results for key: ".$keys[$x]."  :  \n ".$query."\n");
62
                                               $fetchMode = $this->cache->hget($keys[$x], 'fetchMode');
63
                                                $shadowResults = $this->cache->hget($keys[$x],'shadow');
64
                                                $result = $this->cache->hget($keys[$x], 'results');
65
$backup = $this->cache->hget($keys[$x], 'backup');
66
if($shadowResults==null)
67
{$shadowResults=$result;}
68
                                                //$this->log->info("Currently stored backup".$backup);
69
                                               //$this->log->info("With currently stored results".$result );
70
 $this->cache->hmset($keys[$x],array("results" =>$shadowResults,"query" =>$query,"persistent" =>$persistent,"shadow" =>$shadowResults,"fetchMode" =>$fetchMode,"backup" =>$backup));  
71
//$this->log->info(" Promoted Stored Entry : ");
72
}
73
else{
74
$this->log->info("going to delete the key: ".$keys[$x]);
75
$this->cache->del($keys[$x]);
76
}
77

    
78
}//catch
79
catch(Exception $e) {
80

    
81
$this->log->error('Error : '.$e->getMessage());
82

    
83
$this->log->info("going to delete problematic key: ".$keys[$x]);
84
$this->cache->del($keys[$x]);
85
}
86

    
87

    
88
        }
89
 else
90
{
91

    
92
 $this->log->info("Ignoring chart key: ".$keys[$x]);
93
if (strpos($keys[$x],'STATS')===false)
94
{ 
95
//todO  here should we delete chart entries so that changes are visible in portal?
96
$this->log->info("going to delete the key: ".$keys[$x]);
97
$this->cache->del($keys[$x]);
98
}
99
}
100
}
101
$this->log->info("All Shadow Entries Promoted Sucessfully!");
102
$this->cache->quit();
103

    
104
}
105
}
106
?>
(18-18/28)