Removed needless caching stuff.
This commit is contained in:
parent
ca8095a138
commit
35d7150965
4 changed files with 70 additions and 220 deletions
|
@ -11,10 +11,6 @@ database = database
|
||||||
charset = utf8mb4
|
charset = utf8mb4
|
||||||
collation = utf8mb4_bin
|
collation = utf8mb4_bin
|
||||||
|
|
||||||
[Cache]
|
|
||||||
host = 127.0.0.1
|
|
||||||
prefix = msz:
|
|
||||||
|
|
||||||
[Site]
|
[Site]
|
||||||
name = Misuzu
|
name = Misuzu
|
||||||
description = Describe your description.
|
description = Describe your description.
|
||||||
|
|
|
@ -29,7 +29,6 @@ $errorHandler->register();
|
||||||
// TODO: do something about this, probably a good idea to include shit as required rather than all at once here
|
// TODO: do something about this, probably a good idea to include shit as required rather than all at once here
|
||||||
require_once 'src/array.php';
|
require_once 'src/array.php';
|
||||||
require_once 'src/audit_log.php';
|
require_once 'src/audit_log.php';
|
||||||
require_once 'src/cache.php';
|
|
||||||
require_once 'src/changelog.php';
|
require_once 'src/changelog.php';
|
||||||
require_once 'src/colour.php';
|
require_once 'src/colour.php';
|
||||||
require_once 'src/comments.php';
|
require_once 'src/comments.php';
|
||||||
|
@ -331,7 +330,6 @@ MIG;
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
cache_init(config_get_default([], 'Cache'));
|
|
||||||
geoip_init(config_get_default('', 'GeoIP', 'database_path'));
|
geoip_init(config_get_default('', 'GeoIP', 'database_path'));
|
||||||
|
|
||||||
if (!MSZ_DEBUG) {
|
if (!MSZ_DEBUG) {
|
||||||
|
|
150
public/index.php
150
public/index.php
|
@ -12,90 +12,80 @@ if (config_get_default(false, 'Site', 'embed_linked_data')) {
|
||||||
|
|
||||||
$news = news_posts_get(0, 5, null, true);
|
$news = news_posts_get(0, 5, null, true);
|
||||||
|
|
||||||
$stats = cache_get('index:stats:v2', function () {
|
$stats = [
|
||||||
return [
|
'users' => db_query('
|
||||||
'users' => db_query('
|
|
||||||
SELECT
|
|
||||||
(
|
|
||||||
SELECT COUNT(`user_id`)
|
|
||||||
FROM `msz_users`
|
|
||||||
WHERE `user_deleted` IS NULL
|
|
||||||
) as `all`,
|
|
||||||
(
|
|
||||||
SELECT COUNT(`user_id`)
|
|
||||||
FROM `msz_users`
|
|
||||||
WHERE `user_active` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)
|
|
||||||
) as `online`,
|
|
||||||
(
|
|
||||||
SELECT COUNT(`user_id`)
|
|
||||||
FROM `msz_users`
|
|
||||||
WHERE `user_active` >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
|
|
||||||
) as `active`
|
|
||||||
')->fetch(PDO::FETCH_ASSOC),
|
|
||||||
'comments' => (int)db_query('
|
|
||||||
SELECT COUNT(`comment_id`)
|
|
||||||
FROM `msz_comments_posts`
|
|
||||||
WHERE `comment_deleted` IS NULL
|
|
||||||
')->fetchColumn(),
|
|
||||||
'forum_topics' => (int)db_query('
|
|
||||||
SELECT COUNT(`topic_id`)
|
|
||||||
FROM `msz_forum_topics`
|
|
||||||
WHERE `topic_deleted` IS NULL
|
|
||||||
')->fetchColumn(),
|
|
||||||
'forum_posts' => (int)db_query('
|
|
||||||
SELECT COUNT(`post_id`)
|
|
||||||
FROM `msz_forum_posts`
|
|
||||||
WHERE `post_deleted` IS NULL
|
|
||||||
')->fetchColumn(),
|
|
||||||
];
|
|
||||||
}, 900);
|
|
||||||
|
|
||||||
$changelog = cache_get('index:changelog:v1', function () {
|
|
||||||
return db_query('
|
|
||||||
SELECT
|
SELECT
|
||||||
c.`change_id`, c.`change_log`,
|
(
|
||||||
a.`action_name`, a.`action_colour`, a.`action_class`,
|
SELECT COUNT(`user_id`)
|
||||||
DATE(`change_created`) as `change_date`,
|
FROM `msz_users`
|
||||||
!ISNULL(c.`change_text`) as `change_has_text`
|
WHERE `user_deleted` IS NULL
|
||||||
FROM `msz_changelog_changes` as c
|
) as `all`,
|
||||||
LEFT JOIN `msz_changelog_actions` as a
|
(
|
||||||
ON a.`action_id` = c.`action_id`
|
SELECT COUNT(`user_id`)
|
||||||
ORDER BY c.`change_created` DESC
|
FROM `msz_users`
|
||||||
LIMIT 10
|
WHERE `user_active` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)
|
||||||
')->fetchAll(PDO::FETCH_ASSOC);
|
) as `online`,
|
||||||
}, 300);
|
(
|
||||||
|
SELECT COUNT(`user_id`)
|
||||||
|
FROM `msz_users`
|
||||||
|
WHERE `user_active` >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
|
||||||
|
) as `active`
|
||||||
|
')->fetch(PDO::FETCH_ASSOC),
|
||||||
|
'comments' => (int)db_query('
|
||||||
|
SELECT COUNT(`comment_id`)
|
||||||
|
FROM `msz_comments_posts`
|
||||||
|
WHERE `comment_deleted` IS NULL
|
||||||
|
')->fetchColumn(),
|
||||||
|
'forum_topics' => (int)db_query('
|
||||||
|
SELECT COUNT(`topic_id`)
|
||||||
|
FROM `msz_forum_topics`
|
||||||
|
WHERE `topic_deleted` IS NULL
|
||||||
|
')->fetchColumn(),
|
||||||
|
'forum_posts' => (int)db_query('
|
||||||
|
SELECT COUNT(`post_id`)
|
||||||
|
FROM `msz_forum_posts`
|
||||||
|
WHERE `post_deleted` IS NULL
|
||||||
|
')->fetchColumn(),
|
||||||
|
];
|
||||||
|
|
||||||
$birthdays = user_session_active()
|
$changelog = db_query('
|
||||||
? cache_get('index:birthdays:v1', user_get_birthdays(), 300)
|
SELECT
|
||||||
: [];
|
c.`change_id`, c.`change_log`,
|
||||||
|
a.`action_name`, a.`action_colour`, a.`action_class`,
|
||||||
|
DATE(`change_created`) as `change_date`,
|
||||||
|
!ISNULL(c.`change_text`) as `change_has_text`
|
||||||
|
FROM `msz_changelog_changes` as c
|
||||||
|
LEFT JOIN `msz_changelog_actions` as a
|
||||||
|
ON a.`action_id` = c.`action_id`
|
||||||
|
ORDER BY c.`change_created` DESC
|
||||||
|
LIMIT 10
|
||||||
|
')->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
$latestUser = cache_get('index:latest_user:v1', function () {
|
$birthdays = user_session_active() ? user_get_birthdays() : [];
|
||||||
return db_query('
|
|
||||||
SELECT
|
|
||||||
u.`user_id`, u.`username`, u.`user_created`,
|
|
||||||
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`
|
|
||||||
FROM `msz_users` as u
|
|
||||||
LEFT JOIN `msz_roles` as r
|
|
||||||
ON r.`role_id` = u.`display_role`
|
|
||||||
WHERE `user_deleted` IS NULL
|
|
||||||
ORDER BY u.`user_id` DESC
|
|
||||||
LIMIT 1
|
|
||||||
')->fetch(PDO::FETCH_ASSOC);
|
|
||||||
}, 1800);
|
|
||||||
|
|
||||||
$onlineUsers = cache_get('index:online_users:v1', function () {
|
$latestUser = db_query('
|
||||||
return db_query('
|
SELECT
|
||||||
SELECT
|
u.`user_id`, u.`username`, u.`user_created`,
|
||||||
u.`user_id`, u.`username`,
|
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`
|
||||||
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`
|
FROM `msz_users` as u
|
||||||
FROM `msz_users` as u
|
LEFT JOIN `msz_roles` as r
|
||||||
LEFT JOIN `msz_roles` as r
|
ON r.`role_id` = u.`display_role`
|
||||||
ON r.`role_id` = u.`display_role`
|
WHERE `user_deleted` IS NULL
|
||||||
WHERE u.`user_active` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)
|
ORDER BY u.`user_id` DESC
|
||||||
ORDER BY u.`user_active` DESC
|
LIMIT 1
|
||||||
LIMIT 104
|
')->fetch(PDO::FETCH_ASSOC);
|
||||||
')->fetchAll(PDO::FETCH_ASSOC);
|
|
||||||
}, 30);
|
$onlineUsers = db_query('
|
||||||
|
SELECT
|
||||||
|
u.`user_id`, u.`username`,
|
||||||
|
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`
|
||||||
|
FROM `msz_users` as u
|
||||||
|
LEFT JOIN `msz_roles` as r
|
||||||
|
ON r.`role_id` = u.`display_role`
|
||||||
|
WHERE u.`user_active` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)
|
||||||
|
ORDER BY u.`user_active` DESC
|
||||||
|
LIMIT 104
|
||||||
|
')->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
echo tpl_render('home.' . (user_session_active() ? 'home' : 'landing'), [
|
echo tpl_render('home.' . (user_session_active() ? 'home' : 'landing'), [
|
||||||
'statistics' => $stats,
|
'statistics' => $stats,
|
||||||
|
|
134
src/cache.php
134
src/cache.php
|
@ -1,134 +0,0 @@
|
||||||
<?php
|
|
||||||
define('MSZ_CACHE_REDIS_STORE', '_msz_cache_redis');
|
|
||||||
define('MSZ_CACHE_OPTIONS_STORE', '_msz_cache_options');
|
|
||||||
|
|
||||||
define('MSZ_CACHE_INIT_OK', 0);
|
|
||||||
define('MSZ_CACHE_INIT_ACTIVE', 1);
|
|
||||||
define('MSZ_CACHE_INIT_FAIL', 2);
|
|
||||||
define('MSZ_CACHE_INIT_AUTH', 3);
|
|
||||||
define('MSZ_CACHE_INIT_DATABASE', 4);
|
|
||||||
|
|
||||||
function cache_init(array $options, bool $start = false): void
|
|
||||||
{
|
|
||||||
$GLOBALS[MSZ_CACHE_OPTIONS_STORE] = $options;
|
|
||||||
|
|
||||||
if ($start) {
|
|
||||||
cache_start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function cache_start(?array $options = null): bool
|
|
||||||
{
|
|
||||||
if (!empty($GLOBALS[MSZ_CACHE_REDIS_STORE])) {
|
|
||||||
return MSZ_CACHE_INIT_ACTIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($options === null) {
|
|
||||||
$options = $GLOBALS[MSZ_CACHE_OPTIONS_STORE] ?? [];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($options['host'])) {
|
|
||||||
// if no host is present we just act as a void
|
|
||||||
return MSZ_CACHE_INIT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
$redis = new Redis;
|
|
||||||
|
|
||||||
if (!$redis->connect($options['host'], $options['port'] ?? null)) {
|
|
||||||
return MSZ_CACHE_INIT_FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($options['password']) && !$redis->auth($options['password'])) {
|
|
||||||
return MSZ_CACHE_INIT_AUTH;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($options['database']) && !$redis->select($options['database'])) {
|
|
||||||
return MSZ_CACHE_INIT_DATABASE;
|
|
||||||
}
|
|
||||||
|
|
||||||
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
|
|
||||||
$redis->setOption(Redis::OPT_PREFIX, $options['prefix'] ?? '');
|
|
||||||
|
|
||||||
$GLOBALS[MSZ_CACHE_REDIS_STORE] = $redis;
|
|
||||||
return MSZ_CACHE_INIT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
function cache_available(): bool
|
|
||||||
{
|
|
||||||
if (!empty($GLOBALS[MSZ_CACHE_REDIS_STORE])) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$startUp = cache_start();
|
|
||||||
return $startUp === MSZ_CACHE_INIT_OK || $startUp === MSZ_CACHE_INIT_ACTIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
function cache_exists(string $key): bool
|
|
||||||
{
|
|
||||||
return cache_available() && $GLOBALS[MSZ_CACHE_REDIS_STORE]->exists($key);
|
|
||||||
}
|
|
||||||
|
|
||||||
function cache_remove($keys): int
|
|
||||||
{
|
|
||||||
if (!cache_available()) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $GLOBALS[MSZ_CACHE_REDIS_STORE]->delete($keys);
|
|
||||||
}
|
|
||||||
|
|
||||||
function cache_get(string $key, $fallback, int $ttl = 0)
|
|
||||||
{
|
|
||||||
if (!cache_available() || $ttl < 0) {
|
|
||||||
return is_callable($fallback) ? $fallback() : $fallback;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!cache_exists($key)) {
|
|
||||||
return cache_set($key, $fallback, $ttl);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $GLOBALS[MSZ_CACHE_REDIS_STORE]->get($key);
|
|
||||||
}
|
|
||||||
|
|
||||||
function cache_set(string $key, $value, int $ttl = 0)
|
|
||||||
{
|
|
||||||
if (is_callable($value)) {
|
|
||||||
$value = $value();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!cache_available() || $ttl < 0) {
|
|
||||||
return $value;
|
|
||||||
} elseif ($ttl < 1) {
|
|
||||||
$GLOBALS[MSZ_CACHE_REDIS_STORE]->set($key, $value);
|
|
||||||
} else {
|
|
||||||
$GLOBALS[MSZ_CACHE_REDIS_STORE]->setEx($key, $ttl, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
function cache_increment(string $key, int $amount = 1): int
|
|
||||||
{
|
|
||||||
if (!cache_available()) {
|
|
||||||
return abs($amount);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($amount <= 1) {
|
|
||||||
return $GLOBALS[MSZ_CACHE_REDIS_STORE]->incr($key);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $GLOBALS[MSZ_CACHE_REDIS_STORE]->incrBy($key, $amount);
|
|
||||||
}
|
|
||||||
|
|
||||||
function cache_decrement(string $key, int $amount = 1): int
|
|
||||||
{
|
|
||||||
if (!cache_available()) {
|
|
||||||
return abs($amount) * -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($amount <= 1) {
|
|
||||||
return $GLOBALS[MSZ_CACHE_REDIS_STORE]->decr($key);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $GLOBALS[MSZ_CACHE_REDIS_STORE]->decrBy($key, $amount);
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue