Added expiration and deleted old cache data in pandora_db

This commit is contained in:
Daniel Maya 2019-05-23 10:35:03 +02:00
parent 5c0a197b6b
commit 2de2d1f482
6 changed files with 18 additions and 10 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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,

View File

@ -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()");
}
###############################################################################