00001 <?php 00002 /* 00003 The NuSOAP project home is: 00004 http://sourceforge.net/projects/nusoap/ 00005 00006 The primary support for NuSOAP is the mailing list: 00007 nusoap-general@lists.sourceforge.net 00008 */ 00009 00018 class nusoap_wsdlcache { 00023 var $fplock; 00028 var $cache_lifetime; 00033 var $cache_dir; 00038 var $debug_str = ''; 00039 00047 function nusoap_wsdlcache($cache_dir='.', $cache_lifetime=0) { 00048 $this->fplock = array(); 00049 $this->cache_dir = $cache_dir != '' ? $cache_dir : '.'; 00050 $this->cache_lifetime = $cache_lifetime; 00051 } 00052 00060 function createFilename($wsdl) { 00061 return $this->cache_dir.'/wsdlcache-' . md5($wsdl); 00062 } 00063 00070 function debug($string){ 00071 $this->debug_str .= get_class($this).": $string\n"; 00072 } 00073 00081 function get($wsdl) { 00082 $filename = $this->createFilename($wsdl); 00083 if ($this->obtainMutex($filename, "r")) { 00084 // check for expired WSDL that must be removed from the cache 00085 if ($this->cache_lifetime > 0) { 00086 if (file_exists($filename) && (time() - filemtime($filename) > $this->cache_lifetime)) { 00087 unlink($filename); 00088 $this->debug("Expired $wsdl ($filename) from cache"); 00089 $this->releaseMutex($filename); 00090 return null; 00091 } 00092 } 00093 // see what there is to return 00094 if (!file_exists($filename)) { 00095 $this->debug("$wsdl ($filename) not in cache (1)"); 00096 $this->releaseMutex($filename); 00097 return null; 00098 } 00099 $fp = @fopen($filename, "r"); 00100 if ($fp) { 00101 $s = implode("", @file($filename)); 00102 fclose($fp); 00103 $this->debug("Got $wsdl ($filename) from cache"); 00104 } else { 00105 $s = null; 00106 $this->debug("$wsdl ($filename) not in cache (2)"); 00107 } 00108 $this->releaseMutex($filename); 00109 return (!is_null($s)) ? unserialize($s) : null; 00110 } else { 00111 $this->debug("Unable to obtain mutex for $filename in get"); 00112 } 00113 return null; 00114 } 00115 00124 function obtainMutex($filename, $mode) { 00125 if (isset($this->fplock[md5($filename)])) { 00126 $this->debug("Lock for $filename already exists"); 00127 return false; 00128 } 00129 $this->fplock[md5($filename)] = fopen($filename.".lock", "w"); 00130 if ($mode == "r") { 00131 return flock($this->fplock[md5($filename)], LOCK_SH); 00132 } else { 00133 return flock($this->fplock[md5($filename)], LOCK_EX); 00134 } 00135 } 00136 00144 function put($wsdl_instance) { 00145 $filename = $this->createFilename($wsdl_instance->wsdl); 00146 $s = serialize($wsdl_instance); 00147 if ($this->obtainMutex($filename, "w")) { 00148 $fp = fopen($filename, "w"); 00149 if (! $fp) { 00150 $this->debug("Cannot write $wsdl_instance->wsdl ($filename) in cache"); 00151 $this->releaseMutex($filename); 00152 return false; 00153 } 00154 fputs($fp, $s); 00155 fclose($fp); 00156 $this->debug("Put $wsdl_instance->wsdl ($filename) in cache"); 00157 $this->releaseMutex($filename); 00158 return true; 00159 } else { 00160 $this->debug("Unable to obtain mutex for $filename in put"); 00161 } 00162 return false; 00163 } 00164 00172 function releaseMutex($filename) { 00173 $ret = flock($this->fplock[md5($filename)], LOCK_UN); 00174 fclose($this->fplock[md5($filename)]); 00175 unset($this->fplock[md5($filename)]); 00176 if (! $ret) { 00177 $this->debug("Not able to release lock for $filename"); 00178 } 00179 return $ret; 00180 } 00181 00189 function remove($wsdl) { 00190 $filename = $this->createFilename($wsdl); 00191 if (!file_exists($filename)) { 00192 $this->debug("$wsdl ($filename) not in cache to be removed"); 00193 return false; 00194 } 00195 // ignore errors obtaining mutex 00196 $this->obtainMutex($filename, "w"); 00197 $ret = unlink($filename); 00198 $this->debug("Removed ($ret) $wsdl ($filename) from cache"); 00199 $this->releaseMutex($filename); 00200 return $ret; 00201 } 00202 } 00203 00207 class wsdlcache extends nusoap_wsdlcache { 00208 } 00209 ?>
Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|