Popraw obsługę błędów i optymalizację połączeń z Redis w różnych klasach

This commit is contained in:
2024-11-08 00:16:19 +01:00
parent fc35061406
commit e3b4ec973b
12 changed files with 101 additions and 54 deletions

View File

@@ -2,7 +2,8 @@
global $gdb;
foreach ( $this -> apilo_payment_types_list as $payment_type )
{
$payment_types[ $payment_type['id'] ] = $payment_type['name'];
if ( isset( $payment_type['name'] ) && isset( $payment_type['id'] ) )
$payment_types[ $payment_type['id'] ] = $payment_type['name'];
}
// sellasist payment methods
$sellasist_payment_types_list = [];

BIN
autoload/.DS_Store vendored

Binary file not shown.

View File

@@ -1,30 +1,54 @@
<?
class RedisConnection {
class RedisConnection
{
private static $instance = null;
private $redis;
private function __construct() {
private function __construct()
{
global $config;
$this -> redis = new \Redis();
if ( !$this -> redis -> connect( $config['redis']['host'], $config['redis']['port'] ) ) {
throw new Exception("Nie udało się połączyć z serwerem Redis.");
$this->redis = new \Redis();
try
{
// Próba połączenia z serwerem Redis
if (!$this->redis->connect($config['redis']['host'], $config['redis']['port']))
{
// Logowanie błędu lub inna forma obsługi, zamiast rzucania wyjątku
error_log("Nie udało się połączyć z serwerem Redis.");
$this->redis = null; // Ustawienie na null, aby uniknąć błędów w przyszłości
return; // Wyjście z konstruktora, nie kontynuując autoryzacji
}
// Autentykacja za pomocą hasła
if ( !$this -> redis -> auth( $config['redis']['password'] ) ) {
// Próba autoryzacji
if (!$this->redis->auth($config['redis']['password']))
{
error_log("Autoryzacja do serwera Redis nie powiodła się.");
$this->redis = null; // Ustawienie na null, aby uniknąć błędów w przyszłości
return; // Wyjście z konstruktora
}
}
catch (\Exception $e)
{
// Obsługa innych potencjalnych wyjątków
error_log("Błąd podczas połączenia z Redis: " . $e->getMessage());
$this->redis = null; // Ustawienie na null, aby uniknąć błędów w przyszłości
}
}
public static function getInstance() {
if ( self::$instance === null ) {
public static function getInstance()
{
if (self::$instance === null)
{
self::$instance = new self();
}
return self::$instance;
}
public function getConnection() {
public function getConnection()
{
return $this->redis;
}
}
}

Binary file not shown.

View File

@@ -362,7 +362,8 @@ class ShopBasket
\S::set_session( 'ekomi-purchase', true );
$redis = \RedisConnection::getInstance() -> getConnection();
$redis -> flushAll();
if ( $redis )
$redis -> flushAll();
header( 'Location: /zamowienie/' . \front\factory\ShopOrder::order_hash( $order_id ) );
exit;

View File

@@ -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;

View File

@@ -20,16 +20,25 @@ class ProductCustomField implements \ArrayAccess
try
{
$redis = \RedisConnection::getInstance() -> getConnection();
$objectData = $redis -> get( "shop\ProductCustomField:$custom_field_id" );
if ( !$objectData )
if ( $redis )
{
$object = new self( $custom_field_id );
$redis -> setex( "shop\ProductCustomField:$custom_field_id", 60 * 60 * 24, serialize( $object ) );
$objectData = $redis -> get( "shop\ProductCustomField:$custom_field_id" );
if ( !$objectData )
{
$object = new self( $custom_field_id );
$redis -> setex( "shop\ProductCustomField:$custom_field_id", 60 * 60 * 24, serialize( $object ) );
}
else
{
$object = unserialize( $objectData );
}
}
else
{
$object = unserialize( $objectData );
// Log the error if needed
$object = new self( $custom_field_id );
}
}
catch ( \Exception $e )

View File

@@ -1,9 +1,11 @@
<?php
error_reporting( 0 );
require_once dirname( __FILE__ ) . '/config.php';
/* grid - zapisanie elementu */
if ( $_POST['a'] == 'gsave' )
{
$msg = '';
$grid = $_SESSION[ 'g' . $_POST[ 'gtable' ] ];
if ( is_a( $grid, 'grid' ) )
{

View File

@@ -722,7 +722,7 @@ class gdb
}
}
$this->exec('INSERT INTO "' . $table . '" (' . implode(', ', $columns) . ') VALUES (' . implode($values, ', ') . ')');
$this->exec('INSERT INTO "' . $table . '" (' . implode(', ', $columns) . ') VALUES (' . implode(', ',$values) . ')');
$lastId[] = $this->pdo->lastInsertId();
}

View File

@@ -145,7 +145,7 @@ class grid
'server' => $this -> gdb_opt['server'],
'username' => $this -> gdb_opt['username'],
'password' => $this -> gdb_opt['password'],
'port' => $this -> gdb_opt['port'],
'port' => isset( $this -> gdb_opt['port'] ) ? $this -> gdb_opt['port'] : 3306,
'charset' => 'utf8'
] );
}
@@ -437,6 +437,7 @@ class grid
public function getDataSummary()
{
$summary = [];
$where = self::getWhereCondition();
if ( is_array( $this -> summary ) ) foreach ( $this -> summary as $key )
@@ -511,7 +512,7 @@ class grid
public function saveElement( $values )
{
if ( !$values[ $this -> id ] )
if ( !isset( $values[ $this -> id ] ) or !$values[ $this -> id ] )
{
unset( $values[ $this -> id ] );
return $this -> connectToDb() -> insert( $this -> table, $values );

View File

@@ -631,7 +631,7 @@ jQuery( 'body' ).on( 'click', '#g-save, #g-edit-save', function()
data:
{
gtable: gtable,
values: JSON.stringify( values ),
values: JSON.stringify( formattedValues ),
a: 'gsave'
},
beforeSend: function()

View File

@@ -17,19 +17,19 @@
<label for="<?= $ce['db'];?>" class="col-lg-3 control-label"><?= $ce['name'];?>:</label>
<? if ( $ce['type'] == 'text' ):?>
<div class="col-lg-9">
<input type="text" class="form-control <? if ( $ce['require'] ):?>require<? endif;?>" name="<?= $ce['db'];?>" id="<?= $ce['db'];?>"
<?
if ( is_array( $ce['params'] ) ): foreach( $ce['params'] as $key => $val ):
echo $key . '="' . $val . '"';
<input type="text" class="form-control <? if ( isset( $ce['require'] ) and $ce['require'] ):?>require<? endif;?>" name="<?= $ce['db'];?>" id="<?= $ce['db'];?>"
<?
if ( isset( $ce['params'] ) and is_array( $ce['params'] ) ): foreach( $ce['params'] as $key => $val ):
echo $key . '="' . $val . '"';
endforeach; endif;
?> value="<?= htmlspecialchars( $this -> element[ $ce['db'] ] );?>" <? if ( $ce['readonly-edit'] and $this -> element[ $this -> values['id'] ] ):?>readonly="readonly"<? endif;?> />
</div>
<? elseif ( $ce['type'] == 'select' ):?>
<div class="col-lg-9">
<select name="<?= $ce['db'];?>" id="<?= $ce['db'];?>" class="form-control <? if ( $ce['require'] ):?>require<? endif;?>"
<?
if ( is_array( $ce['params'] ) ): foreach( $ce['params'] as $key => $val ):
echo $key . '="' . $val . '"';
<?
if ( is_array( $ce['params'] ) ): foreach( $ce['params'] as $key => $val ):
echo $key . '="' . $val . '"';
endforeach; endif;
?>>
<option value="null">---- <?= $ce['name'];?> ----</option>
@@ -38,20 +38,20 @@
$results = $_SESSION[ 'g' . $g_table ] -> connectToDb() -> query( $ce['replace']['sql'] ) -> fetchAll();
if ( is_array( $results ) ) foreach ( $results as $row )
{
echo '<option value="' . $row[0] . '"';
echo '<option value="' . $row[0] . '"';
if ( $this -> element[ $ce['db'] ] !== null and $row[0] == $this -> element[ $ce['db'] ] )
echo ' selected="selected" ';
echo '>' . $row[1] . '</option>';
}
?>
<? else:?>
<?
<?
if ( is_array( $ce['replace']['array'] ) ): foreach ( $ce['replace']['array'] as $key => $val ):
?>
<option value="<?= $key;?>"
<? if (
<option value="<?= $key;?>"
<? if (
( $this -> element[ $ce['db'] ] !== null and $key == $this -> element[ $ce['db'] ] )
or
or
( $this -> element[ $ce['db'] ] === null and isset( $ce['default_value'] ) and $ce['default_value'] == $key )
):?>
selected="selected"
@@ -59,7 +59,7 @@
>
<?= $val;?>
</option>
<?
<?
endforeach; endif;
?>
<? endif;?>
@@ -77,9 +77,9 @@
<? elseif ( $ce['type'] == 'textarea' ):?>
<div class="col col-lg-9">
<textarea name="<?= $ce['db'];?>" id="<?= $ce['db'];?>" class="form-control"
<?
if ( is_array( $ce['params'] ) ): foreach( $ce['params'] as $key => $val ):
echo $key . '="' . $val . '"';
<?
if ( isset( $ce['params'] ) and is_array( $ce['params'] ) ): foreach( $ce['params'] as $key => $val ):
echo $key . '="' . $val . '"';
endforeach; endif;
?>><?= $this -> element[ $ce['db'] ];?></textarea>
</div>