pandorafms/pandora_console/include/rest-api/models/CachedModel.php

118 lines
3.4 KiB
PHP
Raw Normal View History

2019-05-20 15:15:54 +02:00
<?php
declare(strict_types=1);
namespace Models;
use Models\Model;
/**
* This class should be extended to add functionalities to
* fetch, clear and save item cache.
*/
2019-05-27 12:10:05 +02:00
abstract class CachedModel extends Model
2019-05-20 15:15:54 +02:00
{
/**
* Used to decide if the cache should also be indexed by user or not.
*
* @var boolean
*/
protected static $indexCacheByUser = false;
2019-05-20 15:15:54 +02:00
/**
* Obtain a data structure from the database using a filter.
*
* @param array $filter Filter to retrieve the modeled element.
*
* @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
*
* @abstract
*/
abstract protected static function fetchCachedData(array $filter);
/**
* Stores the data structure obtained.
*
* @param array $filter Filter to retrieve the modeled element.
* @param array $data Data to store in cache.
2019-05-20 15:15:54 +02:00
*
* @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
*
* @abstract
*/
2019-05-27 12:10:05 +02:00
abstract protected static function saveCachedData(
array $filter,
array $data
): bool;
2019-05-20 15:15:54 +02:00
/**
* Deletes previous data that are not useful.
*
* @param array $filter Filter to retrieve the modeled element.
*
* @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
*
* @abstract
*/
abstract protected static function clearCachedData(array $filter): int;
/**
* Obtain a model's instance from the database using a filter.
*
* @param array $filter Filter to retrieve the modeled element.
*
* @return self A modeled element's instance.
*
* @overrides Model::fromDB.
*/
public static function fromDB(array $filter): Model
{
global $config;
// TODO: Remove references to the VC items. This class should be usable with any resource.
2019-05-27 12:10:05 +02:00
if ($filter['cache_expiration'] > 0) {
2019-05-20 15:15:54 +02:00
// Obtain the item's data from cache.
2019-05-27 12:10:05 +02:00
$cachedData = static::fetchCachedData($filter);
if ($cachedData === null) {
$userId = (static::$indexCacheByUser === true) ? $config['id_user'] : null;
2019-05-20 15:15:54 +02:00
// Delete expired data cache.
static::clearCachedData(
[
'vc_item_id' => $filter['id'],
'vc_id' => $filter['id_layout'],
'user_id' => $userId,
2019-05-20 15:15:54 +02:00
]
);
// Obtain the item's data from the database.
$data = static::fetchDataFromDB($filter);
// Save the item's data in cache.
static::saveCachedData(
[
'vc_item_id' => $filter['id'],
'vc_id' => $filter['id_layout'],
'user_id' => $userId,
'expiration' => $filter['cache_expiration'],
2019-05-20 15:15:54 +02:00
],
$data
);
} else {
2019-05-27 12:10:05 +02:00
$data = $cachedData;
2019-05-20 15:15:54 +02:00
}
} else {
$data = static::fetchDataFromDB($filter);
2019-05-20 15:15:54 +02:00
}
return static::fromArray($data);
2019-05-20 15:15:54 +02:00
}
}