ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  public 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  public 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  $this->last_access = "hit";
134  return $this->entry;
135  }
136  $this->last_access = "miss";
137  }
138 
145  protected function readEntry($a_id)
146  {
147  global $ilDB;
148 
149  $table = $this->getUseLongContent()
150  ? "cache_clob"
151  : "cache_text";
152 
153  $query = "SELECT value FROM $table WHERE " .
154  "component = " . $ilDB->quote($this->getComponent(), "text") . " AND " .
155  "name = " . $ilDB->quote($this->getName(), "text") . " AND " .
156  "expire_time > " . $ilDB->quote(time(), "integer") . " AND " .
157  "ilias_version = " . $ilDB->quote(ILIAS_VERSION_NUMERIC, "text") . " AND " .
158  "entry_id = " . $ilDB->quote($a_id, "text");
159 
160  $set = $ilDB->query($query);
161 
162  if ($rec = $ilDB->fetchAssoc($set)) {
163  $this->entry = $rec["value"];
164  return true;
165  }
166 
167  return false;
168  }
169 
173  public function getLastAccessStatus()
174  {
175  return $this->last_access;
176  }
177 
178 
190  public function storeEntry(
191  $a_id,
192  $a_value,
193  $a_int_key1 = null,
194  $a_int_key2 = null,
195  $a_text_key1 = null,
196  $a_text_key2 = null
197  ) {
198  global $ilDB;
199 
200  $table = $this->getUseLongContent()
201  ? "cache_clob"
202  : "cache_text";
203  $type = $this->getUseLongContent()
204  ? "clob"
205  : "text";
206 
207  // do not store values, that do not fit into the text field
208  if (strlen($a_value) > 4000 && $type == "text") {
209  return;
210  }
211 
212  $set = $ilDB->replace($table, array(
213  "component" => array("text", $this->getComponent()),
214  "name" => array("text", $this->getName()),
215  "entry_id" => array("text", $a_id)
216  ), array(
217  "value" => array($type, $a_value),
218  "int_key_1" => array("integer", $a_int_key1),
219  "int_key_2" => array("integer", $a_int_key2),
220  "text_key_1" => array("text", $a_text_key1),
221  "text_key_2" => array("text", $a_text_key2),
222  "expire_time" => array("integer", (int) (time() + $this->getExpiresAfter())),
223  "ilias_version" => array("text", ILIAS_VERSION_NUMERIC)
224  ));
225 
226  // In 1/2000 times, delete old entries
227  $random = new \ilRandom();
228  $num = $random->int(1, 2000);
229  if ($num == 500) {
230  $ilDB->manipulate(
231  "DELETE FROM $table WHERE " .
232  " ilias_version <> " . $ilDB->quote(ILIAS_VERSION_NUMERIC, "text") .
233  " OR expire_time < " . $ilDB->quote(time(), "integer")
234  );
235  }
236  }
237 
241  public function deleteByAdditionalKeys(
242  $a_int_key1 = null,
243  $a_int_key2 = null,
244  $a_text_key1 = null,
245  $a_text_key2 = null
246  ) {
247  global $ilDB;
248 
249  $table = $this->getUseLongContent()
250  ? "cache_clob"
251  : "cache_text";
252 
253  $q = "DELETE FROM $table WHERE " .
254  "component = " . $ilDB->quote($this->getComponent(), "text") .
255  " AND name = " . $ilDB->quote($this->getName(), "text");
256 
257  $fds = array("int_key_1" => array("v" => $a_int_key1, "t" => "integer"),
258  "int_key_2" => array("v" => $a_int_key2, "t" => "integer"),
259  "text_key_1" => array("v" => $a_text_key1, "t" => "text"),
260  "text_key_2" => array("v" => $a_text_key2, "t" => "text"));
261  $sep = " AND";
262  foreach ($fds as $k => $fd) {
263  if (!is_null($fd["v"])) {
264  $q .= $sep . " " . $k . " = " . $ilDB->quote($fd["v"], $fd["t"]);
265  $set = " AND";
266  }
267  }
268  $ilDB->manipulate($q);
269  }
270 
274  public function deleteAllEntries()
275  {
276  global $ilDB;
277 
278  $table = $this->getUseLongContent()
279  ? "cache_clob"
280  : "cache_text";
281 
282  $q = "DELETE FROM $table WHERE " .
283  "component = " . $ilDB->quote($this->getComponent(), "text") .
284  " AND name = " . $ilDB->quote($this->getName(), "text");
285  $ilDB->manipulate($q);
286  }
287 
293  public function deleteEntry($a_id)
294  {
295  global $ilDB;
296 
297  $table = $this->getUseLongContent()
298  ? "cache_clob"
299  : "cache_text";
300 
301  $ilDB->manipulate("DELETE FROM " . $table . " WHERE "
302  . " entry_id = " . $ilDB->quote($a_id, "text")
303  . " AND component = " . $ilDB->quote($this->getComponent(), "text") .
304  " AND name = " . $ilDB->quote($this->getName(), "text"));
305  }
306 }
getEntry($a_id)
Get entry.
deleteAllEntries()
Delete all entries of cache.
const ILIAS_VERSION_NUMERIC
$type
readEntry($a_id)
Read entry.
getComponent()
Get component.
isDisabled()
Check if cache is disabled Forced if member view is active.
deleteByAdditionalKeys( $a_int_key1=null, $a_int_key2=null, $a_text_key1=null, $a_text_key2=null)
Delete by additional keys.
storeEntry( $a_id, $a_value, $a_int_key1=null, $a_int_key2=null, $a_text_key1=null, $a_text_key2=null)
Store entry.
getLastAccessStatus()
Last access.
getName()
Get name.
$query
Cache class.
setComponent($a_val)
Set component.
static getInstance()
Get instance.
__construct($a_component, $a_cache_name, $a_use_long_content=false)
Constructor.
global $ilDB
setExpiresAfter($a_val)
Set expires after x seconds.
if(empty($password)) $table
Definition: pwgen.php:24
deleteEntry($a_id)
Delete entry.
setName($a_val)
Set name.
getUseLongContent()
Get use long content.
getExpiresAfter()
Get expires after x seconds.
setUseLongContent($a_val)
Set use long content.