Sprint 1.1: Cache-System in Tools.php implementiert - enableCache, restoreCacheSettings, clearCache, clearCompile, clearSmartyCache, clearSf2Cache, clearAllCache und Hilfsfunktionen hinzugefügt

This commit is contained in:
thomas 2025-07-06 20:26:39 +02:00
parent 5dc4841227
commit 37d18df50c
2 changed files with 240 additions and 8 deletions

View File

@ -2,11 +2,11 @@
## Milestone 1: Core-System Erweiterung (Sprint 1.1-1.3) ## Milestone 1: Core-System Erweiterung (Sprint 1.1-1.3)
### Sprint 1.1: Tools.php Erweiterung (50% abgeschlossen) ### Sprint 1.1: Tools.php Erweiterung (75% abgeschlossen)
- [x] Security-Funktionen (hash, getToken, AdminToken, String-Operationen, Utility-Funktionen) - [x] Security-Funktionen (hash, getToken, AdminToken, String-Operationen, Utility-Funktionen)
- [x] File-Operationen (deleteDirectory, file_get_contents, copy, scandir, etc.) - [x] File-Operationen (deleteDirectory, file_get_contents, copy, scandir, etc.)
- [x] Math-Funktionen (ps_round, math_round, round_helper, ceilf, floorf, spreadAmount) - [x] Math-Funktionen (ps_round, math_round, round_helper, ceilf, floorf, spreadAmount)
- [ ] Cache-System Erweiterung - [x] Cache-System Erweiterung (enableCache, restoreCacheSettings, clearCache, clearCompile, clearSmartyCache, clearSf2Cache, clearAllCache, getMemoryLimit, getOctets, isX86_64arch, isPHPCLI, argvToGET, getMaxUploadSize, convertBytes)
- [ ] Context.php Erweiterung - [ ] Context.php Erweiterung
- [ ] Cart.php Erweiterung - [ ] Cart.php Erweiterung
@ -122,11 +122,10 @@
- [ ] Monitoring Setup - [ ] Monitoring Setup
- [ ] Backup Setup - [ ] Backup Setup
## Gesamtfortschritt: 8% (1 von 18 Sprints abgeschlossen) ## Gesamtfortschritt: 11% (1 von 18 Sprints zu 75% abgeschlossen)
### Nächste Schritte: ### Nächste Schritte:
1. Cache-System Erweiterung in Tools.php 1. Context.php Erweiterung
2. Context.php Erweiterung 2. Cart.php Erweiterung
3. Cart.php Erweiterung 3. Sprint 1.1 abschließen
4. Sprint 1.1 abschließen 4. Sprint 1.2 beginnen (Datenbank & ORM)
5. Sprint 1.2 beginnen (Datenbank & ORM)

View File

