From 2de2d1f48236431e2db28172ff8b14ace65316af Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Thu, 23 May 2019 10:35:03 +0200 Subject: [PATCH] Added expiration and deleted old cache data in pandora_db --- pandora_console/extras/mr/28.sql | 1 + .../extras/pandoradb_migrate_6.0_to_7.0.mysql.sql | 1 + .../include/rest-api/models/CacheModel.php | 8 +++++--- .../include/rest-api/models/VisualConsole/Item.php | 13 ++++++------- pandora_console/pandoradb.sql | 1 + pandora_server/util/pandora_db.pl | 4 ++++ 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pandora_console/extras/mr/28.sql b/pandora_console/extras/mr/28.sql index e7dccba2b3..bb09cc7173 100644 --- a/pandora_console/extras/mr/28.sql +++ b/pandora_console/extras/mr/28.sql @@ -12,6 +12,7 @@ CREATE TABLE IF NOT EXISTS `tvisual_console_elements_cache` ( `user_id` VARCHAR(60) DEFAULT NULL, `data` TEXT NOT NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `expiration` INTEGER UNSIGNED NOT NULL COMMENT 'Seconds to expire', PRIMARY KEY(`id`), FOREIGN KEY(`vc_id`) REFERENCES `tlayout`(`id`) ON DELETE CASCADE, diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql index e7b57b4c27..c99ae39303 100644 --- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql +++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql @@ -2115,6 +2115,7 @@ CREATE TABLE `tvisual_console_elements_cache` ( `user_id` VARCHAR(60) DEFAULT NULL, `data` TEXT NOT NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `expiration` INTEGER UNSIGNED NOT NULL COMMENT 'Seconds to expire', PRIMARY KEY(`id`), FOREIGN KEY(`vc_id`) REFERENCES `tlayout`(`id`) ON DELETE CASCADE, diff --git a/pandora_console/include/rest-api/models/CacheModel.php b/pandora_console/include/rest-api/models/CacheModel.php index b6c6fb9155..35a55e7996 100644 --- a/pandora_console/include/rest-api/models/CacheModel.php +++ b/pandora_console/include/rest-api/models/CacheModel.php @@ -30,6 +30,7 @@ abstract class CacheModel extends Model * Stores the data structure obtained. * * @param array $filter Filter to retrieve the modeled element. + * @param array $data Data to store in cache. * * @return array The modeled element data structure stored into the DB. * @throws \Exception When the data cannot be retrieved from the DB. @@ -85,17 +86,18 @@ abstract class CacheModel extends Model 'vc_item_id' => $filter['id'], 'vc_id' => $filter['id_layout'], 'user_id' => $config['id_user'], + 'expiration' => $filter['cache_expiration'], ], $data ); - return static::fromArray($data); } else { $data = \io_safe_output(\json_decode(\base64_decode($cacheData['data']), true)); - return static::fromArray($data); } } else { - return static::fromArray(static::fetchDataFromDB($filter)); + $data = static::fetchDataFromDB($filter); } + + return static::fromArray($data); } diff --git a/pandora_console/include/rest-api/models/VisualConsole/Item.php b/pandora_console/include/rest-api/models/VisualConsole/Item.php index f3386b7126..4fb40a5aba 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Item.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Item.php @@ -743,14 +743,13 @@ class Item extends CacheModel WHERE vc_item_id = '.$filter['id'].' AND vc_id = '.$filter['id_layout'].' AND user_id LIKE "'.$config['id_user'].'" - AND (UNIX_TIMESTAMP(`created_at`) +'.$filter['cache_expiration'].') > UNIX_TIMESTAMP()' + AND (UNIX_TIMESTAMP(`created_at`) + `expiration`) > UNIX_TIMESTAMP()' ); + if ($row === false) { return null; } - // $fecha = \date_create(); - // $fecha = \date_timestamp_get($fecha); $row = \io_safe_output(\io_safe_output($row)); return $row; @@ -762,14 +761,14 @@ class Item extends CacheModel * * @param array $filter Filter to retrieve the modeled element. * - * @return array 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. * * @override CacheModel::saveCachedData. */ protected static function saveCachedData(array $filter, array $data): bool { - db_process_sql_insert( + $result = \db_process_sql_insert( 'tvisual_console_elements_cache', [ 'vc_id' => $filter['vc_id'], @@ -779,7 +778,8 @@ class Item extends CacheModel 'expiration' => $filter['expiration'], ] ); - return false; + + return ($result > 0) ? true : false; } @@ -795,7 +795,6 @@ class Item extends CacheModel */ protected static function clearCachedData(array $filter): int { - hd($filter); return \db_process_sql_delete('tvisual_console_elements_cache', $filter); } diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index f03f4b3337..e74e7c7c18 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -3574,6 +3574,7 @@ CREATE TABLE IF NOT EXISTS `tvisual_console_elements_cache` ( `user_id` VARCHAR(60) DEFAULT NULL, `data` TEXT NOT NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `expiration` INTEGER UNSIGNED NOT NULL COMMENT 'Seconds to expire', PRIMARY KEY(`id`), FOREIGN KEY(`vc_id`) REFERENCES `tlayout`(`id`) ON DELETE CASCADE, diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index 881f73940b..4044f94c3e 100644 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -446,6 +446,10 @@ sub pandora_purgedb ($$) { my $message_limit = time() - 86400 * $conf->{'_delete_old_messages'}; db_do ($dbh, "DELETE FROM tmensajes WHERE timestamp < ?", $message_limit); } + + # Delete old cache data + log_message ('PURGE', "Deleting old cache data."); + db_do ($dbh, "DELETE FROM `tvisual_console_elements_cache` WHERE (UNIX_TIMESTAMP(`created_at`) + `expiration`) < UNIX_TIMESTAMP()"); } ###############################################################################