ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilGlobalCacheQueryWrapper.php
Go to the documentation of this file.
1 <?php
2 require_once('./Services/GlobalCache/classes/class.ilGlobalCache.php');
3 
11 
12  const FETCH_TYPE_ASSOC = 1;
13  const FETCH_TYPE_OBJECT = 2;
14  const MODE_SINGLE = 1;
15  const MODE_MULTIPLE = 2;
19  protected $query = '';
23  protected $cache_key = '';
31  protected $mode = self::MODE_SINGLE;
32 
33 
40  public function __construct($cache_key, $query, $fetch_type = self::FETCH_TYPE_OBJECT, $mode = self::MODE_SINGLE) {
41  $this->cache_key = $cache_key;
42  $this->fetch_type = $fetch_type;
43  $this->mode = $mode;
44  $this->query = $query;
45  }
46 
47 
51  public function get() {
52  $ilGlobalCache = ilGlobalCache::getInstance();
53  if ($ilGlobalCache->isActive()) {
54  $rec = $ilGlobalCache->get($this->cache_key);
55  if (! $rec) {
56  $rec = $this->getFromDb();
57  $ilGlobalCache->set($this->cache_key, $rec, 600);
58  }
59  } else {
60  $rec = $this->getFromDb();
61  }
62 
63  return $rec;
64  }
65 
66 
70  protected function getFromDb() {
71  global $ilDB;
75  $return = array();
76  $res = $ilDB->query($this->query);
77  switch ($this->getFetchType()) {
78  case self::FETCH_TYPE_OBJECT:
79  while ($rec = $ilDB->fetchObject($res)) {
80  $return[] = $rec;
81  }
82  break;
83  case self::FETCH_TYPE_ASSOC:
84  while ($rec = $ilDB->fetchAssoc($res)) {
85  $return[] = $rec;
86  }
87  break;
88  }
89 
90  if ($this->getMode() == self::MODE_SINGLE) {
91  return $return[0];
92  } else {
93  return $return;
94  }
95  }
96 
97 
101  public function setFetchType($fetch_type) {
102  $this->fetch_type = $fetch_type;
103  }
104 
105 
109  public function getFetchType() {
110  return $this->fetch_type;
111  }
112 
113 
117  public function setMode($mode) {
118  $this->mode = $mode;
119  }
120 
121 
125  public function getMode() {
126  return $this->mode;
127  }
128 }
129 
130 ?>