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
6
class RestoreCache {
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("RestoreCache");
18
           Logger::configure('./js/log4php/log4php.xml');
19
if(class_exists("Predis\Client")){
20
21
                        try {
22
//todo changed timeout from 0 to -1
23
                        $this->cache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_host,"port" => $redis_port,"read_write_timeout" => -1));
24
                                $this->log->info("redis host: ".$redis_host." and redis port: ".$redis_port);
25
                        } catch(Exception $e) {
26
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
27
                                $this->cache = null;
28
                        }
29
                }
30
        else{
31
            $this->log->info("cache does not exist"); //predis
32
                        exit;
33
        }
34
35
36
	}
37
38
public function restore()
39
{
40
41
//first restore numbers
42
43
 $backupKeys=$this->cache->hkeys('STATS_NUMBERS_BACKUP');
44
 $this->log->info("Restoring  Stat Numbers...");
45
46
for($x=0;$x<count($backupKeys);$x++)
47
{
48
	$key=$backupKeys[$x];
49
	$value=$this->cache->hget('STATS_NUMBERS_BACKUP',$key);
50
	$this->cache->hset('STATS_NUMBERS',$key,$value); }
51
52
//now promote all other entries
53
 $keys = $this->cache->keys("*");
54 33890 eri.katsar
55
$this->log->info("Restoring data queries...");
56
$this->log->info("*******************************************");
57 33214 eri.katsar
58
 for($x=0;$x<count($keys);$x++) {
59
60
if ((strpos($keys[$x],'chart')===false)&&(strpos($keys[$x],'table')===false)&&(strpos($keys[$x],'STATS')===false) )
61
{
62
 try{
63
$query = $this->cache->hget($keys[$x],'query');
64
$persistent= $this->cache->hget($keys[$x],'persistent');
65
66 37784 eri.katsar
if($query!=null)
67 33214 eri.katsar
{
68
69
                                                $fetchMode = $this->cache->hget($keys[$x], 'fetchMode');
70
                                                $shadowResults = $this->cache->hget($keys[$x],'shadow');
71
                                                $result = $this->cache->hget($keys[$x], 'results');
72
                                                $backup = $this->cache->hget($keys[$x], 'backup');
73
74
 $this->cache->hmset($keys[$x],array("results" =>$backup,"query" =>$query,"persistent" =>$persistent,"shadow" =>$shadowResults,"fetchMode" =>$fetchMode,"backup" =>$backup));
75 33890 eri.katsar
$this->log->info("Restored ".$keys[$x]);
76 33214 eri.katsar
77 33890 eri.katsar
78 33214 eri.katsar
}
79
80
}//catch
81
catch(Exception $e) {
82
83
$this->log->error('Error : '.$e->getMessage());
84
85
}
86
        }
87
 else
88
{ $this->log->info("Ignoring chart key: ".$keys[$x]); }
89
}
90
91
$this->cache->quit();
92
93
$this->log->info("Done! ");
94
	}
95
}
96
97
98
?>