diff --git a/pandora_console/include/rest-api/models/CachedModel.php b/pandora_console/include/rest-api/models/CachedModel.php index ce7543e32a..e4cf65b15a 100644 --- a/pandora_console/include/rest-api/models/CachedModel.php +++ b/pandora_console/include/rest-api/models/CachedModel.php @@ -12,6 +12,13 @@ use Models\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. @@ -73,12 +80,14 @@ abstract class CachedModel extends Model // Obtain the item's data from cache. $cachedData = static::fetchCachedData($filter); if ($cachedData === null) { + $userId = (static::$indexCacheByUser === true) ? $config['id_user'] : null; + // Delete expired data cache. static::clearCachedData( [ 'vc_item_id' => $filter['id'], 'vc_id' => $filter['id_layout'], - 'user_id' => $config['id_user'], + 'user_id' => $userId, ] ); // Obtain the item's data from the database. @@ -88,7 +97,7 @@ abstract class CachedModel extends Model [ 'vc_item_id' => $filter['id'], 'vc_id' => $filter['id_layout'], - 'user_id' => $config['id_user'], + 'user_id' => $userId, 'expiration' => $filter['cache_expiration'], ], $data diff --git a/pandora_console/include/rest-api/models/VisualConsole/Item.php b/pandora_console/include/rest-api/models/VisualConsole/Item.php index cb629d3885..617b48e517 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Item.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Item.php @@ -735,18 +735,21 @@ class Item extends CachedModel { global $config; - $sql = sprintf( - 'SELECT `data` - FROM `tvisual_console_elements_cache` - WHERE `vc_item_id` = %d - AND `vc_id` = %d - AND `user_id` LIKE \'%s\' - AND (UNIX_TIMESTAMP(`created_at`) + `expiration`) > UNIX_TIMESTAMP()', - $filter['id'], - $filter['id_layout'], - $config['id_user'] + $filter = [ + 'vc_id' => (int) $filter['id_layout'], + 'vc_item_id' => (int) $filter['id'], + '(UNIX_TIMESTAMP(`created_at`) + `expiration`) > UNIX_TIMESTAMP()' + ]; + + if (static::$indexCacheByUser === true) { + $filter['user_id'] = $config['id_user']; + } + + $data = \db_get_value_filter( + 'data', + 'tvisual_console_elements_cache', + $filter ); - $data = \db_get_value_sql($sql); if ($data === false) { return null; diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php b/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php index 608e98bf01..e627003244 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php @@ -26,6 +26,13 @@ final class Group extends Item */ protected static $useHtmlOutput = true; + /** + * Enable the cache index by user id. + * + * @var boolean + */ + protected static $indexCacheByUser = true; + /** * Returns a valid representation of the model.