[ticket/15342] Use cache to get the number of files and the size of any storage

PHPBB3-15342
This commit is contained in:
Rubén Calvo 2018-06-10 20:26:22 +02:00
parent 80e5fe255b
commit 9184d34a51
2 changed files with 60 additions and 26 deletions

View file

@ -5,6 +5,7 @@ services:
class: phpbb\storage\storage class: phpbb\storage\storage
arguments: arguments:
- '@dbal.conn' - '@dbal.conn'
- '@cache.driver'
- '@storage.adapter.factory' - '@storage.adapter.factory'
- 'attachment' - 'attachment'
- '%tables.storage%' - '%tables.storage%'
@ -15,6 +16,7 @@ services:
class: phpbb\storage\storage class: phpbb\storage\storage
arguments: arguments:
- '@dbal.conn' - '@dbal.conn'
- '@cache.driver'
- '@storage.adapter.factory' - '@storage.adapter.factory'
- 'avatar' - 'avatar'
- '%tables.storage%' - '%tables.storage%'
@ -25,6 +27,7 @@ services:
class: phpbb\storage\storage class: phpbb\storage\storage
arguments: arguments:
- '@dbal.conn' - '@dbal.conn'
- '@cache.driver'
- '@storage.adapter.factory' - '@storage.adapter.factory'
- 'backup' - 'backup'
- '%tables.storage%' - '%tables.storage%'

View file

@ -13,18 +13,35 @@
namespace phpbb\storage; namespace phpbb\storage;
use phpbb\db\driver\driver_interface; use phpbb\cache\driver\driver_interface as cache;
use phpbb\db\driver\driver_interface as db;
/** /**
* @internal Experimental * @internal Experimental
*/ */
class storage class storage
{ {
/**
* @var \phpbb\storage\adapter\adapter_interface
*/
protected $adapter;
/** /**
* @var \phpbb\db\driver\driver_interface * @var \phpbb\db\driver\driver_interface
*/ */
protected $db; protected $db;
/**
* Cache driver
* @var \phpbb\cache\driver\driver_interface
*/
protected $cache;
/**
* @var \phpbb\storage\adapter_factory
*/
protected $factory;
/** /**
* @var string * @var string
*/ */
@ -35,26 +52,18 @@ class storage
*/ */
protected $storage_table; protected $storage_table;
/**
* @var \phpbb\storage\adapter_factory
*/
protected $factory;
/**
* @var \phpbb\storage\adapter\adapter_interface
*/
protected $adapter;
/** /**
* Constructor * Constructor
* *
* @param \phpbb\cache\driver\driver_interface $db
* @param \phpbb\db\driver\driver_interface $db * @param \phpbb\db\driver\driver_interface $db
* @param \phpbb\storage\adapter_factory $factory * @param \phpbb\storage\adapter_factory $factory
* @param string $storage_name * @param string $storage_name
*/ */
public function __construct(driver_interface $db, adapter_factory $factory, $storage_name, $storage_table) public function __construct(db $db, cache $cache, adapter_factory $factory, $storage_name, $storage_table)
{ {
$this->db = $db; $this->db = $db;
$this->cache = $cache;
$this->factory = $factory; $this->factory = $factory;
$this->storage_name = $storage_name; $this->storage_name = $storage_name;
$this->storage_table = $storage_table; $this->storage_table = $storage_table;
@ -255,6 +264,9 @@ class storage
WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
$this->db->sql_query($sql); $this->db->sql_query($sql);
} }
$this->cache->destroy('_storage_' . $this->get_name() . '_totalsize');
$this->cache->destroy('_storage_' . $this->get_name() . '_numfiles');
} }
public function untrack_file($path) public function untrack_file($path)
@ -267,6 +279,9 @@ class storage
$sql = 'DELETE FROM ' . $this->storage_table . ' $sql = 'DELETE FROM ' . $this->storage_table . '
WHERE ' . $this->db->sql_build_array('DELETE', $sql_ary); WHERE ' . $this->db->sql_build_array('DELETE', $sql_ary);
$this->db->sql_query($sql); $this->db->sql_query($sql);
$this->cache->destroy('_storage_' . $this->get_name() . '_totalsize');
$this->cache->destroy('_storage_' . $this->get_name() . '_numfiles');
} }
/** /**
@ -303,14 +318,22 @@ class storage
*/ */
public function get_size() public function get_size()
{ {
$sql = 'SELECT SUM(filesize) AS total $total_size = $this->cache->get('_storage_' . $this->get_name() . '_totalsize');
if ($total_size === false)
{
$sql = 'SELECT SUM(filesize) AS totalsize
FROM ' . $this->storage_table . " FROM ' . $this->storage_table . "
WHERE storage = '" . $this->get_name() . "'"; WHERE storage = '" . $this->get_name() . "'";
$result = $this->db->sql_query($sql); $result = $this->db->sql_query($sql);
$total = (int) $this->db->sql_fetchfield('total');
$this->db->sql_freeresult($result);
return $row; $total_size = (int) $this->db->sql_fetchfield('totalsize');
$this->cache->put('_storage_' . $this->get_name() . '_totalsize', $total_size);
$this->db->sql_freeresult($result);
}
return $total_size;
} }
/** /**
@ -320,13 +343,21 @@ class storage
*/ */
public function get_num_files() public function get_num_files()
{ {
$sql = 'SELECT COUNT(file_id) AS total $number_files = $this->cache->get('_storage_' . $this->get_name() . '_numfiles');
if ($number_files === false)
{
$sql = 'SELECT COUNT(file_id) AS numfiles
FROM ' . $this->storage_table . " FROM ' . $this->storage_table . "
WHERE storage = '" . $this->get_name() . "'"; WHERE storage = '" . $this->get_name() . "'";
$result = $this->db->sql_query($sql); $result = $this->db->sql_query($sql);
$total = (int) $this->db->sql_fetchfield('total');
$this->db->sql_freeresult($result);
return $total; $number_files = (int) $this->db->sql_fetchfield('numfiles');
$this->cache->put('_storage_' . $this->get_name() . '_numfiles', $number_files);
$this->db->sql_freeresult($result);
}
return $number_files;
} }
} }