質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

FuelPHP

FuelPHPは、軽量高速で開発が可能なPHPのWebアプリケーションフレームワークです。

Q&A

1回答

1111閲覧

【FuelPHP】Session::get()が遅い

tampopopofu

総合スコア22

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

FuelPHP

FuelPHPは、軽量高速で開発が可能なPHPのWebアプリケーションフレームワークです。

0グッド

2クリップ

投稿2019/07/14 05:20

編集2022/01/12 10:55

前提・実現したいこと

fuelphpを使って開発をしています。
最近、バージョンを「1.8.0」から「1.8.1」に上げました。
ログイン処理はDBを使用し、認証成功ならセッションにユーザIDを格納しています。
その後の画面遷移ごとにセッションからユーザIDを確認しています。
Authパッケージは使用しています。

発生している問題・エラーメッセージ

ログインチェックする際に、セッションからユーザIDを取得するのですが、「1.8.1」にしてからすごい時間が掛かるようになりました。

該当のソースコード

ログイン成功時にセッションに値を格納。

php

1public function complete_login($user_id) { 2 Session::set('user_login', $user_id); // ← ここは速い 3 Session::instance()->rotate(); 4}

画面遷移ごとに行うログインチェック。

php

1// ログインチェック 2if (!Auth::check()) { 3 Response::redirect('/'); 4 return; 5}

Auth::check()から呼ばれるperform_check()が以下。

php

1protected function perform_check() { 2 $this->user = Session::get('user_login'); // ← ここに時間が掛かる 3 return !empty($this->user); 4}

試したこと

Session::get()しているところに固定値のユーザIDを入れたら前と同じくらいの処理時間になりました。

php

1protected function perform_check() { 2 $this->user = 'USER0001'; // ← 固定値にすると速い 3 return !empty($this->user); 4}

補足情報(FW/ツールのバージョンなど)

「1.8.0」から「1.8.1」にバージョンアップしてSessionクラスを見てみましたが、結構変わっていてどこが原因か特定できませんでした。

処理速度としましては10倍以上の差が出ています。
150msだったのが1,800msになったりとかです。

追加情報

php

1// authのConfigです 2<?php 3return array( 4 'driver' => 'Dbdriver', 5 'verify_multiple_logins' => false, 6 'salt' => 'dummy_salt', 7 'iterations' => 10000, 8);

config/config.php

php

1<?php 2/** 3 * Fuel is a fast, lightweight, community driven PHP 5.4+ framework. 4 * 5 * @package Fuel 6 * @version 1.8.1 7 * @author Fuel Development Team 8 * @license MIT License 9 * @copyright 2010 - 2018 Fuel Development Team 10 * @link http://fuelphp.com 11 */ 12 13return array( 14 // 'base_url' => null, 15 // 'url_suffix' => '', 16 'index_file' => '', 17 'profiling' => false, 18 // 'cache_dir' => APPPATH.'cache/', 19 'caching' => true, 20 'cache_lifetime' => 7200, // In Seconds 21 // 'ob_callback' => null, 22 'language' => 'ja', // Default language 23 'language_fallback' => 'en', // Fallback language when file isn't available for default language 24 'locale' => null, 25 'encoding' => 'UTF-8', 26 'server_gmt_offset' => 0, 27 'default_timezone' => 'Asia/Tokyo', 28 'log_threshold' => Fuel::L_ALL, 29 // 'log_path' => APPPATH.'logs/', 30 'log_date_format' => 'Y-m-d H:i:s', 31 32 /** 33 * Security settings 34 */ 35 'security' => array( 36 'csrf_autoload' => false, 37 // 'csrf_autoload_methods' => array('post', 'put', 'delete'), 38 // 'csrf_bad_request_on_fail' => false, 39 // 'csrf_auto_token' => false, 40 'csrf_token_key' => 'csrf_token', 41 'csrf_expiration' => 10000, 42 43 /** 44 * A salt to make sure the generated security tokens are not predictable 45 */ 46 'token_salt' => 'dummy_token_salt', 47 48 /** 49 * Allow the Input class to use X headers when present 50 * 51 * Examples of these are HTTP_X_FORWARDED_FOR and HTTP_X_FORWARDED_PROTO, which 52 * can be faked which could have security implications 53 */ 54 // 'allow_x_headers' => false, 55 56 /** 57 * This input filter can be any normal PHP function as well as 'xss_clean' 58 * 59 * WARNING: Using xss_clean will cause a performance hit. 60 * How much is dependant on how much input data there is. 61 */ 62 'uri_filter' => array('htmlentities'), 63 64 /** 65 * This input filter can be any normal PHP function as well as 'xss_clean' 66 * 67 * WARNING: Using xss_clean will cause a performance hit. 68 * How much is dependant on how much input data there is. 69 */ 70 // 'input_filter' => array(), 71 72 /** 73 * This output filter can be any normal PHP function as well as 'xss_clean' 74 * 75 * WARNING: Using xss_clean will cause a performance hit. 76 * How much is dependant on how much input data there is. 77 */ 78 'output_filter' => array('Security::htmlentities'), 79 80 /** 81 * Encoding mechanism to use on htmlentities() 82 */ 83 // 'htmlentities_flags' => ENT_QUOTES, 84 85 /** 86 * Whether to encode HTML entities as well 87 */ 88 // 'htmlentities_double_encode' => false, 89 90 /** 91 * Whether to automatically filter view data 92 */ 93 // 'auto_filter_output' => true, 94 95 /** 96 * With output encoding switched on all objects passed will be converted to strings or 97 * throw exceptions unless they are instances of the classes in this array. 98 */ 99 'whitelisted_classes' => array( 100 'Fuel\Core\Presenter', 101 'Fuel\Core\Response', 102 'Fuel\Core\View', 103 'Fuel\Core\ViewModel', 104 'Closure', 105 ), 106 ), 107 108 /** 109 * Cookie settings 110 */ 111 // 'cookie' => array( 112 // Number of seconds before the cookie expires 113 // 'expiration' => 0, 114 // Restrict the path that the cookie is available to 115 // 'path' => '/', 116 // Restrict the domain that the cookie is available to 117 // 'domain' => null, 118 // Only transmit cookies over secure connections 119 // 'secure' => false, 120 // Only transmit cookies over HTTP, disabling Javascript access 121 // 'http_only' => false, 122 // ), 123 124 /** 125 * Validation settings 126 */ 127 // 'validation' => array( 128 /** 129 * Whether to fallback to global when a value is not found in the input array. 130 */ 131 // 'global_input_fallback' => true, 132 // ), 133 134 /** 135 * Controller class prefix 136 */ 137 // 'controller_prefix' => 'Controller_', 138 139 /** 140 * Routing settings 141 */ 142 // 'routing' => array( 143 /** 144 * Whether URI routing is case sensitive or not 145 */ 146 // 'case_sensitive' => true, 147 148 /** 149 * Whether to strip the extension 150 */ 151 // 'strip_extension' => true, 152 // ), 153 154 /** 155 * To enable you to split up your application into modules which can be 156 * routed by the first uri segment you have to define their basepaths 157 * here. By default empty, but to use them you can add something 158 * like this: 159 * array(APPPATH.'modules'.DS) 160 * 161 * Paths MUST end with a directory separator (the DS constant)! 162 */ 163 'module_paths' => [ 164 APPPATH.'modules'.DS 165 ], 166 167 /** 168 * To enable you to split up your additions to the framework, packages are 169 * used. You can define the basepaths for your packages here. By default 170 * empty, but to use them you can add something like this: 171 * array(APPPATH.'modules'.DS) 172 * 173 * Paths MUST end with a directory separator (the DS constant)! 174 */ 175 'package_paths' => array( 176 PKGPATH, 177 ), 178 179 /**************************************************************************/ 180 /* Always Load */ 181 /**************************************************************************/ 182 'always_load' => [ 183 184 /** 185 * These packages are loaded on Fuel's startup. 186 * You can specify them in the following manner: 187 * 188 * array('auth'); // This will assume the packages are in PKGPATH 189 * 190 * // Use this format to specify the path to the package explicitly 191 * array( 192 * array('auth' => PKGPATH.'auth/') 193 * ); 194 */ 195 // 'packages' => array( 196 // //'orm', 197 // ), 198 'packages' => [ 199 'auth', 200 'email', 201 ], 202 203 /** 204 * These modules are always loaded on Fuel's startup. You can specify them 205 * in the following manner: 206 * 207 * array('module_name'); 208 * 209 * A path must be set in module_paths for this to work. 210 */ 211 'modules' => [ 212 'AAA', 213 'BBB', 214 ], 215 216 /** 217 * Classes to autoload & initialize even when not used 218 */ 219 // 'classes' => array(), 220 221 /** 222 * Configs to autoload 223 * 224 * Examples: if you want to load 'session' config into a group 'session' you only have to 225 * add 'session'. If you want to add it to another group (example: 'auth') you have to 226 * add it like 'session' => 'auth'. 227 * If you don't want the config in a group use null as groupname. 228 */ 229 // 'config' => array(), 230 'config' => [ 231 ], 232 233 /** 234 * Language files to autoload 235 * 236 * Examples: if you want to load 'validation' lang into a group 'validation' you only have to 237 * add 'validation'. If you want to add it to another group (example: 'forms') you have to 238 * add it like 'validation' => 'forms'. 239 * If you don't want the lang in a group use null as groupname. 240 */ 241 // 'language' => array(), 242 ], 243 244);

sessionのConfigはdefault(Core)のままで触ってはいません。

<データベース>
xamppの10.1.22-MariaDB、phpMyAdminのバージョンは4.8.5です。

お分かりになる方、どうぞご教授お願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Eggpan

2019/07/17 16:32

セッションのコンフィグ設定や、セッションドライバがデータベース利用の場合はDB環境も追記してもらったほうが良い回答が得られると思います。 時々重くなる、とかであれば gc_probability 設定に関わる動作周りかもしれません。
tampopopofu

2019/07/20 05:28

ご助言ありがとうございます。 Config等の情報を追加しました。
guest

回答1

0

直接の回答では無いですが・・。

config/config.phpのprofilerをtrueにすると画面からプロファイラが表示できます。
気になる箇所にProfiler::mark("適当なメッセージ");を挿入するとProfiler内のConsoleタブで実行時間が確認できますので、ソースファイル各所にいれてボトルネックを探してみてはどうでしょうか。

手元の環境で1.8.0と1.8.1を入れてみてSession::getだけ実行する処理で比べてみましたが、どちらも0.01〜0.02秒の間で実行されまして、1割か2割程度1.8.1の方が遅い結果にはなりましたが、速度にそこまでの差はありませんでした。

投稿2020/02/22 10:38

Eggpan

総合スコア2665

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Eggpan

2020/02/22 23:47

CookieドライバでSession::get すると概ね下記のメソッドを通るみたいです。 Session::_init Session::forge Session_Cookie::start Session_Cookie::_change_state Session_Cookie::read Session_Cookie::_get_cookie Crypt::decode Crypt::encode
tampopopofu

2020/03/15 05:03

ありがとうございます! 試してみます。
tampopopofu

2020/03/15 05:20

ajaxでの処理なのでうまくプロファイルが利かないみたいですね。。 あれからもう一度フレームワークを再DLして入れ替えたところ、アプリの起動直後はやはり1,800ms程度掛かりますが、そのまま何回か動かしていくと300ms~400msに落ち着きます。 一旦はこのままでいこうと思います。 あと分かったことは、ブラウザの開発ツールのネットワークを見ると、処理時間のうち、9割は「待機」の時間で占められていました。 「Session::get」を入れると待機し、無くすと待機がなくなり処理は速いです。 なんなんでしょうねぇ。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問