Now only the Group item of the VC is cached by user

This commit is contained in:
Alejandro Gallardo Escobar 2019-05-28 10:38:58 +02:00
parent fb8bc48d64
commit 5ea79cdfb8
3 changed files with 32 additions and 13 deletions

View File

@ -12,6 +12,13 @@ use Models\Model;
abstract class CachedModel extends Model abstract class CachedModel extends Model
{ {
/**
* Used to decide if the cache should also be indexed by user or not.
*
* @var boolean
*/
protected static $indexCacheByUser = false;
/** /**
* Obtain a data structure from the database using a filter. * Obtain a data structure from the database using a filter.
@ -73,12 +80,14 @@ abstract class CachedModel extends Model
// Obtain the item's data from cache. // Obtain the item's data from cache.
$cachedData = static::fetchCachedData($filter); $cachedData = static::fetchCachedData($filter);
if ($cachedData === null) { if ($cachedData === null) {
$userId = (static::$indexCacheByUser === true) ? $config['id_user'] : null;
// Delete expired data cache. // Delete expired data cache.
static::clearCachedData( static::clearCachedData(
[ [
'vc_item_id' => $filter['id'], 'vc_item_id' => $filter['id'],
'vc_id' => $filter['id_layout'], 'vc_id' => $filter['id_layout'],
'user_id' => $config['id_user'], 'user_id' => $userId,
] ]
); );
// Obtain the item's data from the database. // Obtain the item's data from the database.
@ -88,7 +97,7 @@ abstract class CachedModel extends Model
[ [
'vc_item_id' => $filter['id'], 'vc_item_id' => $filter['id'],
'vc_id' => $filter['id_layout'], 'vc_id' => $filter['id_layout'],
'user_id' => $config['id_user'], 'user_id' => $userId,
'expiration' => $filter['cache_expiration'], 'expiration' => $filter['cache_expiration'],
], ],
$data $data

View File

@ -735,18 +735,21 @@ class Item extends CachedModel
{ {
global $config; global $config;
$sql = sprintf( $filter = [
'SELECT `data` 'vc_id' => (int) $filter['id_layout'],
FROM `tvisual_console_elements_cache` 'vc_item_id' => (int) $filter['id'],
WHERE `vc_item_id` = %d '(UNIX_TIMESTAMP(`created_at`) + `expiration`) > UNIX_TIMESTAMP()'
AND `vc_id` = %d ];
AND `user_id` LIKE \'%s\'
AND (UNIX_TIMESTAMP(`created_at`) + `expiration`) > UNIX_TIMESTAMP()', if (static::$indexCacheByUser === true) {
$filter['id'], $filter['user_id'] = $config['id_user'];
$filter['id_layout'], }
$config['id_user']
$data = \db_get_value_filter(
'data',
'tvisual_console_elements_cache',
$filter
); );
$data = \db_get_value_sql($sql);
if ($data === false) { if ($data === false) {
return null; return null;

View File

@ -26,6 +26,13 @@ final class Group extends Item
*/ */
protected static $useHtmlOutput = true; protected static $useHtmlOutput = true;
/**
* Enable the cache index by user id.
*
* @var boolean
*/
protected static $indexCacheByUser = true;
/** /**
* Returns a valid representation of the model. * Returns a valid representation of the model.