Project

General

Profile

1 33214 eri.katsar
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
//require_once('./MYDB.php');
5
include_once('./MYDB.php');
6
//require_once('./controller.php');
7
8
class RefreshCharts{
9
	private $log;
10
	private $cache = null;
11
	public function __construct(){
12
13
		global $redis_host;
14
		global $redis_port;
15
		global $redis_scheme;
16
		global $host;
17
18
	$this->log = Logger::getLogger(__CLASS__);
19
      	Logger::configure('./js/log4php/log4php.xml');
20
21
              if(class_exists("Predis\Client")){
22
23
			try {
24
//todo changed timeout from 0 to -1
25
			$this->cache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_host,"port" => $redis_port,"read_write_timeout" => -1));
26
				$this->log->info("redis host: ".$redis_host." and redis port: ".$redis_port);
27
			} catch(Exception $e) {
28
				$this->log->error("Error connecting to Redis server: ".$e->getMessage());
29
				$this->cache = null;
30
			}
31
		}
32
    	else{
33
            $this->log->info("cache does not exist"); //predis
34
			exit;
35
    	}
36
	}
37
38
39
function refresh() {
40
		if($this->cache != null){
41
			$this->log->info("cache exists");
42
			$database = new MYDB();
43
			$database->doConnect($GLOBALS['schema_file']);
44
			$keys = $this->cache->keys("*");
45
46
 $this->log->info(" ******************REFRESHING CACHE*************************");
47
48
//TODO REFRESH CHARTS AND DATA
49
50
for($x=0;$x<count($keys);$x++) {
51 37264 eri.katsar
   $this->log->info("Looking up for key: ".$keys[$x]);
52 33214 eri.katsar
53
if ((strpos($keys[$x],'chart')===false)&&(strpos($keys[$x],'table')===false)&&(strpos($keys[$x],'STATS')===false))
54
{
55
   try
56
{
57
58
$query = $this->cache->hget($keys[$x],'query');
59
$persistent= $this->cache->hget($keys[$x],'persistent');
60 37784 eri.katsar
if($query!=null)
61 37264 eri.katsar
{						$this->log->info("going to recalculate the results for query  ".$query );
62 33214 eri.katsar
						$fetchMode = $this->cache->hget($keys[$x], 'fetchMode');
63
						if($fetchMode==null)
64
                                                      { $fetchMode=3; }
65
                                                $shadowResults = $database->doQueryNoCache($query, $fetchMode);
66
                                                $result = $this->cache->hget($keys[$x], 'results');
67
				               // $this->log->info("Currently stored results".$result );
68
                                               $backup = $this->cache->hget($keys[$x], 'backup');
69
                                                //$this->log->info("Currently stored backup".$backup);
70
71
                                                 if(json_encode($shadowResults))
72
                                                 {$shadowResults=json_encode($shadowResults);}
73
	$this->cache->hmset($keys[$x],array("results" => $result, "query" =>$query,"persistent" => $persistent,"shadow" => $shadowResults, "fetchMode" => $fetchMode, "backup" => $backup));
74 37264 eri.katsar
$this->log->info("Stored Entry : ");
75
$this->log->info("Shadow ".$this->cache->hget($keys[$x],'shadow'));
76 33214 eri.katsar
					}
77
else{
78
79
$this->log->info("going to delete the key: ".$keys[$x]);
80
                     $this->cache->del($keys[$x]);
81
}
82
83
}//catch
84
catch(Exception $e) {
85
$this->log->error('Error : '.$e->getMessage());
86
87
}
88
	}
89
90
91
 else
92
{
93 37264 eri.katsar
$this->log->info("Ignoring chart key: ".$keys[$x]);
94 33214 eri.katsar
95
 }
96
97
98
  			}
99
100
101
// for eeach key loop
102
103
  			$database->doDisconnect();
104
105
		} else {
106
			$this->log->info("cache does not exist");
107
		}
108
	}
109
}
110
//$rc = new RefreshCharts();
111
//$rc->refresh();
112
113
?>