@ -1183,4 +1183,237 @@ class Tools
$rows[0][$column] += $diff; $rows[0][$column] += $diff;
} }
} }
/**
* Enable cache for Smarty
*
* @param int $level
* @param Context|null $context
*/
public static function enableCache($level = 1, ?Context $context = null)
{
if (!$context) {
$context = Context::getContext();
}
$smarty = $context->smarty;
if (!Configuration::get('PS_SMARTY_CACHE')) {
return;
}
if ($smarty->force_compile == 0 && $smarty->caching == $level) {
return;
}
self::$_forceCompile = (int) $smarty->force_compile;
self::$_caching = (int) $smarty->caching;
$smarty->force_compile = false;
$smarty->caching = (int) $level;
$smarty->cache_lifetime = 31536000; // 1 Year
}
/**
* Restore cache settings
*
* @param Context|null $context
*/
public static function restoreCacheSettings(?Context $context = null)
{
if (!$context) {
$context = Context::getContext();
}
if (isset(self::$_forceCompile)) {
$context->smarty->force_compile = (bool) self::$_forceCompile;
}
if (isset(self::$_caching)) {
$context->smarty->caching = (int) self::$_caching;
}
}
/**
* Clear cache for Smarty
*
* @param Smarty|null $smarty
* @param bool|string $tpl
* @param string|null $cache_id
* @param string|null $compile_id
* @return int|null
*/
public static function clearCache($smarty = null, $tpl = false, $cache_id = null, $compile_id = null)
{
if ($smarty === null) {
$smarty = Context::getContext()->smarty;
}
if ($smarty === null) {
return null;
}
if (!$tpl && $cache_id === null && $compile_id === null) {
return $smarty->clearAllCache();
}
$ret = $smarty->clearCache($tpl, $cache_id, $compile_id);
Hook::exec('actionClearCache');
return $ret;
}
/**
* Clear compile for Smarty
*
* @param Smarty|null $smarty
* @return int|null
*/
public static function clearCompile($smarty = null)
{
if ($smarty === null) {
$smarty = Context::getContext()->smarty;
}
if ($smarty === null) {
return null;
}
$ret = $smarty->clearCompiledTemplate();
Hook::exec('actionClearCompileCache');
return $ret;
}
/**
* Clear Smarty cache and compile folders
*/
public static function clearSmartyCache()
{
$smarty = Context::getContext()->smarty;
self::clearCache($smarty);
self::clearCompile($smarty);
}
/**
* Clear Symfony cache
*
* @param string|null $env
*/
public static function clearSf2Cache($env = null)
{
if (null === $env) {
$env = _PS_ENV_;
}
$dir = _PS_ROOT_DIR_ . '/var/cache/' . $env . '/';
register_shutdown_function(function () use ($dir) {
$fs = new Filesystem();
$fs->remove($dir);
Hook::exec('actionClearSf2Cache');
});
}
/**
* Clear both Smarty and Symfony cache
*/
public static function clearAllCache()
{
self::clearSmartyCache();
self::clearSf2Cache();
}
/**
* Get memory limit in octets
*
* @return int|string
*/
public static function getMemoryLimit()
{
$memory_limit = @ini_get('memory_limit');
return self::getOctets($memory_limit);
}
/**
* Get octets from configuration option
*
* @param string $option
* @return int|string
*/
public static function getOctets($option)
{
if (preg_match('/[0-9]+k/i', $option)) {
return 1024 * (int) $option;
}
if (preg_match('/[0-9]+m/i', $option)) {
return 1024 * 1024 * (int) $option;
}
if (preg_match('/[0-9]+g/i', $option)) {
return 1024 * 1024 * 1024 * (int) $option;
}
return $option;
}
/**
* Check if server uses 64bit architecture
*
* @return bool
*/
public static function isX86_64arch()
{
return PHP_INT_MAX == '9223372036854775807';
}
/**
* Check if php-cli is used
*
* @return bool
*/
public static function isPHPCLI()
{
return defined('STDIN') || (self::strtolower(PHP_SAPI) == 'cli' && (!isset($_SERVER['REMOTE_ADDR']) || empty($_SERVER['REMOTE_ADDR'])));
}
/**
* Convert argv to GET parameters
*
* @param int $argc
* @param array $argv
*/
public static function argvToGET($argc, $argv)
{
if ($argc <= 1) {
return;
}
parse_str($argv[1], $args);
if (!is_array($args) || !count($args)) {
return;
}
$_GET = array_merge($args, $_GET);
$_SERVER['QUERY_STRING'] = $argv[1];
}
/**
* Get max file upload size
*
* @param int $max_size
* @return int
*/
public static function getMaxUploadSize($max_size = 0)
{
$values = [self::convertBytes(ini_get('upload_max_filesize'))];
if ($max_size > 0) {
$values[] = $max_size;
}
$post_max_size = self::convertBytes(ini_get('post_max_size'));
if ($post_max_size > 0) {
$values[] = $post_max_size;
}
return min($values);
}
} }