134 lines
3.3 KiB
PHP
134 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* TNT OFFICIAL MODULE FOR PRESTASHOP
|
|
*
|
|
* @author GFI Informatique <www.gfi.world>
|
|
* @copyright 2016-2019 GFI Informatique, 2016-2019 TNT
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
*/
|
|
|
|
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_ClassLoader.php';
|
|
|
|
class TNTOfficiel_Cache
|
|
{
|
|
/**
|
|
* Prevent Construct.
|
|
*/
|
|
final private function __construct()
|
|
{
|
|
trigger_error(sprintf('%s() %s is static.', __FUNCTION__, get_class($this)), E_USER_ERROR);
|
|
}
|
|
|
|
/**
|
|
* Get a key identifier.
|
|
*
|
|
* @param string $strArgFQClassName
|
|
* @param string $strFunctionName
|
|
* @param array $arrArgParameters
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function getKeyIdentifier($strArgFQClassName, $strFunctionName, $arrArgParameters)
|
|
{
|
|
$strCNLS = strrchr($strArgFQClassName, '\\');
|
|
$strUQCN = ($strCNLS === false ? $strArgFQClassName : substr($strCNLS, 1));
|
|
|
|
$strCacheKey = $strUQCN.'::'.$strFunctionName.'_'.md5(serialize($arrArgParameters));
|
|
|
|
return $strCacheKey;
|
|
}
|
|
|
|
/**
|
|
* Check if data is stored in cache from a key identifier.
|
|
*
|
|
* @param string $strArgKey
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function isStored($strArgKey)
|
|
{
|
|
if (!is_string($strArgKey)) {
|
|
return false;
|
|
}
|
|
|
|
$boolExistL1 = Cache::isStored($strArgKey);
|
|
$boolExistL2 = false;
|
|
|
|
if (_PS_CACHE_ENABLED_ && !$boolExistL1) {
|
|
$objCache = Cache::getInstance();
|
|
$boolExistL2 = $objCache->exists($strArgKey);
|
|
}
|
|
|
|
return $boolExistL1 || $boolExistL2;
|
|
}
|
|
|
|
/**
|
|
* Store data in cache using a key identifier.
|
|
*
|
|
* @param string $strArgKey
|
|
* @param mixed $mxdArgValue
|
|
* @param int $intArgTTL
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function store($strArgKey, $mxdArgValue, $intArgTTL)
|
|
{
|
|
if (!is_string($strArgKey)) {
|
|
return false;
|
|
}
|
|
// No data, no store.
|
|
if ($mxdArgValue === null) {
|
|
return false;
|
|
}
|
|
|
|
$boolSetL1 = Cache::store($strArgKey, $mxdArgValue);
|
|
$boolSetL2 = true;
|
|
|
|
if (_PS_CACHE_ENABLED_) {
|
|
$objCache = Cache::getInstance();
|
|
$boolSetL2 = $objCache->set($strArgKey, $mxdArgValue, $intArgTTL);
|
|
}
|
|
|
|
return $boolSetL1 && $boolSetL2;
|
|
}
|
|
|
|
/**
|
|
* Retrieve data from cache using a key identifier.
|
|
*
|
|
* @param string $strArgKey
|
|
*
|
|
* @return mixed|null
|
|
*/
|
|
public static function retrieve($strArgKey)
|
|
{
|
|
if (!is_string($strArgKey)) {
|
|
return null;
|
|
}
|
|
|
|
if (Cache::isStored($strArgKey)) {
|
|
return Cache::retrieve($strArgKey);
|
|
} elseif (_PS_CACHE_ENABLED_) {
|
|
$objCache = Cache::getInstance();
|
|
if ($objCache->exists($strArgKey)) {
|
|
return $objCache->get($strArgKey);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Gets the number of seconds between the current time and next midnight.
|
|
*
|
|
* @return int
|
|
*/
|
|
public static function getSecondsUntilMidnight()
|
|
{
|
|
$intTSNow = time();
|
|
$intTSMidnight = strtotime('tomorrow midnight');
|
|
$intSecondsUntilMidnight = $intTSMidnight - $intTSNow;
|
|
|
|
return $intSecondsUntilMidnight;
|
|
}
|
|
}
|