From e3b4ec973b7a28ba0a8c3997f82b327edb4a6170 Mon Sep 17 00:00:00 2001 From: Jacek Pyziak Date: Fri, 8 Nov 2024 00:16:19 +0100 Subject: [PATCH] =?UTF-8?q?Popraw=20obs=C5=82ug=C4=99=20b=C5=82=C4=99d?= =?UTF-8?q?=C3=B3w=20i=20optymalizacj=C4=99=20po=C5=82=C4=85cze=C5=84=20z?= =?UTF-8?q?=20Redis=20w=20r=C3=B3=C5=BCnych=20klasach?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shop-payment-method/view-list.php | 3 +- autoload/.DS_Store | Bin 6148 -> 0 bytes autoload/class.RedisConnection.php | 48 +++++++++++++----- autoload/front/.DS_Store | Bin 6148 -> 0 bytes autoload/front/controls/class.ShopBasket.php | 3 +- autoload/shop/class.Product.php | 39 ++++++++------ autoload/shop/class.ProductCustomField.php | 19 +++++-- libraries/grid/ajax.php | 2 + libraries/grid/gdb.min.php | 2 +- libraries/grid/grid.php | 5 +- libraries/grid/js/grid.js | 2 +- libraries/grid/templates/edit.php | 32 ++++++------ 12 files changed, 101 insertions(+), 54 deletions(-) delete mode 100644 autoload/.DS_Store delete mode 100644 autoload/front/.DS_Store diff --git a/admin/templates/shop-payment-method/view-list.php b/admin/templates/shop-payment-method/view-list.php index 08d9c01..23961f5 100644 --- a/admin/templates/shop-payment-method/view-list.php +++ b/admin/templates/shop-payment-method/view-list.php @@ -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 = []; diff --git a/autoload/.DS_Store b/autoload/.DS_Store deleted file mode 100644 index 3239922a7e1693c4aee135bf32fa7086814704fc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}N6?5T4Nr3%&H{F|W`!h-Lc(1-(iw>Y-~&(R-f8*YM`WC-UYuKZ=V1!Glzp zf#jQHX7ckbn`DTH7Y~~`(Ts>X6hRiHA=2G69l7%a$U4UT_GZ4iD#NZZ%wcxP+E3`3 z_EhMOTI>I09ZKJ?w`Gl(?&9_F<>~zSY#cw7%{Lyq+dsz8VScw*RWJ|?1Ovgq&l$j- zEz(U5qYegwfneZ+0XZKMiePrEhI(|M(i8wF&uA6sl1oTTa?Fm^5EckqD9}RLQw+9n z^e6Yrj@8h@i9PvXpZPs`VVxc0Cv_*zhEWFt!N8b-V{6Xj{$JvkX(svOkZ1)1!N5Ob zfG7Q`Utm*yw|?24+_eei5=BJ(swfcXqelQ5a*mv;(d 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; } -} \ No newline at end of file +} diff --git a/autoload/front/.DS_Store b/autoload/front/.DS_Store deleted file mode 100644 index 7285fd13b00a087a6417494eb5c700e132b02819..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKy-veG47N)IK`b2^Z)5Hogep8iC1z;LpVa6o)PmTu@mMgi_iY&Ye72&51c?cu z%9ecJ#b@8ocTpS@5l^?v3DKB{a;V_w7|nplxM)vCW|2Wob3}Tim(|^5wun7%H~dEi zmab_*8*1HOwQge!$xjr$bq1UPXP{v~z7GK^ zSQyrd>C=HGl>opD<|OD#Eg><%urRC@F#=%?1!^c;iNP8UdoaJkuvXM?Vk-3~NPLApRqeXz<1v_)`YH0K&6M2mk;8 diff --git a/autoload/front/controls/class.ShopBasket.php b/autoload/front/controls/class.ShopBasket.php index d7a6f42..e7908cf 100644 --- a/autoload/front/controls/class.ShopBasket.php +++ b/autoload/front/controls/class.ShopBasket.php @@ -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; diff --git a/autoload/shop/class.Product.php b/autoload/shop/class.Product.php index 2004ccc..b7700df 100644 --- a/autoload/shop/class.Product.php +++ b/autoload/shop/class.Product.php @@ -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; diff --git a/autoload/shop/class.ProductCustomField.php b/autoload/shop/class.ProductCustomField.php index 21c065e..4ffaa64 100644 --- a/autoload/shop/class.ProductCustomField.php +++ b/autoload/shop/class.ProductCustomField.php @@ -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 ) diff --git a/libraries/grid/ajax.php b/libraries/grid/ajax.php index 56e203e..0d3214e 100644 --- a/libraries/grid/ajax.php +++ b/libraries/grid/ajax.php @@ -1,9 +1,11 @@ exec('INSERT INTO "' . $table . '" (' . implode(', ', $columns) . ') VALUES (' . implode($values, ', ') . ')'); + $this->exec('INSERT INTO "' . $table . '" (' . implode(', ', $columns) . ') VALUES (' . implode(', ',$values) . ')'); $lastId[] = $this->pdo->lastInsertId(); } diff --git a/libraries/grid/grid.php b/libraries/grid/grid.php index f77c6e5..0b477ad 100644 --- a/libraries/grid/grid.php +++ b/libraries/grid/grid.php @@ -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 ); diff --git a/libraries/grid/js/grid.js b/libraries/grid/js/grid.js index 75f3a7c..bd3c5bb 100644 --- a/libraries/grid/js/grid.js +++ b/libraries/grid/js/grid.js @@ -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() diff --git a/libraries/grid/templates/edit.php b/libraries/grid/templates/edit.php index 2f7289c..3985f72 100644 --- a/libraries/grid/templates/edit.php +++ b/libraries/grid/templates/edit.php @@ -17,19 +17,19 @@
- $val ): - echo $key . '="' . $val . '"'; + $val ): + echo $key . '="' . $val . '"'; endforeach; endif; ?> value=" element[ $ce['db'] ] );?>" element[ $this -> values['id'] ] ):?>readonly="readonly" />