1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-04-05 00:13:29 +02:00

[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
arguments:
- '@dbal.conn'
- '@cache.driver'
- '@storage.adapter.factory'
- 'attachment'
- '%tables.storage%'
@ -15,6 +16,7 @@ services:
class: phpbb\storage\storage
arguments:
- '@dbal.conn'
- '@cache.driver'
- '@storage.adapter.factory'
- 'avatar'
- '%tables.storage%'
@ -25,6 +27,7 @@ services:
class: phpbb\storage\storage
arguments:
- '@dbal.conn'
- '@cache.driver'
- '@storage.adapter.factory'
- 'backup'
- '%tables.storage%'

View File

@ -13,18 +13,35 @@
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
*/
class storage
{
/**
* @var \phpbb\storage\adapter\adapter_interface
*/
protected $adapter;
/**
* @var \phpbb\db\driver\driver_interface
*/
protected $db;
/**
* Cache driver
* @var \phpbb\cache\driver\driver_interface
*/
protected $cache;
/**
* @var \phpbb\storage\adapter_factory
*/
protected $factory;
/**
* @var string
*/
@ -35,26 +52,18 @@ class storage
*/
protected $storage_table;
/**
* @var \phpbb\storage\adapter_factory
*/
protected $factory;
/**
* @var \phpbb\storage\adapter\adapter_interface
*/
protected $adapter;
/**
* Constructor
*
* @param \phpbb\cache\driver\driver_interface $db
* @param \phpbb\db\driver\driver_interface $db
* @param \phpbb\storage\adapter_factory $factory
* @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->cache = $cache;
$this->factory = $factory;
$this->storage_name = $storage_name;
$this->storage_table = $storage_table;
@ -255,6 +264,9 @@ class storage
WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
$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)
@ -267,6 +279,9 @@ class storage
$sql = 'DELETE FROM ' . $this->storage_table . '
WHERE ' . $this->db->sql_build_array('DELETE', $sql_ary);
$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()
{
$sql = 'SELECT SUM(filesize) AS total
FROM ' . $this->storage_table . "
WHERE storage = '" . $this->get_name() . "'";
$result = $this->db->sql_query($sql);
$total = (int) $this->db->sql_fetchfield('total');
$this->db->sql_freeresult($result);
$total_size = $this->cache->get('_storage_' . $this->get_name() . '_totalsize');
return $row;
if ($total_size === false)
{
$sql = 'SELECT SUM(filesize) AS totalsize
FROM ' . $this->storage_table . "
WHERE storage = '" . $this->get_name() . "'";
$result = $this->db->sql_query($sql);
$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()
{
$sql = 'SELECT COUNT(file_id) AS total
FROM ' . $this->storage_table . "
WHERE storage = '" . $this->get_name() . "'";
$result = $this->db->sql_query($sql);
$total = (int) $this->db->sql_fetchfield('total');
$this->db->sql_freeresult($result);
$number_files = $this->cache->get('_storage_' . $this->get_name() . '_numfiles');
return $total;
if ($number_files === false)
{
$sql = 'SELECT COUNT(file_id) AS numfiles
FROM ' . $this->storage_table . "
WHERE storage = '" . $this->get_name() . "'";
$result = $this->db->sql_query($sql);
$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;
}
}