<?php /* --------------------------------------------------- Class: cacher Author: Adam Flatau of Reflect Studios Date: 03/14/08 Description: Class to mange both APC and memcache data transactions. --------------------------------------------------*/ class cacher{ /* ------------------------------------ Our first two set of arrays we will be defined in our config. To hold the server IPs and ports for the memcache servers. Each array must have the same index matching with server to ip example: serverArray[0] = localhost serverArray[1] = server2 portArray[0] = 11211 portArray[1] = 1337 ------------------------------------*/ public $serverArray = MEMCACHE_SERVER_ARRAY; // we will define an array of servers in the config for our memcache public $portArray = MEMCACHE_PORT_ARRAY; // defnine our server ports public $memCache; // reserved for our memcache object if needed function __construct($memcache = 0){ if($memcache){ $this->memCache = new Memcache; } } function memConnect($server = '' , $pCon = 0){ /* -------------------------------------- Our function to connect to the memcache server. We will go ahead and get our server IP address from our serverArray and port from portArray. If failure to connecting to server, it will try next sever in the array. If pcon is set to true, it will then open up a pressitent connection. If server is supllied it will connect with that server. We Will check to see if the server is an IP or index value for the serverArray. --------------------------------------*/ if($this->memCache){ if(!empty($server)){ if(is_numeric($server)){ // index from serverArray $memcon = $this->memCache->connect($this->serverArray[$server]); if($memcon){ return array(1,1); }else{ $this->p_error(0,'There was an error while trying to connect to the memcache server IP:' . $server); return array(0,'Error with connection to memcache server'); } }else{ // use custom IP , we will do an // explode to see if there is a port $serverData = explode(':',$server); $server = $serverData[0]; if(!empty($serverData[1])){ $memcon = @$this->memCache->connect($server,$serverData[1]); if($memcon){ return array(1,1); }else{ $this->p_error(0,'There was an error while trying to connect to the memcache server IP:' . $server . ' Port:' . $serverData[1]); return array(0,'Error with connection to memcache server'); } }else{ $memcon = @this->memCache->connect($server,'11211'); if($memcon){ return array(1,1); }else{ $this->p_error(0,'There was an error while trying to connect to the memcache server IP:' . $server); return array(0,'Error with connection to memcache server'); } } } }else{ // ok grab one from the serverArray } }else{ // memcache object was not created in the constructor } } function p_error($err,$sql = ""){ switch($err){ case 0: // custom error $err = "0 - " . date("m-j-y") . " Custom Error: " . $sql; write_error("CACHER",$err); break; default: break; } } function __destruct(){ } }?>