Minor improvements

This commit is contained in:
Alejandro Gallardo Escobar 2019-05-27 12:10:05 +02:00
parent ce8ba54e09
commit f8a0fc67f9
2 changed files with 36 additions and 31 deletions

View File

@ -9,7 +9,7 @@ use Models\Model;
* This class should be extended to add functionalities to * This class should be extended to add functionalities to
* fetch, clear and save item cache. * fetch, clear and save item cache.
*/ */
abstract class CacheModel extends Model abstract class CachedModel extends Model
{ {
@ -37,7 +37,10 @@ abstract class CacheModel extends Model
* *
* @abstract * @abstract
*/ */
abstract protected static function saveCachedData(array $filter, array $data): bool; abstract protected static function saveCachedData(
array $filter,
array $data
): bool;
/** /**
@ -66,10 +69,10 @@ abstract class CacheModel extends Model
{ {
global $config; global $config;
if ($filter['cache_expiration'] != 0) { if ($filter['cache_expiration'] > 0) {
// Obtain the item's data from cache. // Obtain the item's data from cache.
$cacheData = static::fetchCachedData($filter); $cachedData = static::fetchCachedData($filter);
if ($cacheData === null) { if ($cachedData === null) {
// Delete expired data cache. // Delete expired data cache.
static::clearCachedData( static::clearCachedData(
[ [
@ -91,7 +94,7 @@ abstract class CacheModel extends Model
$data $data
); );
} else { } else {
$data = \io_safe_output(\json_decode(\base64_decode($cacheData['data']), true)); $data = $cachedData;
} }
} else { } else {
$data = static::fetchDataFromDB($filter); $data = static::fetchDataFromDB($filter);

View File

@ -3,12 +3,12 @@
declare(strict_types=1); declare(strict_types=1);
namespace Models\VisualConsole; namespace Models\VisualConsole;
use Models\CacheModel; use Models\CachedModel;
/** /**
* Model of a generic Visual Console Item. * Model of a generic Visual Console Item.
*/ */
class Item extends CacheModel class Item extends CachedModel
{ {
/** /**
@ -698,9 +698,6 @@ class Item extends CacheModel
throw new \Exception('error fetching the data from the DB'); throw new \Exception('error fetching the data from the DB');
} }
// Load side libraries.
include_once $config['homedir'].'/include/functions_io.php';
// Clean up to two levels of HTML entities. // Clean up to two levels of HTML entities.
$row = \io_safe_output(\io_safe_output($row)); $row = \io_safe_output(\io_safe_output($row));
@ -732,54 +729,56 @@ class Item extends CacheModel
* @return array The Visual Console Item data structure stored into the DB. * @return array The Visual Console Item data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB. * @throws \Exception When the data cannot be retrieved from the DB.
* *
* @override CacheModel::fetchCachedData. * @override CachedModel::fetchCachedData.
*/ */
protected static function fetchCachedData(array $filter) protected static function fetchCachedData(array $filter)
{ {
global $config; global $config;
$row = \db_get_row_sql( $sql = sprintf(
'SELECT * FROM tvisual_console_elements_cache 'SELECT `data`
WHERE vc_item_id = '.$filter['id'].' FROM `tvisual_console_elements_cache`
AND vc_id = '.$filter['id_layout'].' WHERE `vc_item_id` = %d
AND user_id LIKE "'.$config['id_user'].'" AND `vc_id` = %d
AND (UNIX_TIMESTAMP(`created_at`) + `expiration`) > UNIX_TIMESTAMP()' AND `user_id` LIKE \'%s\'
AND (UNIX_TIMESTAMP(`created_at`) + `expiration`) > UNIX_TIMESTAMP()',
$filter['id'],
$filter['id_layout'],
$config['id_user']
); );
$data = \db_get_value_sql($sql);
if ($row === false) { if ($data === false) {
return null; return null;
} }
$row = \io_safe_output(\io_safe_output($row)); return json_decode(base64_decode($data), true);
return $row;
} }
/** /**
* Stores the data structure obtained. * Stores the data structure obtained.
* *
* @param array $filter Filter to retrieve the modeled element. * @param array $filter Filter to save the modeled element.
* @param array $data Modeled element to save.
* *
* @return boolean The modeled element data structure stored into the DB. * @return boolean The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB. * @throws \Exception When the data cannot be retrieved from the DB.
* *
* @override CacheModel::saveCachedData. * @override CachedModel::saveCachedData.
*/ */
protected static function saveCachedData(array $filter, array $data): bool protected static function saveCachedData(array $filter, array $data): bool
{ {
$result = \db_process_sql_insert( return \db_process_sql_insert(
'tvisual_console_elements_cache', 'tvisual_console_elements_cache',
[ [
'vc_id' => $filter['vc_id'], 'vc_id' => $filter['vc_id'],
'vc_item_id' => $filter['vc_item_id'], 'vc_item_id' => $filter['vc_item_id'],
'user_id' => $filter['user_id'], 'user_id' => $filter['user_id'],
'data' => \base64_encode(\json_encode(\io_safe_input($data))), 'data' => base64_encode(json_encode($data)),
'expiration' => $filter['expiration'], 'expiration' => $filter['expiration'],
] ]
); ) > 0;
return ($result > 0) ? true : false;
} }
@ -791,11 +790,14 @@ class Item extends CacheModel
* @return array The modeled element data structure stored into the DB. * @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB. * @throws \Exception When the data cannot be retrieved from the DB.
* *
* @override CacheModel::clearCachedData. * @override CachedModel::clearCachedData.
*/ */
protected static function clearCachedData(array $filter): int protected static function clearCachedData(array $filter): int
{ {
return \db_process_sql_delete('tvisual_console_elements_cache', $filter); return \db_process_sql_delete(
'tvisual_console_elements_cache',
$filter
);
} }