Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various performance improvements #3119

Merged
merged 4 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions modules/Core/widgets/ProfilePostsWidget.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

/*
* Made by Aberdeener
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-pr8
* NamelessMC version 2.0.2
*
* License: MIT
*
Expand Down Expand Up @@ -51,21 +50,37 @@ public function initialise(): void {
if ($this->_cache->isCached('profile_posts_' . $user_id)) {
$posts_array = $this->_cache->retrieve('profile_posts_' . $user_id);
} else {
$posts = DB::getInstance()->query('SELECT * FROM nl2_user_profile_wall_posts ORDER BY time DESC LIMIT 5')->results();
foreach ($posts as $post) {
$post_author = new User($post->author_id);

if ($this->_user->isLoggedIn()) {
if ($this->_user->isBlocked($post->author_id, $this->_user->data()->id)) {
continue;
}
if ($post_author->isPrivateProfile() && !$this->_user->hasPermission('profile.private.bypass')) {
continue;
}
} else if ($post_author->isPrivateProfile()) {
continue;
if ($this->_user->isLoggedIn()) {
if ($this->_user->hasPermission('profile.private.bypass')) {
$posts = DB::getInstance()->query('SELECT * FROM nl2_user_profile_wall_posts ORDER BY `time` DESC LIMIT 5')->results();
} else {
$posts = DB::getInstance()->query(
<<<SQL
SELECT *
FROM nl2_user_profile_wall_posts
WHERE `user_id` NOT IN (
SELECT `id` FROM nl2_users WHERE `private_profile` = 1
)
AND EXISTS (SELECT `id` FROM nl2_blocked_users WHERE `user_blocked_id` = `user_id`) = 0
ORDER BY `time` DESC LIMIT 5
SQL,
)->results();
}
} else {
$posts = DB::getInstance()->query(
<<<SQL
SELECT *
FROM nl2_user_profile_wall_posts
WHERE `user_id` NOT IN (
SELECT `id` FROM nl2_users WHERE `private_profile` = 1
)
ORDER BY `time` DESC LIMIT 5
SQL,
)->results();
}

foreach ($posts as $post) {
$post_author = new User($post->author_id);
$post_user = new User($post->user_id);
$link = rtrim($post_user->getProfileURL(), '/');

Expand Down
31 changes: 10 additions & 21 deletions modules/Core/widgets/StatsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Made by Samerton
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-pr8
* NamelessMC version 2.0.2
*
* License: MIT
*
Expand Down Expand Up @@ -36,28 +36,24 @@ public function initialise(): void {
$this->_cache->setCache('statistics');

if ($this->_cache->isCached('statistics')) {

$users_query = $this->_cache->retrieve('statistics');
$users_registered = $users_query['users_registered'];
$latest_member = $users_query['latest_member'];

} else {
$users_query = DB::getInstance()->query('SELECT `id` FROM nl2_users ORDER BY `joined` DESC LIMIT 1')->first()->id;
$users_registered = DB::getInstance()->query('SELECT COUNT(*) as c FROM nl2_users')->first()->c;

$users_query = DB::getInstance()->orderAll('users', 'joined', 'DESC')->results();
$users_registered = count($users_query);

$latest_user = new User($users_query[0]->id);
$latest_user = new User($users_query);
$latest_member = [
'style' => $latest_user->getGroupStyle(),
'profile' => $latest_user->getProfileURL(),
'avatar' => $latest_user->getAvatar(),
'username' => $latest_user->getDisplayname(true),
'nickname' => $latest_user->getDisplayname(),
'id' => Output::getClean($users_query[0]->id)
'id' => Output::getClean($users_query)
];

$users_query = null;

$this->_cache->store(
'statistics',
[
Expand All @@ -70,17 +66,15 @@ public function initialise(): void {
}

if (!$this->_cache->isCached('online_users')) {
$online_users = DB::getInstance()->query('SELECT count(*) FROM nl2_users WHERE last_online > ?', [strtotime('-5 minutes')])->first();
$online_users = $online_users->{'count(*)'};
$online_users = DB::getInstance()->query('SELECT COUNT(*) as c FROM nl2_users WHERE last_online > ?', [strtotime('-5 minutes')])->first()->c;
$this->_cache->store('online_users', $online_users, 60);
} else {
$online_users = $this->_cache->retrieve('online_users');
}

if (!$this->_cache->isCached('online_guests')) {
try {
$online_guests = DB::getInstance()->query('SELECT count(*) FROM nl2_online_guests WHERE last_seen > ?', [strtotime('-5 minutes')])->first();
$online_guests = $online_guests->{'count(*)'};
$online_guests = DB::getInstance()->query('SELECT COUNT(*) as c FROM nl2_online_guests WHERE last_seen > ?', [strtotime('-5 minutes')])->first()->c;
$this->_cache->store('online_guests', $online_guests, 60);
} catch (Exception $e) {
// Upgrade script hasn't been run
Expand All @@ -90,22 +84,17 @@ public function initialise(): void {
$online_guests = $this->_cache->retrieve('online_guests');
}

$forum_module = DB::getInstance()->get('modules', ['name', 'Forum'])->results();
$forum_module = $forum_module[0];

if ($forum_module->enabled) {
if (Util::isModuleEnabled('Forum')) {
$this->_cache->setCache('forum_stats');
if (!$this->_cache->isCached('total_topics')) {
$total_topics = DB::getInstance()->query('SELECT count(*) FROM nl2_topics WHERE deleted = 0')->first();
$total_topics = $total_topics->{'count(*)'};
$total_topics = DB::getInstance()->query('SELECT COUNT(*) as c FROM nl2_topics WHERE deleted = 0')->first()->c;
$this->_cache->store('total_topics', $total_topics, 60);
} else {
$total_topics = $this->_cache->retrieve('total_topics');
}

if (!$this->_cache->isCached('total_posts')) {
$total_posts = DB::getInstance()->query('SELECT count(*) FROM nl2_posts WHERE deleted = 0')->first();
$total_posts = $total_posts->{'count(*)'};
$total_posts = DB::getInstance()->query('SELECT COUNT(*) as c FROM nl2_posts WHERE deleted = 0')->first()->c;
$this->_cache->store('total_posts', $total_posts, 60);
} else {
$total_posts = $this->_cache->retrieve('total_posts');
Expand Down
Loading