ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.wsdlcache.php
Go to the documentation of this file.
1 <?php
2 
3 
4 
13 class wsdlcache {
18  var $fplock;
33  var $debug_str = '';
34 
43  $this->fplock = array();
44  $this->cache_dir = $cache_dir != '' ? $cache_dir : '.';
45  $this->cache_lifetime = $cache_lifetime;
46  }
47 
55  function createFilename($wsdl) {
56  return $this->cache_dir.'/wsdlcache-' . md5($wsdl);
57  }
58 
65  function debug($string){
66  $this->debug_str .= get_class($this).": $string\n";
67  }
68 
76  function get($wsdl) {
77  $filename = $this->createFilename($wsdl);
78  if ($this->obtainMutex($filename, "r")) {
79  // check for expired WSDL that must be removed from the cache
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  // see what there is to return
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 
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 
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 
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 
174  function remove($wsdl) {
175  $filename = $this->createFilename($wsdl);
176  // ignore errors obtaining mutex
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 ?>