Skip to content

Commit 72d1a41

Browse files
committed
Cache ids from mysql when fetching all objects
1 parent d0833aa commit 72d1a41

File tree

9 files changed

+144
-27
lines changed

9 files changed

+144
-27
lines changed

libraries/Document.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ public function __construct($data) {
5858
}
5959

6060
public static function getAllDocuments() {
61+
$cache_key = "bnetdocs-documents";
62+
$cache_val = Common::$cache->get($cache_key);
63+
if ($cache_val !== false) {
64+
$ids = explode(",", $cache_val);
65+
$objects = [];
66+
foreach ($ids as $id) {
67+
$objects[] = new self($id);
68+
}
69+
return $objects;
70+
}
6171
if (!isset(Common::$database)) {
6272
Common::$database = DatabaseDriver::getDatabaseObject();
6373
}
@@ -78,15 +88,18 @@ public static function getAllDocuments() {
7888
if (!$stmt->execute()) {
7989
throw new QueryException("Cannot refresh documents");
8090
}
81-
$documents = [];
91+
$ids = [];
92+
$objects = [];
8293
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
83-
$documents[] = new self($row);
94+
$ids[] = (int) $row->id;
95+
$objects[] = new self($row);
8496
Common::$cache->set(
8597
"bnetdocs-document-" . $row->id, serialize($row), 300
8698
);
8799
}
88100
$stmt->closeCursor();
89-
return $documents;
101+
Common::$cache->set($cache_key, implode(",", $ids), 300);
102+
return $objects;
90103
} catch (PDOException $e) {
91104
throw new QueryException("Cannot refresh documents", $e);
92105
}

libraries/NewsCategory.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public function __construct($data) {
4343
}
4444

4545
public static function getAll() {
46+
$cache_key = "bnetdocs-newscategories";
47+
$cache_val = Common::$cache->get($cache_key);
48+
if ($cache_val !== false) {
49+
$ids = explode(",", $cache_val);
50+
$objects = [];
51+
foreach ($ids as $id) {
52+
$objects[] = new self($id);
53+
}
54+
return $objects;
55+
}
4656
if (!isset(Common::$database)) {
4757
Common::$database = DatabaseDriver::getDatabaseObject();
4858
}
@@ -59,15 +69,18 @@ public static function getAll() {
5969
if (!$stmt->execute()) {
6070
throw new QueryException("Cannot refresh news categories");
6171
}
62-
$news_categories = [];
72+
$ids = [];
73+
$objects = [];
6374
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
64-
$news_categories[] = new self($row);
75+
$ids[] = (int) $row->id;
76+
$objects[] = new self($row);
6577
Common::$cache->set(
6678
"bnetdocs-newscategory-" . $row->id, serialize($row), 300
6779
);
6880
}
6981
$stmt->closeCursor();
70-
return $news_categories;
82+
Common::$cache->set($cache_key, implode(",", $ids), 300);
83+
return $objects;
7184
} catch (PDOException $e) {
7285
throw new QueryException("Cannot refresh news categories", $e);
7386
}

libraries/NewsPost.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ public static function create(
9595
}
9696

9797
public static function getAllNews($reverse) {
98+
$cache_key = "bnetdocs-newsposts";
99+
$cache_val = Common::$cache->get($cache_key);
100+
if ($cache_val !== false) {
101+
$ids = explode(",", $cache_val);
102+
$objects = [];
103+
foreach ($ids as $id) {
104+
$objects[] = new self($id);
105+
}
106+
return $objects;
107+
}
98108
if (!isset(Common::$database)) {
99109
Common::$database = DatabaseDriver::getDatabaseObject();
100110
}
@@ -121,15 +131,18 @@ public static function getAllNews($reverse) {
121131
if (!$stmt->execute()) {
122132
throw new QueryException("Cannot refresh news post");
123133
}
124-
$news_posts = [];
134+
$ids = [];
135+
$objects = [];
125136
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
126-
$news_posts[] = new self($row);
137+
$ids[] = (int) $row->id;
138+
$objects[] = new self($row);
127139
Common::$cache->set(
128140
"bnetdocs-newspost-" . $row->id, serialize($row), 300
129141
);
130142
}
131143
$stmt->closeCursor();
132-
return $news_posts;
144+
Common::$cache->set($cache_key, implode(",", $ids), 300);
145+
return $objects;
133146
} catch (PDOException $e) {
134147
throw new QueryException("Cannot refresh news post", $e);
135148
}

