ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCache.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
18 class ilCache
19 {
26  function __construct($a_component, $a_cache_name, $a_use_long_content = false)
27  {
28  $this->setComponent($a_component);
29  $this->setName($a_cache_name);
30  $this->setUseLongContent($a_use_long_content);
31  }
32 
38  public function isDisabled()
39  {
40  include_once './Services/Container/classes/class.ilMemberViewSettings.php';
41  return ilMemberViewSettings::getInstance()->isActive();
42  }
43 
49  function setComponent($a_val)
50  {
51  $this->component = $a_val;
52  }
53 
59  protected function getComponent()
60  {
61  return $this->component;
62  }
63 
69  protected function setName($a_val)
70  {
71  $this->name = $a_val;
72  }
73 
79  protected function getName()
80  {
81  return $this->name;
82  }
83 
89  protected function setUseLongContent($a_val)
90  {
91  $this->use_long_content = $a_val;
92  }
93 
99  protected function getUseLongContent()
100  {
101  return $this->use_long_content;
102  }
103 
109  public function setExpiresAfter($a_val)
110  {
111  $this->expires_after = $a_val;
112  }
113 
119  public function getExpiresAfter()
120  {
121  return $this->expires_after;
122  }
123 
130  final public function getEntry($a_id)
131  {
132  if ($this->readEntry($a_id)) // cache hit
133  {
134  $this->last_access = "hit";
135  return $this->entry;
136  }
137  $this->last_access = "miss";
138  }
139 
146  protected function readEntry($a_id)
147  {
148  global $ilDB;
149 
150  $table = $this->getUseLongContent()
151  ? "cache_clob"
152  : "cache_text";
153 
154  $query = "SELECT value FROM $table WHERE ".
155  "component = ".$ilDB->quote($this->getComponent(), "text")." AND ".
156  "name = ".$ilDB->quote($this->getName(), "text")." AND ".
157  "expire_time > ".$ilDB->quote(time(), "integer")." AND ".
158  "ilias_version = ".$ilDB->quote(ILIAS_VERSION_NUMERIC, "text")." AND ".
159  "entry_id = ".$ilDB->quote($a_id, "text");
160 
161  $set = $ilDB->query($query);
162 
163  if ($rec = $ilDB->fetchAssoc($set))
164  {
165  $this->entry = $rec["value"];
166  return true;
167  }
168 
169  return false;
170  }
171 
176  {
177  return $this->last_access;
178  }
179 
180 
192  function storeEntry($a_id, $a_value, $a_int_key1 = null, $a_int_key2 = null,
193  $a_text_key1 = null, $a_text_key2 = null)
194  {
195  global $ilDB;
196 
197  $table = $this->getUseLongContent()
198  ? "cache_clob"
199  : "cache_text";
200  $type = $this->getUseLongContent()
201  ? "clob"
202  : "text";
203 
204  // do not store values, that do not fit into the text field
205  if (strlen($a_value) > 4000 && $type == "text")
206  {
207  return;
208  }
209 
210  $set = $ilDB->replace($table, array(
211  "component" => array("text", $this->getComponent()),
212  "name" => array("text", $this->getName()),
213  "entry_id" => array("text", $a_id)
214  ), array (
215  "value" => array($type, $a_value),
216  "int_key_1" => array("integer", $a_int_key1),
217  "int_key_2" => array("integer", $a_int_key2),
218  "text_key_1" => array("text", $a_text_key1),
219  "text_key_2" => array("text", $a_text_key2),
220  "expire_time" => array("integer", (int) (time() + $this->getExpiresAfter())),
221  "ilias_version" => array("text", ILIAS_VERSION_NUMERIC)
222  ));
223 
224  // In 1/2000 times, delete old entries
225  $num = rand(1,2000);
226  if ($num == 500)
227  {
228  $ilDB->manipulate("DELETE FROM $table WHERE ".
229  " ilias_version <> ".$ilDB->quote(ILIAS_VERSION_NUMERIC, "text").
230  " OR expire_time < ".$ilDB->quote(time(), "integer")
231  );
232  }
233 
234  }
235 
239  public function deleteByAdditionalKeys($a_int_key1 = null, $a_int_key2 = null,
240  $a_text_key1 = null, $a_text_key2 = null)
241  {
242  global $ilDB;
243 
244  $table = $this->getUseLongContent()
245  ? "cache_clob"
246  : "cache_text";
247 
248  $q = "DELETE FROM $table WHERE ".
249  "component = ".$ilDB->quote($this->getComponent(), "text").
250  " AND name = ".$ilDB->quote($this->getName(), "text");
251 
252  $fds = array("int_key_1" => array("v" => $a_int_key1, "t" => "integer"),
253  "int_key_2" => array("v" => $a_int_key2, "t" => "integer"),
254  "text_key_1" => array("v" => $a_text_key1, "t" => "text"),
255  "text_key_2" => array("v" => $a_text_key2, "t" => "text"));
256  $sep = " AND";
257  foreach ($fds as $k => $fd)
258  {
259  if (!is_null($fd["v"]))
260  {
261  $q .= $sep." ".$k." = ".$ilDB->quote($fd["v"], $fd["t"]);
262  $set = " AND";
263  }
264  }
265  $ilDB->manipulate($q);
266  }
267 
271  public function deleteAllEntries()
272  {
273  global $ilDB;
274 
275  $table = $this->getUseLongContent()
276  ? "cache_clob"
277  : "cache_text";
278 
279  $q = "DELETE FROM $table WHERE ".
280  "component = ".$ilDB->quote($this->getComponent(), "text").
281  " AND name = ".$ilDB->quote($this->getName(), "text");
282  $ilDB->manipulate($q);
283  }
284 
290  function deleteEntry($a_id)
291  {
292  global $ilDB;
293 
294  $table = $this->getUseLongContent()
295  ? "cache_clob"
296  : "cache_text";
297 
298  $ilDB->manipulate("DELETE FROM ".$table." WHERE "
299  ." entry_id = ".$ilDB->quote($a_id, "text")
300  ." AND component = ".$ilDB->quote($this->getComponent(), "text").
301  " AND name = ".$ilDB->quote($this->getName(), "text"));
302  }
303 
304 }
305 ?>