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 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 33689 eri.katsar
		global $host;
19
                global $db_name;
20 33214 eri.katsar
21
22 33689 eri.katsar
//$redis_host='localhost';
23
/*$redis_remote_host='duffy.di.uoa.gr';
24
25 33214 eri.katsar
                $redis_remote_port=$redis_port;
26
                $redis_remote_scheme=$redis_scheme;
27
              */
28 37264 eri.katsar
29 33214 eri.katsar
               $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 33689 eri.katsar
public function migrate($redis_remote_host,$redis_remote_port)
51 33214 eri.katsar
{
52
53
global $redis_host;
54
                global $redis_port;
55
                global $redis_scheme;
56
                global $host;
57 37264 eri.katsar
58 33214 eri.katsar
59 33668 eri.katsar
$backupKeys=$this->cache->hkeys('STATS_NUMBERS');
60 33689 eri.katsar
$this->log->info("migrating Stat Numbers from ".$redis_host." to  ".$redis_remote_host." on ".$redis_remote_port);
61 33214 eri.katsar
62 33668 eri.katsar
try {
63 33214 eri.katsar
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 33689 eri.katsar
                                $this->log->info("redis host: ".$redis_remote_host." and redis port: ".$redis_remote_port);
67 33214 eri.katsar
                        } catch(Exception $e) {
68
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
69
                                $this->cache = null;
70
                        }
71
72
73 33668 eri.katsar
$this->remotecache->connect();
74 33214 eri.katsar
75 33668 eri.katsar
for($x=0;$x<count($backupKeys);$x++)
76
{
77
        $key=$backupKeys[$x];
78
        $value=$this->cache->hget('STATS_NUMBERS',$key);
79 33214 eri.katsar
80 33668 eri.katsar
$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 33214 eri.katsar
85 37264 eri.katsar
 $this->log->info("migrating Shadow Stat Numbers from ".$redis_host." to  ".$redis_remote_host." on ".$redis_remote_port);
86 34069 eri.katsar
87
88 34644 eri.katsar
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 33668 eri.katsar
$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 33214 eri.katsar
if ((strpos($keys[$x],'chart')===false)&&(strpos($keys[$x],'table')===false)&&(strpos($keys[$x],'STATS')===false) )
117
{
118
119 33668 eri.katsar
 try
120
{
121
122 33214 eri.katsar
$values=$this->cache->hgetall($keys[$x]);
123 37264 eri.katsar
124 33668 eri.katsar
{
125 33214 eri.katsar
126
$this->log->info("Moving...");
127
128
$this->remotecache->hmset($keys[$x],$values);
129
130 33668 eri.katsar
$this->log->info("id : ".$keys[$x]);
131 33689 eri.katsar
//$this->log->info("query : ".$this->remotecache->hget($keys[$x],"query"));
132 37264 eri.katsar
$this->log->info("res: ".$this->remotecache->hget($keys[$x],"results"));
133 33668 eri.katsar
}
134 33214 eri.katsar
135 34471 eri.katsar
136 33214 eri.katsar
137
}
138
catch(Exception $e) {
139
140
$this->log->error('Error : '.$e->getMessage());
141
}
142 33668 eri.katsar
}
143 33214 eri.katsar
 else
144
{ $this->log->info("Ignoring chart key: ".$keys[$x]); }
145 33668 eri.katsar
146
147 33214 eri.katsar
}
148
149 34471 eri.katsar
150
151
152 33668 eri.katsar
$this->remotecache->quit();
153 33214 eri.katsar
$this->cache->quit();
154
155 33668 eri.katsar
$this->log->info("Done migtating ! ");
156 33214 eri.katsar
157
}
158
159 33668 eri.katsar
}
160 33214 eri.katsar
161 33689 eri.katsar
//$rc = new MigrateCache();
162
//$rc->migrate();
163 33214 eri.katsar
?>