From fb93b908c9f077c48a18e289b9420b65e949001a Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 7 Oct 2018 00:40:14 +0200 Subject: [PATCH] Caching but it's procedural now. --- misuzu.php | 10 +--- public/index.php | 6 +-- src/Cache.php | 126 -------------------------------------------- src/cache.php | 134 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 137 deletions(-) delete mode 100644 src/Cache.php create mode 100644 src/cache.php diff --git a/misuzu.php b/misuzu.php index d9560240..7130ebb4 100644 --- a/misuzu.php +++ b/misuzu.php @@ -28,6 +28,7 @@ $errorHandler->register(); require_once 'src/array.php'; require_once 'src/audit_log.php'; +require_once 'src/cache.php'; require_once 'src/changelog.php'; require_once 'src/colour.php'; require_once 'src/comments.php'; @@ -244,14 +245,7 @@ MIG; exit; } - new Cache( - config_get('Cache', 'host'), - config_get('Cache', 'port'), - config_get('Cache', 'database'), - config_get('Cache', 'password'), - config_get_default('', 'Cache', 'prefix') - ); - + cache_init(config_get_default([], 'Cache')); geoip_init(config_get_default('', 'GeoIP', 'database_path')); tpl_init([ diff --git a/public/index.php b/public/index.php index df6412aa..f02f810c 100644 --- a/public/index.php +++ b/public/index.php @@ -33,7 +33,7 @@ $news = Database::query(' LIMIT 5 ')->fetchAll(PDO::FETCH_ASSOC); -$statistics = Cache::instance()->get('index:stats:v1', function () { +$statistics = cache_get('index:stats:v1', function () { return [ 'users' => (int)Database::query(' SELECT COUNT(`user_id`) @@ -52,7 +52,7 @@ $statistics = Cache::instance()->get('index:stats:v1', function () { ]; }, 10800); -$changelog = Cache::instance()->get('index:changelog:v1', function () { +$changelog = cache_get('index:changelog:v1', function () { return Database::query(' SELECT c.`change_id`, c.`change_log`, @@ -67,7 +67,7 @@ $changelog = Cache::instance()->get('index:changelog:v1', function () { ')->fetchAll(PDO::FETCH_ASSOC); }, 1800); -$onlineUsers = Cache::instance()->get('index:online:v1', function () { +$onlineUsers = cache_get('index:online:v1', function () { return Database::query(' SELECT u.`user_id`, u.`username`, diff --git a/src/Cache.php b/src/Cache.php deleted file mode 100644 index c4a371b6..00000000 --- a/src/Cache.php +++ /dev/null @@ -1,126 +0,0 @@ -redis; - } - - public static function hasInstance(): bool - { - return self::$instance instanceof static; - } - - public function __construct( - string $host, - ?int $port = null, - ?int $database = null, - ?string $password = null, - string $prefix = '' - ) { - if (self::hasInstance()) { - throw new UnexpectedValueException('Only one instance of Cache may exist.'); - } - - self::$instance = $this; - $this->redis = new Redis; - $this->redis->connect($host, $port); - - if ($password !== null && !$this->redis->auth($password)) { - throw new InvalidArgumentException('Redis auth failed.'); - } - - if ($database !== null && !$this->redis->select($database)) { - throw new UnexpectedValueException('Redis select failed.'); - } - - $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); - $this->redis->setOption(Redis::OPT_PREFIX, $prefix); - } - - public function __destruct() - { - $this->redis->close(); - self::$instance = null; - } - - public function set(string $key, $value, int $ttl = 0) - { - if (is_callable($value)) { - $value = $value(); - } - - if ($ttl < 0) { - return $value; - } elseif ($ttl < 1) { - $this->redis->set($key, $value); - } else { - $this->redis->setEx($key, $ttl, $value); - } - - return $value; - } - - public function exists(string $key): bool - { - return $this->redis->exists($key); - } - - public function delete($keys): int - { - return $this->redis->delete($keys); - } - - public function increment(string $key, int $amount = 1): int - { - if ($amount <= 1) { - return $this->redis->incr($key); - } - - return $this->redis->incrBy($key, $amount); - } - - public function decrement(string $key, int $amount = 1): int - { - if ($amount <= 1) { - return $this->redis->decr($key); - } - - return $this->redis->decrBy($key, $amount); - } - - public function get(string $key, $fallback, int $ttl = 0) - { - if ($ttl < 0) { - return is_callable($fallback) ? $fallback() : $fallback; - } - - if (!$this->exists($key)) { - return $this->set($key, $fallback, $ttl); - } - - return $this->redis->get($key); - } -} diff --git a/src/cache.php b/src/cache.php new file mode 100644 index 00000000..503c62c7 --- /dev/null +++ b/src/cache.php @@ -0,0 +1,134 @@ +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); +}