| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
class wsdlcache { |
|---|
| 14 |
|
|---|
| 15 |
* @var resource |
|---|
| 16 |
* @access private |
|---|
| 17 |
*/ |
|---|
| 18 |
var $fplock; |
|---|
| 19 |
|
|---|
| 20 |
* @var integer |
|---|
| 21 |
* @access private |
|---|
| 22 |
*/ |
|---|
| 23 |
var $cache_lifetime; |
|---|
| 24 |
|
|---|
| 25 |
* @var string |
|---|
| 26 |
* @access private |
|---|
| 27 |
*/ |
|---|
| 28 |
var $cache_dir; |
|---|
| 29 |
|
|---|
| 30 |
* @var string |
|---|
| 31 |
* @access public |
|---|
| 32 |
*/ |
|---|
| 33 |
var $debug_str = ''; |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
* constructor |
|---|
| 37 |
* |
|---|
| 38 |
* @param string $cache_dir directory for cache-files |
|---|
| 39 |
* @param integer $cache_lifetime lifetime for caching-files in seconds or 0 for unlimited |
|---|
| 40 |
* @access public |
|---|
| 41 |
*/ |
|---|
| 42 |
function wsdlcache($cache_dir='.', $cache_lifetime=0) { |
|---|
| 43 |
$this->fplock = array(); |
|---|
| 44 |
$this->cache_dir = $cache_dir != '' ? $cache_dir : '.'; |
|---|
| 45 |
$this->cache_lifetime = $cache_lifetime; |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
* creates the filename used to cache a wsdl instance |
|---|
| 50 |
* |
|---|
| 51 |
* @param string $wsdl The URL of the wsdl instance |
|---|
| 52 |
* @return string The filename used to cache the instance |
|---|
| 53 |
* @access private |
|---|
| 54 |
*/ |
|---|
| 55 |
function createFilename($wsdl) { |
|---|
| 56 |
return $this->cache_dir.'/wsdlcache-' . md5($wsdl); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
* adds debug data to the class level debug string |
|---|
| 61 |
* |
|---|
| 62 |
* @param string $string debug data |
|---|
| 63 |
* @access private |
|---|
| 64 |
*/ |
|---|
| 65 |
function debug($string){ |
|---|
| 66 |
$this->debug_str .= get_class($this).": $string\n"; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
* gets a wsdl instance from the cache |
|---|
| 71 |
* |
|---|
| 72 |
* @param string $wsdl The URL of the wsdl instance |
|---|
| 73 |
* @return object wsdl The cached wsdl instance, null if the instance is not in the cache |
|---|
| 74 |
* @access public |
|---|
| 75 |
*/ |
|---|
| 76 |
function get($wsdl) { |
|---|
| 77 |
$filename = $this->createFilename($wsdl); |
|---|
| 78 |
if ($this->obtainMutex($filename, "r")) { |
|---|
| 79 |
|
|---|
| 80 |
if ($this->cache_lifetime > 0) { |
|---|
| 81 |
if (file_exists($filename) && (time() - filemtime($filename) > $this->cache_lifetime)) { |
|---|
| 82 |
unlink($filename); |
|---|
| 83 |
$this->debug("Expired $wsdl ($filename) from cache"); |
|---|
| 84 |
$this->releaseMutex($filename); |
|---|
| 85 |
return null; |
|---|
| 86 |
} |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
$fp = @fopen($filename, "r"); |
|---|
| 90 |
if ($fp) { |
|---|
| 91 |
$s = implode("", @file($filename)); |
|---|
| 92 |
fclose($fp); |
|---|
| 93 |
$this->debug("Got $wsdl ($filename) from cache"); |
|---|
| 94 |
} else { |
|---|
| 95 |
$s = null; |
|---|
| 96 |
$this->debug("$wsdl ($filename) not in cache"); |
|---|
| 97 |
} |
|---|
| 98 |
$this->releaseMutex($filename); |
|---|
| 99 |
return (!is_null($s)) ? unserialize($s) : null; |
|---|
| 100 |
} else { |
|---|
| 101 |
$this->debug("Unable to obtain mutex for $filename in get"); |
|---|
| 102 |
} |
|---|
| 103 |
return null; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
* obtains the local mutex |
|---|
| 108 |
* |
|---|
| 109 |
* @param string $filename The Filename of the Cache to lock |
|---|
| 110 |
* @param string $mode The open-mode ("r" or "w") or the file - affects lock-mode |
|---|
| 111 |
* @return boolean Lock successfully obtained ?! |
|---|
| 112 |
* @access private |
|---|
| 113 |
*/ |
|---|
| 114 |
function obtainMutex($filename, $mode) { |
|---|
| 115 |
if (isset($this->fplock[md5($filename)])) { |
|---|
| 116 |
$this->debug("Lock for $filename already exists"); |
|---|
| 117 |
return false; |
|---|
| 118 |
} |
|---|
| 119 |
$this->fplock[md5($filename)] = fopen($filename.".lock", "w"); |
|---|
| 120 |
if ($mode == "r") { |
|---|
| 121 |
return flock($this->fplock[md5($filename)], LOCK_SH); |
|---|
| 122 |
} else { |
|---|
| 123 |
return flock($this->fplock[md5($filename)], LOCK_EX); |
|---|
| 124 |
} |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
* adds a wsdl instance to the cache |
|---|
| 129 |
* |
|---|
| 130 |
* @param object wsdl $wsdl_instance The wsdl instance to add |
|---|
| 131 |
* @return boolean WSDL successfully cached |
|---|
| 132 |
* @access public |
|---|
| 133 |
*/ |
|---|
| 134 |
function put($wsdl_instance) { |
|---|
| 135 |
$filename = $this->createFilename($wsdl_instance->wsdl); |
|---|
| 136 |
$s = serialize($wsdl_instance); |
|---|
| 137 |
if ($this->obtainMutex($filename, "w")) { |
|---|
| 138 |
$fp = fopen($filename, "w"); |
|---|
| 139 |
fputs($fp, $s); |
|---|
| 140 |
fclose($fp); |
|---|
| 141 |
$this->debug("Put $wsdl_instance->wsdl ($filename) in cache"); |
|---|
| 142 |
$this->releaseMutex($filename); |
|---|
| 143 |
return true; |
|---|
| 144 |
} else { |
|---|
| 145 |
$this->debug("Unable to obtain mutex for $filename in put"); |
|---|
| 146 |
} |
|---|
| 147 |
return false; |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
* releases the local mutex |
|---|
| 152 |
* |
|---|
| 153 |
* @param string $filename The Filename of the Cache to lock |
|---|
| 154 |
* @return boolean Lock successfully released |
|---|
| 155 |
* @access private |
|---|
| 156 |
*/ |
|---|
| 157 |
function releaseMutex($filename) { |
|---|
| 158 |
$ret = flock($this->fplock[md5($filename)], LOCK_UN); |
|---|
| 159 |
fclose($this->fplock[md5($filename)]); |
|---|
| 160 |
unset($this->fplock[md5($filename)]); |
|---|
| 161 |
if (! $ret) { |
|---|
| 162 |
$this->debug("Not able to release lock for $filename"); |
|---|
| 163 |
} |
|---|
| 164 |
return $ret; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
* removes a wsdl instance from the cache |
|---|
| 169 |
* |
|---|
| 170 |
* @param string $wsdl The URL of the wsdl instance |
|---|
| 171 |
* @return boolean Whether there was an instance to remove |
|---|
| 172 |
* @access public |
|---|
| 173 |
*/ |
|---|
| 174 |
function remove($wsdl) { |
|---|
| 175 |
$filename = $this->createFilename($wsdl); |
|---|
| 176 |
|
|---|
| 177 |
$this->obtainMutex($filename, "w"); |
|---|
| 178 |
$ret = unlink($filename); |
|---|
| 179 |
$this->debug("Removed ($ret) $wsdl ($filename) from cache"); |
|---|
| 180 |
$this->releaseMutex($filename); |
|---|
| 181 |
return $ret; |
|---|
| 182 |
} |
|---|
| 183 |
} |
|---|
| 184 |
?> |
|---|
| 185 |
|
|---|