ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
class.ilSessionIStorage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
40{
41 private string $session_id;
42 private string $component_id;
43 private static array $values = [];
44
45 public function __construct(string $a_component_id, string $a_sess_id = '')
46 {
47 $this->component_id = $a_component_id;
48 if ($a_sess_id !== '') {
49 $this->session_id = $a_sess_id;
50 } else {
51 $this->session_id = session_id();
52 }
53 }
54
55 private function initComponentCacheIfNotExists(): void
56 {
57 if (!isset(self::$values[$this->component_id]) || !is_array(self::$values[$this->component_id])) {
58 self::$values[$this->component_id] = [];
59 }
60 }
61
62 public function set(string $a_key, string $a_val): void
63 {
64 global $DIC;
65
66 $ilDB = $DIC['ilDB'];
67
69
70 self::$values[$this->component_id][$a_key] = $a_val;
71 $ilDB->replace(
72 'usr_sess_istorage',
73 [
74 'session_id' => ['text', $this->session_id],
75 'component_id' => ['text', $this->component_id],
76 'vkey' => ['text', $a_key]
77 ],
78 ['value' => ['text', $a_val]]
79 );
80 }
81
82 public function get(string $a_key): string
83 {
84 global $DIC;
85
86 $ilDB = $DIC['ilDB'];
87
88 if (isset(self::$values[$this->component_id][$a_key]) && is_array(self::$values[$this->component_id])) {
89 return self::$values[$this->component_id][$a_key];
90 }
91
92 $set = $ilDB->query(
93 'SELECT value FROM usr_sess_istorage ' .
94 ' WHERE session_id = ' . $ilDB->quote($this->session_id, 'text') .
95 ' AND component_id = ' . $ilDB->quote($this->component_id, 'text') .
96 ' AND vkey = ' . $ilDB->quote($a_key, 'text')
97 );
98 $rec = $ilDB->fetchAssoc($set);
99 $value = (string) ($rec['value'] ?? '');
100
102
103 self::$values[$this->component_id][$a_key] = $value;
104
105 return $value;
106 }
107
112 public static function destroySession($a_session_id): void
113 {
114 global $DIC;
115
116 if (is_array($a_session_id)) {
117 $chunk_size = 500;
118 $batches = array_chunk($a_session_id, $chunk_size);
119 foreach ($batches as $batch) {
120 $q = 'DELETE FROM usr_sess_istorage WHERE ' .
121 $DIC->database()->in('session_id', $batch, false, ilDBConstants::T_TEXT);
122 $DIC->database()->manipulate($q);
123 }
124 } else {
125 $q = 'DELETE FROM usr_sess_istorage WHERE session_id = ' .
126 $DIC->database()->quote($a_session_id, ilDBConstants::T_TEXT);
127 $DIC->database()->manipulate($q);
128 }
129 }
130}
Session based immediate storage.
static destroySession($a_session_id)
Destroy session(s).
__construct(string $a_component_id, string $a_sess_id='')
global $DIC
Definition: shib_login.php:26
$q
Definition: shib_logout.php:25