Hallo an das Forum.
Wir verwenden den neuen Shop v3.1.0 rev 16069 und Redis. Da wir unsere Produkte mehrfach am Tag updaten habe ich ein Smarty-Funktion geschrieben um ein Produkt aus dem Cache zu entfernen und die Veränderung sofort sichtbar zu machen. Es kommt bei uns sehr oft vor, das wir Lieferzeiten anpassen müssen, was leider durch den Cache erst nach einer gewissen Zeit angezeigt wird. Es gibt nichts ärgerlicheres als ein Produkt mit einer falschen Lieferzeit anzubieten und da ich kein Freund bin in Systemdateien einzugreifen(nur ärger bei späteren Updates) finde ich die Smary-Funktion wirklich ein feines Werkzeug.
Hier mal meine Idee zur Lösung:
function.invalidateProductCache.php
<?phpfunction smarty_function_invalidateProductCache
($param, $smarty) { // Connect to Redis /*
try {
$redis = new Redis();
$redis->connect(REDIS_SERVER, REDIS_PORT, REDIS_TIMEOUT);
$redis->select(REDIS_DB);
// List all keys in Redis
$allKeys = $redis->keys('*'); // Fetch all keys
echo "<div>Current Redis Cache Contents:</div><ul>";
foreach ($allKeys as $key) {
$value = $redis->get($key); // Get the value for the key
echo "<li>Key: {$key}, Value: {$value}</li>";
}
echo "</ul>";
} catch (Exception $e) {
// Handle Redis connection errors
echo "<div>Redis connection failed: " . $e->getMessage() . "</div>";
}
*/ // Check if PRODUCTS_ID is passed if (!isset($param['PRODUCTS_ID'])) { return; // No product ID, exit the function } $productId = (int
)$param['PRODUCTS_ID']; // Get the last modified date from the products table $dbQuery_last_modified = xtDBquery
("SELECT products_last_modified FROM products WHERE products_id='" . $productId . "'"); $lastModified = null; while ($dbQueryResult_last_modified = xtc_db_fetch_array
($dbQuery_last_modified, true)) { $lastModified = $dbQueryResult_last_modified['products_last_modified']; } // Debug: Output the last modified date //echo "<div>Last modified date for product ID {$productId}: {$lastModified}</div>"; // Ensure lastModified was fetched correctly if (!isset($lastModified)) { return; // If no last_modified date, exit the function } // Convert the last modified date to a timestamp $lastModifiedTime = strtotime($lastModified); // Get the current time $currentTime = time(); // Check if the product was modified in the last hour (3600 seconds) if (($currentTime - $lastModifiedTime) <= 3600) { // Include the modified_cache class require_once(DIR_FS_CATALOG
. 'includes/classes/modified_cache.php'); // Get the instance of modified_cache $cache = modified_cache
::getInstance(); // Construct the cache key for the product $cacheKey = 'cp_' . $productId; // Set the cache item for the product $cache->setId($cacheKey); // Delete the cache item $cache->clear($cacheKey); // Check if the cache was successfully deleted $item = $cache->getId(); if ($item === null) { echo "<script>window.location.reload();</script>"; } }}?> Aufruf in der jeweiligen product_info html: {invalidateProductCache PRODUCTS_ID={$PRODUCTS_ID}}
Wir haben im Backend die Lebenszeit auf 3600 gestellt.
Ich habe das in meinen Testshop implementiert und es scheint gut zu laufen. Das Product wird nach einer Änderung innerhalb des Zeitraumes aus dem Cache entfernt und das Produkt wird sofort aktualisiert. So wie ich weiterhin sehe bleiben alle anderen Einträge dem Cache erhalten.
Rundum kurz meine Frage, bevor ich mein Entwurf in den laufenden Betrieb übernehme. Seht ihr hier irgendwelche Schwachstellen oder Auswirkungen an die ich vielleicht nicht gedacht habe.
Danke für euer Feedback.
LG Oliver
Linkback: https://www.modified-shop.org/forum/index.php?topic=43533.0