ILIAS  release_8 Revision v8.24
class.ilCache.php
Go to the documentation of this file.
1<?php
2
29{
30 protected string $entry;
31 protected string $last_access;
32 protected int $expires_after;
33 protected bool $use_long_content;
34 protected string $name;
35 protected string $component;
36
37 public function __construct(
38 string $a_component,
39 string $a_cache_name,
40 bool $a_use_long_content = false
41 ) {
42 $this->setComponent($a_component);
43 $this->setName($a_cache_name);
44 $this->setUseLongContent($a_use_long_content);
45 }
46
51 public function isDisabled(): bool
52 {
53 return ilMemberViewSettings::getInstance()->isActive();
54 }
55
56 public function setComponent(string $a_val): void
57 {
58 $this->component = $a_val;
59 }
60
61 protected function getComponent(): string
62 {
63 return $this->component;
64 }
65
66 protected function setName(string $a_val): void
67 {
68 $this->name = $a_val;
69 }
70
71 protected function getName(): string
72 {
73 return $this->name;
74 }
75
76 protected function setUseLongContent(bool $a_val): void
77 {
78 $this->use_long_content = $a_val;
79 }
80
81 protected function getUseLongContent(): bool
82 {
84 }
85
89 public function setExpiresAfter(int $a_val): void
90 {
91 $this->expires_after = $a_val;
92 }
93
94 public function getExpiresAfter(): int
95 {
97 }
98
99 final public function getEntry(string $a_id): ?string
100 {
101 if ($this->readEntry($a_id)) { // cache hit
102 $this->last_access = "hit";
103 return $this->entry;
104 }
105 $this->last_access = "miss";
106 return null;
107 }
108
109 protected function readEntry(string $a_id): bool
110 {
111 global $ilDB;
112
113 $table = $this->getUseLongContent()
114 ? "cache_clob"
115 : "cache_text";
116
117 $query = "SELECT value FROM $table WHERE " .
118 "component = " . $ilDB->quote($this->getComponent(), "text") . " AND " .
119 "name = " . $ilDB->quote($this->getName(), "text") . " AND " .
120 "expire_time > " . $ilDB->quote(time(), "integer") . " AND " .
121 "ilias_version = " . $ilDB->quote(ILIAS_VERSION_NUMERIC, "text") . " AND " .
122 "entry_id = " . $ilDB->quote($a_id, "text");
123
124 $set = $ilDB->query($query);
125
126 if ($rec = $ilDB->fetchAssoc($set)) {
127 $this->entry = $rec["value"];
128 return true;
129 }
130
131 return false;
132 }
133
134 public function getLastAccessStatus(): string
135 {
136 return $this->last_access;
137 }
138
139 public function storeEntry(
140 string $a_id,
141 string $a_value,
142 ?int $a_int_key1 = null,
143 ?int $a_int_key2 = null,
144 ?string $a_text_key1 = null,
145 ?string $a_text_key2 = null
146 ): void {
147 global $ilDB;
148
149 $table = $this->getUseLongContent()
150 ? "cache_clob"
151 : "cache_text";
152 $type = $this->getUseLongContent()
153 ? "clob"
154 : "text";
155
156 // do not store values, that do not fit into the text field
157 if (strlen($a_value) > 4000 && $type == "text") {
158 return;
159 }
160
161 $set = $ilDB->replace($table, array(
162 "component" => array("text", $this->getComponent()),
163 "name" => array("text", $this->getName()),
164 "entry_id" => array("text", $a_id)
165 ), array(
166 "value" => array($type, $a_value),
167 "int_key_1" => array("integer", $a_int_key1),
168 "int_key_2" => array("integer", $a_int_key2),
169 "text_key_1" => array("text", $a_text_key1),
170 "text_key_2" => array("text", $a_text_key2),
171 "expire_time" => array("integer", (time() + $this->getExpiresAfter())),
172 "ilias_version" => array("text", ILIAS_VERSION_NUMERIC)
173 ));
174
175 // In 1/2000 times, delete old entries
176 $random = new \ilRandom();
177 $num = $random->int(1, 2000);
178 if ($num == 500) {
179 $ilDB->manipulate(
180 "DELETE FROM $table WHERE " .
181 " ilias_version <> " . $ilDB->quote(ILIAS_VERSION_NUMERIC, "text") .
182 " OR expire_time < " . $ilDB->quote(time(), "integer")
183 );
184 }
185 }
186
187 public function deleteByAdditionalKeys(
188 ?int $a_int_key1 = null,
189 ?int $a_int_key2 = null,
190 ?string $a_text_key1 = null,
191 ?string $a_text_key2 = null
192 ): void {
193 global $ilDB;
194
195 $table = $this->getUseLongContent()
196 ? "cache_clob"
197 : "cache_text";
198
199 $q = "DELETE FROM $table WHERE " .
200 "component = " . $ilDB->quote($this->getComponent(), "text") .
201 " AND name = " . $ilDB->quote($this->getName(), "text");
202
203 $fds = array("int_key_1" => array("v" => $a_int_key1, "t" => "integer"),
204 "int_key_2" => array("v" => $a_int_key2, "t" => "integer"),
205 "text_key_1" => array("v" => $a_text_key1, "t" => "text"),
206 "text_key_2" => array("v" => $a_text_key2, "t" => "text"));
207 $sep = " AND";
208 foreach ($fds as $k => $fd) {
209 if (!is_null($fd["v"])) {
210 $q .= $sep . " " . $k . " = " . $ilDB->quote($fd["v"], $fd["t"]);
211 $set = " AND";
212 }
213 }
214 $ilDB->manipulate($q);
215 }
216
217 public function deleteAllEntries(): void
218 {
219 global $ilDB;
220
221 $table = $this->getUseLongContent()
222 ? "cache_clob"
223 : "cache_text";
224
225 $q = "DELETE FROM $table WHERE " .
226 "component = " . $ilDB->quote($this->getComponent(), "text") .
227 " AND name = " . $ilDB->quote($this->getName(), "text");
228 $ilDB->manipulate($q);
229 }
230
231 public function deleteEntry(string $a_id): void
232 {
233 global $ilDB;
234
235 $table = $this->getUseLongContent()
236 ? "cache_clob"
237 : "cache_text";
238
239 $ilDB->manipulate("DELETE FROM " . $table . " WHERE "
240 . " entry_id = " . $ilDB->quote($a_id, "text")
241 . " AND component = " . $ilDB->quote($this->getComponent(), "text") .
242 " AND name = " . $ilDB->quote($this->getName(), "text"));
243 }
244}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getEntry(string $a_id)
getUseLongContent()
setName(string $a_val)
setComponent(string $a_val)
string $component
storeEntry(string $a_id, string $a_value, ?int $a_int_key1=null, ?int $a_int_key2=null, ?string $a_text_key1=null, ?string $a_text_key2=null)
isDisabled()
Check if cache is disabled Forced if member view is active.
deleteByAdditionalKeys(?int $a_int_key1=null, ?int $a_int_key2=null, ?string $a_text_key1=null, ?string $a_text_key2=null)
setUseLongContent(bool $a_val)
setExpiresAfter(int $a_val)
Set expires after x seconds.
readEntry(string $a_id)
getLastAccessStatus()
string $name
bool $use_long_content
string $last_access
getExpiresAfter()
string $entry
__construct(string $a_component, string $a_cache_name, bool $a_use_long_content=false)
int $expires_after
deleteEntry(string $a_id)
deleteAllEntries()
const ILIAS_VERSION_NUMERIC
$query
$type