nusoap_wsdlcache

Caches instances of the wsdl class.

Defined (1)

The class is defined in the following location(s).

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