Project

General

Profile

1
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
require_once('./controller.php');
5

    
6
class RefreshCache {
7
	private $log;
8
	private $cache = null;
9

    
10
	public function __construct(){
11
		
12
		global $redis_host;
13
		global $redis_port;
14
		global $redis_scheme;
15
		
16
		global $host;
17

    
18
		$this->log = Logger::getLogger(__CLASS__);
19
		if(class_exists("Predis\Client")){
20
		
21
			try {
22
				
23
				$this->cache = new Predis\Client(array( 
24
					"scheme" => $redis_scheme,
25
					"host" => $redis_host,
26
        			"port" => $redis_port,
27
					"read_write_timeout" => 0));
28

    
29
				$this->log->info("redis host: ".$redis_host);
30
				$this->log->info("redis port: ".$redis_port);
31
				
32
			} catch(Exception $e) {
33
				$this->log->error("Error connecting to Redis server: ".$e->getMessage());
34
				$this->cache = null; 
35
			}
36
		}
37
    	else{
38
            $this->log->info("cache does not exist"); //predis
39
			exit;
40
    	}
41
	}
42
	
43
	function refresh() {
44
	
45
		if($this->cache != null){ 
46
			$this->log->info("cache exists");
47
			
48
			$database = new MYDB();
49
			$database->doConnect($GLOBALS['schema_file']);
50
			
51
			$keys = $this->cache->keys("*");
52
			for($x=0;$x<count($keys);$x++) {
53
								
54
				if($this->cache->hget($keys[$x], 'persistent')) {
55
				
56
					if(($query = $this->cache->hget($keys[$x], 'query'))!=null) {
57
					
58
						$this->log->info("going to recalculate the results for key: ".$keys[$x]);
59
						$fetchMode = $this->cache->hget($keys[$x], 'fetchMode');
60
						$results = $database->doQueryNoCache($query, $fetchMode);
61
						$this->cache->hset($keys[$x], 'results', json_encode($results));
62
					}
63
  						
64
  				} else {
65
  					
66
  					$this->log->info("going to delete the key: ".$keys[$x]);
67
  					$this->cache->del($keys[$x]);
68
  				}
69
  			}
70
  			
71
  			$database->doDisconnect();
72
			
73
		} else {
74
			$this->log->info("cache does not exist");
75
		}
76
	}
77
}
78

    
79
$rc = new RefreshCache();
80
$rc->refresh();
81

    
82

    
83
?>
(21-21/28)