From 564fc25cbc3d6dca20558b6a9d881fa165abd245 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Wed, 10 Apr 2024 19:38:53 +0700 Subject: [PATCH] Added support for APCu caching method (#1442) * Added support for APCu caching method * Create APCu.php * Update APCu.php * Updated * Update CHANGELOG.md --- CHANGELOG.md | 1 + common.php | 4 ++ src/Legacy/Cache/APCu.php | 80 ++++++++++++++++++++++++++++++++++ src/Legacy/Caches.php | 6 +++ src/Legacy/Datastore/APCu.php | 82 +++++++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 src/Legacy/Cache/APCu.php create mode 100644 src/Legacy/Datastore/APCu.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 286d4c71d..53c1f50cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ **Merged pull requests:** - Release 2.4.3 🐎 ([belomaxorka](https://github.com/belomaxorka)) +- Added support for APCu caching method [\#1442](https://github.com/torrentpier/torrentpier/pull/1442) ([belomaxorka](https://github.com/belomaxorka)) - Improved app debug [\#1438](https://github.com/torrentpier/torrentpier/pull/1438) ([belomaxorka](https://github.com/belomaxorka)) - Demo mode: Allow registering torrents by default [\#1440](https://github.com/torrentpier/torrentpier/pull/1440) ([belomaxorka](https://github.com/belomaxorka)) - Code refactoring [\#1441](https://github.com/torrentpier/torrentpier/pull/1441) ([belomaxorka](https://github.com/belomaxorka)) diff --git a/common.php b/common.php index a9e73c8eb..920678fbb 100644 --- a/common.php +++ b/common.php @@ -146,6 +146,10 @@ function CACHE(string $cache_name) * Datastore */ switch ($bb_cfg['datastore_type']) { + case 'apcu': + $datastore = new TorrentPier\Legacy\Datastore\APCu($bb_cfg['cache']['prefix']); + break; + case 'memcache': $datastore = new TorrentPier\Legacy\Datastore\Memcache($bb_cfg['cache']['memcache'], $bb_cfg['cache']['prefix']); break; diff --git a/src/Legacy/Cache/APCu.php b/src/Legacy/Cache/APCu.php new file mode 100644 index 000000000..60d7482a6 --- /dev/null +++ b/src/Legacy/Cache/APCu.php @@ -0,0 +1,80 @@ +is_installed()) { + die("Error: $this->engine extension not installed"); + } + + $this->prefix = $prefix; + $this->dbg_enabled = Dev::sql_dbg_enabled(); + } + + public function get($name, $get_miss_key_callback = '', $ttl = 0) + { + $this->cur_query = "cache->get('$name')"; + $this->debug('start'); + $this->debug('stop'); + $this->cur_query = null; + $this->num_queries++; + + return apcu_fetch($this->prefix . $name); + } + + public function set($name, $value, $ttl = 0) + { + $this->cur_query = "cache->set('$name')"; + $this->debug('start'); + + if (apcu_store($this->prefix . $name, $value, $ttl)) { + $this->debug('stop'); + $this->cur_query = null; + $this->num_queries++; + + return true; + } + + return false; + } + + public function rm($name = '') + { + if ($name) { + $this->cur_query = "cache->rm('$name')"; + $this->debug('start'); + $this->debug('stop'); + $this->cur_query = null; + $this->num_queries++; + + return apcu_delete($this->prefix . $name); + } + + return apcu_clear_cache(); + } + + public function is_installed(): bool + { + return function_exists('apcu_add'); + } +} diff --git a/src/Legacy/Caches.php b/src/Legacy/Caches.php index 8c49dceba..4d60f9b64 100644 --- a/src/Legacy/Caches.php +++ b/src/Legacy/Caches.php @@ -35,6 +35,12 @@ class Caches $cache_cfg =& $engine_cfg[1]; switch ($cache_type) { + case 'apcu': + if (!isset($this->obj[$cache_name])) { + $this->obj[$cache_name] = new Cache\APCu($this->cfg['prefix']); + } + $this->ref[$cache_name] =& $this->obj[$cache_name]; + break; case 'memcache': if (!isset($this->obj[$cache_name])) { $this->obj[$cache_name] = new Cache\Memcache($this->cfg['memcache'], $this->cfg['prefix']); diff --git a/src/Legacy/Datastore/APCu.php b/src/Legacy/Datastore/APCu.php new file mode 100644 index 000000000..beb19aff4 --- /dev/null +++ b/src/Legacy/Datastore/APCu.php @@ -0,0 +1,82 @@ +is_installed()) { + die("Error: $this->engine extension not installed"); + } + + $this->dbg_enabled = Dev::sql_dbg_enabled(); + $this->prefix = $prefix; + } + + public function store($title, $var) + { + $this->data[$title] = $var; + + $this->cur_query = "cache->set('$title')"; + $this->debug('start'); + $this->debug('stop'); + $this->cur_query = null; + $this->num_queries++; + + return (bool)apcu_store($this->prefix . $title, $var); + } + + public function clean() + { + foreach ($this->known_items as $title => $script_name) { + $this->cur_query = "cache->rm('$title')"; + $this->debug('start'); + $this->debug('stop'); + $this->cur_query = null; + $this->num_queries++; + + apcu_delete($this->prefix . $title); + } + } + + public function _fetch_from_store() + { + $item = null; + if (!$items = $this->queued_items) { + $src = $this->_debug_find_caller('enqueue'); + trigger_error("Datastore: item '$item' already enqueued [$src]", E_USER_ERROR); + } + + foreach ($items as $item) { + $this->cur_query = "cache->get('$item')"; + $this->debug('start'); + $this->debug('stop'); + $this->cur_query = null; + $this->num_queries++; + + $this->data[$item] = apcu_fetch($this->prefix . $item); + } + } + + public function is_installed(): bool + { + return function_exists('apcu_add'); + } +}