libraries/Packet.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public function __construct($data) {
8080
}
8181

8282
public static function getAllPackets() {
83+
$cache_key = "bnetdocs-packets";
84+
$cache_val = Common::$cache->get($cache_key);
85+
if ($cache_val !== false) {
86+
$ids = explode(",", $cache_val);
87+
$objects = [];
88+
foreach ($ids as $id) {
89+
$objects[] = new self($id);
90+
}
91+
return $objects;
92+
}
8393
if (!isset(Common::$database)) {
8494
Common::$database = DatabaseDriver::getDatabaseObject();
8595
}
@@ -105,15 +115,18 @@ public static function getAllPackets() {
105115
if (!$stmt->execute()) {
106116
throw new QueryException("Cannot refresh packets");
107117
}
108-
$packets = [];
118+
$ids = [];
119+
$objects = [];
109120
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
110-
$packets[] = new self($row);
121+
$ids[] = (int) $row->id;
122+
$objects[] = new self($row);
111123
Common::$cache->set(
112124
"bnetdocs-packet-" . $row->id, serialize($row), 300
113125
);
114126
}
115127
$stmt->closeCursor();
116-
return $packets;
128+
Common::$cache->set($cache_key, implode(",", $ids), 300);
129+
return $objects;
117130
} catch (PDOException $e) {
118131
throw new QueryException("Cannot refresh packets", $e);
119132
}

libraries/PacketApplicationLayer.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public function __construct($data) {
3636
}
3737

3838
public static function getAllPacketApplicationLayers() {
39+
$cache_key = "bnetdocs-packetapplicationlayers";
40+
$cache_val = Common::$cache->get($cache_key);
41+
if ($cache_val !== false) {
42+
$ids = explode(",", $cache_val);
43+
$objects = [];
44+
foreach ($ids as $id) {
45+
$objects[] = new self($id);
46+
}
47+
return $objects;
48+
}
3949
if (!isset(Common::$database)) {
4050
Common::$database = DatabaseDriver::getDatabaseObject();
4151
}
@@ -51,15 +61,18 @@ public static function getAllPacketApplicationLayers() {
5161
if (!$stmt->execute()) {
5262
throw new QueryException("Cannot refresh packet application layers");
5363
}
54-
$packetapplicationlayers = [];
64+
$ids = [];
65+
$objects = [];
5566
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
56-
$packetapplicationlayers[] = new self($row);
67+
$ids[] = (int) $row->id;
68+
$objects[] = new self($row);
5769
Common::$cache->set(
5870
"bnetdocs-packetapplicationlayer-" . $row->id, serialize($row), 300
5971
);
6072
}
6173
$stmt->closeCursor();
62-
return $packetapplicationlayers;
74+
Common::$cache->set($cache_key, implode(",", $ids), 300);
75+
return $objects;
6376
} catch (PDOException $e) {
6477
throw new QueryException("Cannot refresh packet application layers", $e);
6578
}

libraries/PacketTransportLayer.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public function __construct($data) {
3636
}
3737

3838
public static function getAllPacketTransportLayers() {
39+
$cache_key = "bnetdocs-packettransportlayers";
40+
$cache_val = Common::$cache->get($cache_key);
41+
if ($cache_val !== false) {
42+
$ids = explode(",", $cache_val);
43+
$objects = [];
44+
foreach ($ids as $id) {
45+
$objects[] = new self($id);
46+
}
47+
return $objects;
48+
}
3949
if (!isset(Common::$database)) {
4050
Common::$database = DatabaseDriver::getDatabaseObject();
4151
}
@@ -51,15 +61,18 @@ public static function getAllPacketTransportLayers() {
5161
if (!$stmt->execute()) {
5262
throw new QueryException("Cannot refresh packet transport layers");
5363
}
54-
$packettransportlayers = [];
64+
$ids = [];
65+
$objects = [];
5566
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
56-
$packettransportlayers[] = new self($row);
67+
$ids[] = (int) $row->id;
68+
$objects[] = new self($row);
5769
Common::$cache->set(
5870
"bnetdocs-packettransportlayer-" . $row->id, serialize($row), 300
5971
);
6072
}
6173
$stmt->closeCursor();
62-
return $packettransportlayers;
74+
Common::$cache->set($cache_key, implode(",", $ids), 300);
75+
return $objects;
6376
} catch (PDOException $e) {
6477
throw new QueryException("Cannot refresh packet transport layers", $e);
6578
}

