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