ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilGlobalCacheDBLayer.php
Go to the documentation of this file.
1<?php
2require_once('./Services/GlobalCache/classes/class.ilGlobalCache.php');
3
11
15 protected $component = '';
19 protected $loaded = false;
23 protected $table_name = '';
27 protected static $instances = array();
31 protected $raw_data = array();
35 protected $cached_results = array();
39 protected $ttl = NULL;
43 protected $global_cache;
47 protected $wheres = array();
48
49
55 protected function __construct($component, $table_name, $ttl = NULL) {
56 $this->setTtl($ttl);
59 $this->global_cache = ilGlobalCache::getInstance($component);
60 $this->readFromCache();
61 if (! $this->getLoaded()) {
62 $this->readFromDB();
63 $this->writeToCache();
64 $this->setLoaded(true);
65 }
66 }
67
68
69 protected function readFromCache() {
70 if ($this->global_cache->isActive()) {
71 $data = $this->global_cache->get($this->getTableName() . '_raw_data');
72 $cached_results = $this->global_cache->get($this->getTableName() . '_cached_results');
73 if (is_array($data)) {
74 $this->setRawData($data);
76 $this->setLoaded(true);
77 }
78 }
79 }
80
81
82 protected function writeToCache() {
83 if ($this->global_cache->isActive()) {
84 $this->global_cache->set($this->getTableName() . '_raw_data', $this->getRawData(), $this->getTtl());
85 $this->updateCachedResults();
86 }
87 }
88
89
90 protected function readFromDB() {
91 global $ilDB;
95 $q = 'SELECT * FROM ' . $this->getTableName();
96 $res = $ilDB->query($q);
97 $raw_data = array();
98 while ($set = $ilDB->fetchObject($res)) {
99 $raw_data[] = $set;
100 }
101 $this->setRawData($raw_data);
102 }
103
104
112 public function getWhere($field, $value, $case_sensitive = true) {
113 return $this->filter($this->getRawData(), $field, $value, $case_sensitive);
114 }
115
116
128 public function filter(array $data, $field, $value, $case_sensitive = true, $strip = true) {
129 if (is_array($value)) {
130 $index = md5(serialize($value)) . $case_sensitive;
131 } else {
132 $index = $value . $case_sensitive;
133 }
134 if (isset($this->cached_results[$this->getTableName()][$field][$index])) {
135 return $this->cached_results[$this->getTableName()][$field][$index];
136 }
137 $result = array();
138 foreach ($data as $dat) {
139 if ($case_sensitive) {
140 if (is_array($value)) {
141 if (in_array($dat->{$field}, $value)) {
142 $result[] = $dat;
143 }
144 } elseif ($dat->{$field} == $value) {
145
146 $result[] = $dat;
147 }
148 } else {
149 if (is_array($value)) {
150 if (preg_grep("/" . $dat->{$field} . "/i", $value)) {
151 $result[] = $dat;
152 }
153 } elseif (strcasecmp($dat->{$field}, $value) == 0) {
154 $result[] = $dat;
155 }
156 }
157 }
158 if (count($result) == 1 AND $strip) {
159 $result = $result[0];
160 }
161
162 $this->cached_results[$this->getTableName()][$field][$index] = $result;
163 $this->updateCachedResults();
164
165 return $result;
166 }
167
168
169 protected function updateCachedResults() {
170 $this->global_cache->set($this->getTableName() . '_cached_results', $this->getCachedResults());
171 }
172
173
181 public function where($field, $value, $case_sensitive = true) {
182 $ilGcDbWhere = new ilGcDbWhere();
183 $ilGcDbWhere->setField($field);
184 $ilGcDbWhere->setValue($value);
185 $ilGcDbWhere->setCaseSensitive($case_sensitive);
186 $this->wheres[] = $ilGcDbWhere;
187
188 return $this;
189 }
190
191
197 public function get($strip = true) {
198 $result = $this->getRawData();
199 foreach ($this->wheres as $ilGcDbWhere) {
200 $result = $this->filter($result, $ilGcDbWhere->getField(), $ilGcDbWhere->getValue(), $ilGcDbWhere->getCaseSensitive(), false);
201 }
202
203 if (count($result) == 1 AND $strip) {
204 // echo '<pre>' . print_r($result, 1) . '</pre>';
205 $result = $result[0];
206 }
207
208 return $result;
209 }
210
211
218 public static function getInstance($component, $table_name) {
219 if (! isset(self::$instances[$component . $table_name])) {
220 self::$instances[$component . $table_name] = new self($component, $table_name);
221 }
222
223 return self::$instances[$component . $table_name];
224 }
225
226
230 public function setCachedResults($cached_results) {
231 $this->cached_results = $cached_results;
232 }
233
234
238 public function getCachedResults() {
239 return $this->cached_results;
240 }
241
242
246 public function setRawData($raw_data) {
247 $this->raw_data = $raw_data;
248 }
249
250
254 public function getRawData() {
255 return $this->raw_data;
256 }
257
258
262 public function setTableName($table_name) {
263 $this->table_name = $table_name;
264 }
265
266
270 public function getTableName() {
271 return $this->table_name;
272 }
273
274
278 public function setLoaded($loaded) {
279 $this->loaded = $loaded;
280 }
281
282
286 public function getLoaded() {
287 return $this->loaded;
288 }
289
290
294 public function setComponent($component) {
295 $this->component = $component;
296 }
297
298
302 public function getComponent() {
303 return $this->component;
304 }
305
306
310 public function setTtl($ttl) {
311 $this->ttl = $ttl;
312 }
313
314
318 public function getTtl() {
319 return $this->ttl;
320 }
321}
322
324
328 protected $field = '';
332 protected $value;
336 protected $case_sensitive = true;
337
338
343 $this->case_sensitive = $case_sensitive;
344 }
345
346
350 public function getCaseSensitive() {
352 }
353
354
358 public function setField($field) {
359 $this->field = $field;
360 }
361
362
366 public function getField() {
367 return $this->field;
368 }
369
370
374 public function setValue($value) {
375 $this->value = $value;
376 }
377
378
382 public function getValue() {
383 return $this->value;
384 }
385}
386
387
388?>
$result
setCaseSensitive($case_sensitive)
Class ilGlobalCacheDBLayer.
filter(array $data, $field, $value, $case_sensitive=true, $strip=true)
static getInstance($component, $table_name)
getWhere($field, $value, $case_sensitive=true)
__construct($component, $table_name, $ttl=NULL)
where($field, $value, $case_sensitive=true)
static getInstance($component)
$data
global $ilDB