Popraw obsługę błędów i optymalizację połączeń z Redis w różnych klasach
This commit is contained in:
@@ -112,41 +112,50 @@ class Product implements \ArrayAccess
|
||||
* @param string $permutation_hash The permutation hash of the product.
|
||||
* @return \shop\Product The product object.
|
||||
*/
|
||||
public static function getFromCache( $product_id, $lang_id, $permutation_hash = null )
|
||||
public static function getFromCache($product_id, $lang_id, $permutation_hash = null)
|
||||
{
|
||||
// Check if Redis extension is loaded
|
||||
if ( class_exists( 'Redis' ) )
|
||||
if (class_exists('Redis'))
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get the Redis connection instance
|
||||
$redis = \RedisConnection::getInstance() -> getConnection();
|
||||
$redis = \RedisConnection::getInstance()->getConnection();
|
||||
|
||||
// Try to retrieve the serialized product object from cache
|
||||
$objectData = $redis -> get( "shop\product:$product_id:$lang_id:$permutation_hash" );
|
||||
|
||||
if ( !$objectData )
|
||||
// Check if Redis connection is valid
|
||||
if ($redis)
|
||||
{
|
||||
// Product not found in cache, create a new instance and store it in cache
|
||||
$object = new self( $product_id, $lang_id, $permutation_hash );
|
||||
$redis->setex( "shop\product:$product_id:$lang_id:$permutation_hash", 60 * 60 * 24, serialize( $object ) );
|
||||
// Try to retrieve the serialized product object from cache
|
||||
$objectData = $redis->get("shop\product:$product_id:$lang_id:$permutation_hash");
|
||||
|
||||
if (!$objectData)
|
||||
{
|
||||
// Product not found in cache, create a new instance and store it in cache
|
||||
$object = new self($product_id, $lang_id, $permutation_hash);
|
||||
$redis->setex("shop\product:$product_id:$lang_id:$permutation_hash", 60 * 60 * 24, serialize($object));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Product found in cache, unserialize it
|
||||
$object = unserialize($objectData);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Product found in cache, unserialize it
|
||||
$object = unserialize( $objectData );
|
||||
// Redis connection failed, create a new instance
|
||||
$object = new self($product_id, $lang_id, $permutation_hash);
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch (\Exception $e)
|
||||
{
|
||||
// Log the exception if needed
|
||||
$object = new self( $product_id, $lang_id, $permutation_hash );
|
||||
$object = new self($product_id, $lang_id, $permutation_hash);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Redis extension not loaded, create a new instance
|
||||
$object = new self( $product_id, $lang_id, $permutation_hash );
|
||||
$object = new self($product_id, $lang_id, $permutation_hash);
|
||||
}
|
||||
|
||||
return $object;
|
||||
|
||||
Reference in New Issue
Block a user