mirror of
https://github.com/torrentpier/torrentpier
synced 2025-08-14 18:48:21 -07:00
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:
parent
2472608c6d
commit
564fc25cbc
5 changed files with 173 additions and 0 deletions
|
@ -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))
|
||||
|
|
|
@ -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
80
src/Legacy/Cache/APCu.php
Normal 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');
|
||||
}
|
||||
}
|
|
@ -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']);
|
||||
|
|
82
src/Legacy/Datastore/APCu.php
Normal file
82
src/Legacy/Datastore/APCu.php
Normal 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');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue