Project

General

Profile

1 38716 eri.katsar
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
require_once('./MYDB.php');
5
6
class MigrateCache {
7
	private $log;
8
	private $db;
9
	private $cache = null; //predis
10
11
private $remotecache=null;
12
13
public function __construct(){
14
15
		global $redis_host;
16
		global $redis_port;
17
		global $redis_scheme;
18
		global $host;
19
                global $db_name;
20
21
22
//$redis_host='localhost';
23
/*$redis_remote_host='duffy.di.uoa.gr';
24
25
                $redis_remote_port=$redis_port;
26
                $redis_remote_scheme=$redis_scheme;
27
              */
28
29
               $this->log = Logger::getLogger("__CLASS__");
30
               Logger::configure('./js/log4php/log4php.xml');
31
32
             if(class_exists("Predis\Client")){
33
34
                        try {
35
//todo changed timeout from 0 to -1
36
                        $this->cache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_host,"port" => $redis_port,"read_write_timeout" => -1));
37
                                $this->log->info("redis host: ".$redis_host." and redis port: ".$redis_port);
38
                        } catch(Exception $e) {
39
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
40
                                $this->cache = null;
41
                        }
42
                }
43
        else{
44
            $this->log->info("cache does not exist"); //predis
45
                        exit;
46
        }
47
48
49
	}
50
public function migrate($redis_remote_host,$redis_remote_port)
51
{
52
53
global $redis_host;
54
                global $redis_port;
55
                global $redis_scheme;
56
                global $host;
57
58
59
$backupKeys=$this->cache->hkeys('STATS_NUMBERS');
60
$this->log->info("migrating Stat Numbers from ".$redis_host." to  ".$redis_remote_host." on ".$redis_remote_port);
61
62
try {
63
64
//todo changed timeout from 0 to -1
65
                        $this->remotecache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_remote_host,"port" => $redis_port,"read_write_timeout" => -1));
66
                                $this->log->info("redis host: ".$redis_remote_host." and redis port: ".$redis_remote_port);
67
                        } catch(Exception $e) {
68
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
69
                                $this->cache = null;
70
                        }
71
72
73
$this->remotecache->connect();
74
75
for($x=0;$x<count($backupKeys);$x++)
76
{
77
        $key=$backupKeys[$x];
78
        $value=$this->cache->hget('STATS_NUMBERS',$key);
79
80
$this->log->info("saving  : ".$key." : ".$value);
81
$this->remotecache->hmset('STATS_NUMBERS',$key,$value);
82
$this->log->info("query saved  : ".$this->remotecache->hget('STATS_NUMBERS',$key));
83
}
84
85
 $this->log->info("migrating Shadow Stat Numbers from ".$redis_host." to  ".$redis_remote_host." on ".$redis_remote_port);
86
87
88
89
$backupKeys=$this->cache->hkeys('SHADOW_STATS_NUMBERS');
90
91
92
93
for($x=0;$x<count($backupKeys);$x++)
94
{
95
        $key=$backupKeys[$x];
96
        $value=$this->cache->hget('SHADOW_STATS_NUMBERS',$key);
97
98
$this->log->info("saving  : ".$key." : ".$value);
99
$this->remotecache->hmset('SHADOW_STATS_NUMBERS',$key,$value);
100
$this->log->info("query saved  : ".$this->remotecache->hget('SHADOW_STATS_NUMBERS',$key));
101
}
102
103
104
105
//*****************************
106
107
108
$this->log->info("Migrating data queries...");
109
$this->log->info("*******************************************");
110
111
$keys = $this->cache->keys("*");
112
113
114
for($x=0;$x<count($keys);$x++) {
115
116
if ((strpos($keys[$x],'chart')===false)&&(strpos($keys[$x],'table')===false)&&(strpos($keys[$x],'STATS')===false) )
117
{
118
119
 try
120
{
121
122
$values=$this->cache->hgetall($keys[$x]);
123
124
{
125
126
$this->log->info("Moving...");
127
128
$this->remotecache->hmset($keys[$x],$values);
129
130
$this->log->info("id : ".$keys[$x]);
131
//$this->log->info("query : ".$this->remotecache->hget($keys[$x],"query"));
132
$this->log->info("res: ".$this->remotecache->hget($keys[$x],"results"));
133
}
134
135
136
137
}
138
catch(Exception $e) {
139
140
$this->log->error('Error : '.$e->getMessage());
141
}
142
}
143
 else
144
{ $this->log->info("Ignoring chart key: ".$keys[$x]); }
145
146
147
}
148
149
150
151
152
$this->remotecache->quit();
153
$this->cache->quit();
154
155
$this->log->info("Done migtating ! ");
156
157
}
158
159
}
160
161
//$rc = new MigrateCache();
162
//$rc->migrate();
163
?>