Go to the documentation of this file.00001 <?php
00002
00003
00004
00013 class wsdlcache {
00018 var $fplock;
00023 var $cache_lifetime;
00028 var $cache_dir;
00033 var $debug_str = '';
00034
00042 function wsdlcache($cache_dir='.', $cache_lifetime=0) {
00043 $this->fplock = array();
00044 $this->cache_dir = $cache_dir != '' ? $cache_dir : '.';
00045 $this->cache_lifetime = $cache_lifetime;
00046 }
00047
00055 function createFilename($wsdl) {
00056 return $this->cache_dir.'/wsdlcache-' . md5($wsdl);
00057 }
00058
00065 function debug($string){
00066 $this->debug_str .= get_class($this).": $string\n";
00067 }
00068
00076 function get($wsdl) {
00077 $filename = $this->createFilename($wsdl);
00078 if ($this->obtainMutex($filename, "r")) {
00079
00080 if ($this->cache_lifetime > 0) {
00081 if (file_exists($filename) && (time() - filemtime($filename) > $this->cache_lifetime)) {
00082 unlink($filename);
00083 $this->debug("Expired $wsdl ($filename) from cache");
00084 $this->releaseMutex($filename);
00085 return null;
00086 }
00087 }
00088
00089 $fp = @fopen($filename, "r");
00090 if ($fp) {
00091 $s = implode("", @file($filename));
00092 fclose($fp);
00093 $this->debug("Got $wsdl ($filename) from cache");
00094 } else {
00095 $s = null;
00096 $this->debug("$wsdl ($filename) not in cache");
00097 }
00098 $this->releaseMutex($filename);
00099 return (!is_null($s)) ? unserialize($s) : null;
00100 } else {
00101 $this->debug("Unable to obtain mutex for $filename in get");
00102 }
00103 return null;
00104 }
00105
00114 function obtainMutex($filename, $mode) {
00115 if (isset($this->fplock[md5($filename)])) {
00116 $this->debug("Lock for $filename already exists");
00117 return false;
00118 }
00119 $this->fplock[md5($filename)] = fopen($filename.".lock", "w");
00120 if ($mode == "r") {
00121 return flock($this->fplock[md5($filename)], LOCK_SH);
00122 } else {
00123 return flock($this->fplock[md5($filename)], LOCK_EX);
00124 }
00125 }
00126
00134 function put($wsdl_instance) {
00135 $filename = $this->createFilename($wsdl_instance->wsdl);
00136 $s = serialize($wsdl_instance);
00137 if ($this->obtainMutex($filename, "w")) {
00138 $fp = fopen($filename, "w");
00139 fputs($fp, $s);
00140 fclose($fp);
00141 $this->debug("Put $wsdl_instance->wsdl ($filename) in cache");
00142 $this->releaseMutex($filename);
00143 return true;
00144 } else {
00145 $this->debug("Unable to obtain mutex for $filename in put");
00146 }
00147 return false;
00148 }
00149
00157 function releaseMutex($filename) {
00158 $ret = flock($this->fplock[md5($filename)], LOCK_UN);
00159 fclose($this->fplock[md5($filename)]);
00160 unset($this->fplock[md5($filename)]);
00161 if (! $ret) {
00162 $this->debug("Not able to release lock for $filename");
00163 }
00164 return $ret;
00165 }
00166
00174 function remove($wsdl) {
00175 $filename = $this->createFilename($wsdl);
00176
00177 $this->obtainMutex($filename, "w");
00178 $ret = unlink($filename);
00179 $this->debug("Removed ($ret) $wsdl ($filename) from cache");
00180 $this->releaseMutex($filename);
00181 return $ret;
00182 }
00183 }
00184 ?>