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 MigrateCache {
7
	private $log;
8
	private $db;
9
	private $cache = null; //predis
10
private $remotecache=null;
11
public function __construct(){
12
		global $redis_host;
13
		global $redis_port;
14
		global $redis_scheme;
15
		global $host;  
16
                global $db_name;
17
                               $this->log = Logger::getLogger("__CLASS__");
18
               Logger::configure('./js/log4php/log4php.xml');	
19
 
20
             if(class_exists("Predis\Client")){
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
public function migrate($redis_remote_host,$redis_remote_port)
36
{global $redis_host;
37
                global $redis_port;
38
                global $redis_scheme;
39
                global $host;
40
          
41
$backupKeys=$this->cache->hkeys('STATS_NUMBERS');
42
$this->log->info("migrating Stat Numbers from ".$redis_host." to  ".$redis_remote_host." on ".$redis_remote_port);
43
try {
44
//todo changed timeout from 0 to -1 
45
                        $this->remotecache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_remote_host,"port" => $redis_port,"read_write_timeout" => -1));
46
                                $this->log->info("redis host: ".$redis_remote_host." and redis port: ".$redis_remote_port);
47
                        } catch(Exception $e) {
48
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
49
                                $this->cache = null;
50
                        }
51
$this->remotecache->connect();
52
for($x=0;$x<count($backupKeys);$x++)
53
{
54
  $key=$backupKeys[$x];
55
  $value=$this->cache->hget('STATS_NUMBERS',$key);
56

    
57
$this->log->info("saving  : ".$key." : ".$value);
58
$this->remotecache->hmset('STATS_NUMBERS',$key,$value);
59
$this->log->info("query saved  : ".$this->remotecache->hget('STATS_NUMBERS',$key));
60
}
61
 $this->log->info("migrating Shadow Stat Numbers from ".$redis_host." to  ".$redis_remote_host." on ".$redis_remote_port);
62
$backupKeys=$this->cache->hkeys('SHADOW_STATS_NUMBERS');
63

    
64
for($x=0;$x<count($backupKeys);$x++)
65
{
66
        $key=$backupKeys[$x];
67
        $value=$this->cache->hget('SHADOW_STATS_NUMBERS',$key);
68

    
69
$this->log->info("saving  : ".$key." : ".$value);
70
$this->remotecache->hmset('SHADOW_STATS_NUMBERS',$key,$value);
71
$this->log->info("query saved  : ".$this->remotecache->hget('SHADOW_STATS_NUMBERS',$key));
72
}
73
//*****************************
74
$this->log->info("Migrating data queries...");
75
$this->log->info("*******************************************");
76
$keys = $this->cache->keys("*");
77
for($x=0;$x<count($keys);$x++) {
78
if (strpos($keys[$x],'STATS')===false)
79
{try
80
{$values=$this->cache->hgetall($keys[$x]);
81
$this->log->info("Moving...");
82
$this->remotecache->hmset($keys[$x],$values);
83
$this->log->info("id : ".$keys[$x]);
84
//$this->log->info("res: ".$this->remotecache->hget($keys[$x],"results"));} 
85
}
86
catch(Exception $e) 
87
{$this->log->error('Error : '.$e->getMessage());}
88
}
89
else
90
{$this->log->info("Ignoring chart key: ".$keys[$x]); }}
91
$this->cache->quit();
92

    
93

    
94
$keys = $this->remotecache->keys("*");
95

    
96
$keys = $this->remotecache->keys("*");
97
for($x=0;$x<count($keys);$x++)
98
{if (strpos($keys[$x],'STATS')===false&&$this->remotecache->hget($keys[$x],"persistent")!='true')
99
{try{
100
$this->log->info("Deleting...".$keys[$x]." for pers ".$this->remotecache->hget($keys[$x],"persistent"));
101
$this->remotecache->del($keys[$x]);
102
}
103
catch(Exception $e) {
104
$this->log->error('Error : '.$e->getMessage());
105
}
106
}
107

    
108
}
109

    
110

    
111
$this->remotecache->quit();
112
$this->log->info("Done migrating ! ");
113
}
114

    
115
}
116
//$rc = new MigrateCache();
117
//$rc->migrate();
118
?>
(12-12/28)