123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- class nusoap_wsdlcache
- {
-
- var $fplock;
-
- var $cache_lifetime;
-
- var $cache_dir;
-
- var $debug_str = '';
-
- function __construct($cache_dir='.', $cache_lifetime=0) {
- $this->fplock = array();
- $this->cache_dir = $cache_dir != '' ? $cache_dir : '.';
- $this->cache_lifetime = $cache_lifetime;
- }
-
- function createFilename($wsdl) {
- return $this->cache_dir.'/wsdlcache-' . md5($wsdl);
- }
-
- function debug($string){
- $this->debug_str .= get_class($this).": $string\n";
- }
-
- function get($wsdl) {
- $filename = $this->createFilename($wsdl);
- if ($this->obtainMutex($filename, "r")) {
-
- if ($this->cache_lifetime > 0) {
- if (file_exists($filename) && (time() - filemtime($filename) > $this->cache_lifetime)) {
- unlink($filename);
- $this->debug("Expired $wsdl ($filename) from cache");
- $this->releaseMutex($filename);
- return null;
- }
- }
-
- if (!file_exists($filename)) {
- $this->debug("$wsdl ($filename) not in cache (1)");
- $this->releaseMutex($filename);
- return null;
- }
- $fp = @fopen($filename, "r");
- if ($fp) {
- $s = implode("", @file($filename));
- fclose($fp);
- $this->debug("Got $wsdl ($filename) from cache");
- } else {
- $s = null;
- $this->debug("$wsdl ($filename) not in cache (2)");
- }
- $this->releaseMutex($filename);
- return (!is_null($s)) ? unserialize($s) : null;
- } else {
- $this->debug("Unable to obtain mutex for $filename in get");
- }
- return null;
- }
-
- function obtainMutex($filename, $mode) {
- if (isset($this->fplock[md5($filename)])) {
- $this->debug("Lock for $filename already exists");
- return false;
- }
- $this->fplock[md5($filename)] = fopen($filename.".lock", "w");
- if ($mode == "r") {
- return flock($this->fplock[md5($filename)], LOCK_SH);
- } else {
- return flock($this->fplock[md5($filename)], LOCK_EX);
- }
- }
-
- function put($wsdl_instance) {
- $filename = $this->createFilename($wsdl_instance->wsdl);
- $s = serialize($wsdl_instance);
- if ($this->obtainMutex($filename, "w")) {
- $fp = fopen($filename, "w");
- if (! $fp) {
- $this->debug("Cannot write $wsdl_instance->wsdl ($filename) in cache");
- $this->releaseMutex($filename);
- return false;
- }
- fputs($fp, $s);
- fclose($fp);
- $this->debug("Put $wsdl_instance->wsdl ($filename) in cache");
- $this->releaseMutex($filename);
- return true;
- } else {
- $this->debug("Unable to obtain mutex for $filename in put");
- }
- return false;
- }
-
- function releaseMutex($filename) {
- $ret = flock($this->fplock[md5($filename)], LOCK_UN);
- fclose($this->fplock[md5($filename)]);
- unset($this->fplock[md5($filename)]);
- if (! $ret) {
- $this->debug("Not able to release lock for $filename");
- }
- return $ret;
- }
-
- function remove($wsdl) {
- $filename = $this->createFilename($wsdl);
- if (!file_exists($filename)) {
- $this->debug("$wsdl ($filename) not in cache to be removed");
- return false;
- }
-
- $this->obtainMutex($filename, "w");
- $ret = unlink($filename);
- $this->debug("Removed ($ret) $wsdl ($filename) from cache");
- $this->releaseMutex($filename);
- return $ret;
- }
- }
- class wsdlcache extends nusoap_wsdlcache {
- }
- ?>
|