ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilGlobalCacheQueryWrapper.php
Go to the documentation of this file.
1<?php
2require_once('./Services/GlobalCache/classes/class.ilGlobalCache.php');
3
13
16 const MODE_SINGLE = 1;
17 const MODE_MULTIPLE = 2;
21 protected $query = '';
25 protected $cache_key = '';
34
35
42 public function __construct($cache_key, $query, $fetch_type = self::FETCH_TYPE_OBJECT, $mode = self::MODE_SINGLE) {
43 $this->cache_key = $cache_key;
44 $this->fetch_type = $fetch_type;
45 $this->mode = $mode;
46 $this->query = $query;
47 }
48
49
53 public function get() {
54 $ilGlobalCache = ilGlobalCache::getInstance();
55 if ($ilGlobalCache->isActive()) {
56 $rec = $ilGlobalCache->get($this->cache_key);
57 if (!$rec) {
58 $rec = $this->getFromDb();
59 $ilGlobalCache->set($this->cache_key, $rec, 600);
60 }
61 } else {
62 $rec = $this->getFromDb();
63 }
64
65 return $rec;
66 }
67
68
72 protected function getFromDb() {
73 global $DIC;
74 $ilDB = $DIC['ilDB'];
78 $return = array();
79 $res = $ilDB->query($this->query);
80 switch ($this->getFetchType()) {
82 while ($rec = $ilDB->fetchObject($res)) {
83 $return[] = $rec;
84 }
85 break;
87 while ($rec = $ilDB->fetchAssoc($res)) {
88 $return[] = $rec;
89 }
90 break;
91 }
92
93 if ($this->getMode() == self::MODE_SINGLE) {
94 return $return[0];
95 } else {
96 return $return;
97 }
98 }
99
100
104 public function setFetchType($fetch_type) {
105 $this->fetch_type = $fetch_type;
106 }
107
108
112 public function getFetchType() {
113 return $this->fetch_type;
114 }
115
116
120 public function setMode($mode) {
121 $this->mode = $mode;
122 }
123
124
128 public function getMode() {
129 return $this->mode;
130 }
131}
132
133?>
An exception for terminatinating execution or to throw for unit testing.
Class ilGlobalCacheQueryWrapper.
__construct($cache_key, $query, $fetch_type=self::FETCH_TYPE_OBJECT, $mode=self::MODE_SINGLE)
static getInstance($component)
global $ilDB
global $DIC