libraries/Product.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public function __construct($data) {
4545
}
4646

4747
public static function getAllProducts() {
48+
$cache_key = "bnetdocs-products";
49+
$cache_val = Common::$cache->get($cache_key);
50+
if ($cache_val !== false) {
51+
$ids = explode(",", $cache_val);
52+
$objects = [];
53+
foreach ($ids as $id) {
54+
$objects[] = new self($id);
55+
}
56+
return $objects;
57+
}
4858
if (!isset(Common::$database)) {
4959
Common::$database = DatabaseDriver::getDatabaseObject();
5060
}
@@ -63,15 +73,18 @@ public static function getAllProducts() {
6373
if (!$stmt->execute()) {
6474
throw new QueryException("Cannot refresh products");
6575
}
66-
$products = [];
76+
$ids = [];
77+
$objects = [];
6778
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
68-
$products[] = new self($row);
79+
$ids[] = (int) $row->bnet_product_id;
80+
$objects[] = new self($row);
6981
Common::$cache->set(
7082
"bnetdocs-product-" . $row->bnet_product_id, serialize($row), 300
7183
);
7284
}
7385
$stmt->closeCursor();
74-
return $products;
86+
Common::$cache->set($cache_key, implode(",", $ids), 300);
87+
return $objects;
7588
} catch (PDOException $e) {
7689
throw new QueryException("Cannot refresh products", $e);
7790
}

libraries/Server.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public function getAddress() {
6363
}
6464

6565
public static function getAllServers() {
66+
$cache_key = "bnetdocs-servers";
67+
$cache_val = Common::$cache->get($cache_key);
68+
if ($cache_val !== false) {
69+
$ids = explode(",", $cache_val);
70+
$objects = [];
71+
foreach ($ids as $id) {
72+
$objects[] = new self($id);
73+
}
74+
return $objects;
75+
}
6676
if (!isset(Common::$database)) {
6777
Common::$database = DatabaseDriver::getDatabaseObject();
6878
}
@@ -89,15 +99,18 @@ public static function getAllServers() {
8999
if (!$stmt->execute()) {
90100
throw new QueryException("Cannot refresh servers");
91101
}
92-
$servers = [];
102+
$ids = [];
103+
$objects = [];
93104
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
94-
$servers[] = new self($row);
105+
$ids[] = (int) $row->id;
106+
$objects[] = new self($row);
95107
Common::$cache->set(
96108
"bnetdocs-server-" . $row->id, serialize($row), 300
97109
);
98110
}
99111
$stmt->closeCursor();
100-
return $servers;
112+
Common::$cache->set($cache_key, implode(",", $ids), 300);
113+
return $objects;
101114
} catch (PDOException $e) {
102115
throw new QueryException("Cannot refresh servers", $e);
103116
}

libraries/ServerType.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public function __construct($data) {
3333
}
3434

3535
public static function getAllServerTypes() {
36+
$cache_key = "bnetdocs-servertypes";
37+
$cache_val = Common::$cache->get($cache_key);
38+
if ($cache_val !== false) {
39+
$ids = explode(",", $cache_val);
40+
$objects = [];
41+
foreach ($ids as $id) {
42+
$objects[] = new self($id);
43+
}
44+
return $objects;
45+
}
3646
if (!isset(Common::$database)) {
3747
Common::$database = DatabaseDriver::getDatabaseObject();
3848
}
@@ -47,15 +57,18 @@ public static function getAllServerTypes() {
4757
if (!$stmt->execute()) {
4858
throw new QueryException("Cannot refresh server types");
4959
}
50-
$servers = [];
60+
$ids = [];
61+
$objects = [];
5162
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
52-
$servers[] = new self($row);
63+
$ids[] = (int) $row->id;
64+
$objects[] = new self($row);
5365
Common::$cache->set(
5466
"bnetdocs-servertype-" . $row->id, serialize($row), 300
5567
);
5668
}
5769
$stmt->closeCursor();
58-
return $servers;
70+
Common::$cache->set($cache_key, implode(",", $ids), 300);
71+
return $objects;
5972
} catch (PDOException $e) {
6073
throw new QueryException("Cannot refresh server types", $e);
6174
}

0 commit comments

Comments
 (0)