Go to the documentation of this file.00001 <?php
00002
00003
00004
00013 class wsdlcache {
00014 var $fplock;
00015 var $cache_lifetime;
00016 var $cache_dir;
00017 var $debug_str = '';
00018
00019
00020
00021
00022
00023
00024
00025
00026 function wsdlcache($cache_dir='.', $cache_lifetime=0) {
00027 $this->fplock = array();
00028 $this->cache_dir = $cache_dir != '' ? $cache_dir : '.';
00029 $this->cache_lifetime = $cache_lifetime;
00030 }
00031
00039 function createFilename($wsdl) {
00040 return $this->cache_dir.'/wsdlcache-' . md5($wsdl);
00041 }
00042
00049 function debug($string){
00050 $this->debug_str .= get_class($this).": $string\n";
00051 }
00052
00060 function get($wsdl) {
00061 $filename = $this->createFilename($wsdl);
00062 if ($this->obtainMutex($filename, "r")) {
00063
00064 if ($this->cache_lifetime > 0) {
00065 if (file_exists($filename) && (time() - filemtime($filename) > $this->cache_lifetime)) {
00066 unlink($filename);
00067 $this->debug("Expired $wsdl ($filename) from cache");
00068 $this->releaseMutex($filename);
00069 return null;
00070 }
00071 }
00072
00073 $fp = @fopen($filename, "r");
00074 if ($fp) {
00075 $s = implode("", @file($filename));
00076 fclose($fp);
00077 $this->debug("Got $wsdl ($filename) from cache");
00078 } else {
00079 $s = null;
00080 $this->debug("$wsdl ($filename) not in cache");
00081 }
00082 $this->releaseMutex($filename);
00083 return (!is_null($s)) ? unserialize($s) : null;
00084 }
00085 return null;
00086 }
00087
00096 function obtainMutex($filename, $mode) {
00097 if (isset($this->fplock[md5($filename)])) {
00098 $this->debug("Lock for $filename already exists");
00099 return false;
00100 }
00101 $this->fplock[md5($filename)] = fopen($filename.".lock", "w");
00102 if ($mode == "r") {
00103 return flock($this->fplock[md5($filename)], LOCK_SH);
00104 } else {
00105 return flock($this->fplock[md5($filename)], LOCK_EX);
00106 }
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116 function put($wsdl_instance) {
00117 $filename = $this->createFilename($wsdl_instance->wsdl);
00118 $s = serialize($wsdl_instance);
00119 if ($this->obtainMutex($filename, "w")) {
00120 $fp = fopen($filename, "w");
00121 fputs($fp, $s);
00122 fclose($fp);
00123 $this->debug("Put $wsdl_instance->wsdl ($filename) in cache");
00124 $this->releaseMutex($filename);
00125 return true;
00126 }
00127 return false;
00128 }
00129
00137 function releaseMutex($filename) {
00138 $ret = flock($this->fplock[md5($filename)], LOCK_UN);
00139 fclose($this->fplock[md5($filename)]);
00140 unset($this->fplock[md5($filename)]);
00141 return $ret;
00142 }
00143
00151 function remove($wsdl) {
00152 $filename = $this->createFilename($wsdl);
00153 $this->obtainMutex($filename, "w");
00154 $ret = unlink($filename);
00155 $this->debug("Removed $wsdl ($filename) from cache");
00156 $this->releaseMutex($filename);
00157 return $ret;
00158 }
00159 }
00160 ?>