Added support for APCu caching method (#1442)

* Added support for APCu caching method

* Create APCu.php

* Update APCu.php

* Updated

* Update CHANGELOG.md
This commit is contained in:
Roman Kelesidis 2024-04-10 19:38:53 +07:00 committed by GitHub
commit 564fc25cbc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 173 additions and 0 deletions

View file

@ -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))

View file

@ -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;

80
src/Legacy/Cache/APCu.php Normal file
View file

@ -0,0 +1,80 @@
<?php
/**
* TorrentPier Bull-powered BitTorrent tracker engine
*
* @copyright Copyright (c) 2005-2024 TorrentPier (https://torrentpier.com)
* @link https://github.com/torrentpier/torrentpier for the canonical source repository
* @license https://github.com/torrentpier/torrentpier/blob/master/LICENSE MIT License
*/
namespace TorrentPier\Legacy\Cache;
use TorrentPier\Dev;
/**
* Class APCu
* @package TorrentPier\Legacy\Cache
*/
class APCu extends Common
{
public $used = true;
public $engine = 'APCu';
public $prefix;
public function __construct($prefix = null)
{
if (!$this->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');
}
}

View file

@ -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']);

View file

@ -0,0 +1,82 @@
<?php
/**
* TorrentPier Bull-powered BitTorrent tracker engine
*
* @copyright Copyright (c) 2005-2024 TorrentPier (https://torrentpier.com)
* @link https://github.com/torrentpier/torrentpier for the canonical source repository
* @license https://github.com/torrentpier/torrentpier/blob/master/LICENSE MIT License
*/
namespace TorrentPier\Legacy\Datastore;
use TorrentPier\Dev;
/**
* Class APCu
* @package TorrentPier\Legacy\Datastore
*/
class APCu extends Common
{
public $prefix;
public $engine = 'APCu';
public function __construct($prefix = null)
{
if (!$this->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');
}
}