容量和性能:$ _ COOKIE与$ _SESSION

时间:2019-04-13 18:49:59

标签: php

我有一个非特定于用户的大型多维数组〜100Mb-150Mb,目前将其保存在一个JSON文件中。必须使用来自API的数据每分钟更新此数组。

我不确定是否应该使用add_filter( 'gettext', 'change_add_to_cart_message', 10, 3 ); function change_add_to_cart_message( $translated, $text, $domain ) { global $pagenow; if( $text === 'WooCommerce status' && $domain === 'woocommerce' && is_admin() && $pagenow === 'index.php' ){ $translated = __( 'WooCommerce summary', $domain ); } return $translated; } $_COOKIE进行存储,并避免写入文件(即$_SESSION)以提高性能。

可能没有内存问题,因为已经使用以下脚本顶部的此设置收集了数据:

fopen(); fwrite(); fclose();

数据收集

ini_set('max_execution_time', 0);
ini_set('memory_limit', '-1');
set_time_limit(0);
  • 默认情况下,max capacity of $_SESSION// Config class for path and other constants require_once __DIR__ . "/ConstEQ.php"; class EquityRecords extends EQ implements ConstEQ { public static function getSymbols() { //***************** START: ALL SYMBOLS ARRAY ********************** // // var: is a filename path directory, where there is an md file with list of equities $list_of_equities_file = __DIR__ . self::SYMBOLS_PATH; // var: is content of md file with list of equities $content_of_equities = file_get_contents($list_of_equities_file); // var is an array(3) of equities such as: string(4) "ZYNE", string(10) "2019-01-04", string(27) "ZYNERBA PHARMACEUTICALS INC" $symbols_array = preg_split('/\R/', $content_of_equities); //***************** END: ALL SYMBOLS ARRAY ********************** // // child and mother arrays are created to help calling equities in batches of 100, which seems to be the API limit. $child = array(); $mother = array(); // var: is 100 counter $limit_counter = self::NUMBER_OF_STOCKS_PER_REQUEST; foreach ($symbols_array as $ticker_arr) { $limit_counter = $limit_counter - 1; $symbols_array = preg_split('/\t/', $ticker_arr); array_push($child, $symbols_array); if ($limit_counter <= 0) { $limit_counter = self::NUMBER_OF_STOCKS_PER_REQUEST; array_push($mother, $child); $child = array(); } } return $mother; } public static function allEquitiesSignleJSON() { $equity_arrays = EquityRecords::getSymbols(); $base_url = self::BASE_URL . self::TARGET_QUERY; $current_time = date("Y-m-d-H-i-s"); $all_equities = array(); // ticker: AAPL, GE, AMD foreach ($equity_arrays as $ticker_arr) { $ticker = array_column($ticker_arr, 0); $equity_url = $base_url . implode("%2C", $ticker) . self::END_POINT; $raw_eauity_json = file_get_contents($equity_url); $raw_equity_array = json_decode($raw_eauity_json, true); $all_equities = array_merge($all_equities, $raw_equity_array); } $all_equities_json = json_encode($all_equities); $symbols_dir = __DIR__ . self::SYMBOLS_DIR; if (!is_dir($symbols_dir)) {mkdir($symbols_dir, 0755, true);} $raw_equity_file = $symbols_dir . "/" . $current_time . ".json"; $fp = fopen($raw_equity_file, "x+"); fwrite($fp, $all_equities_json); fclose($fp); echo "YAAAY! Equity JSON file success at " . __METHOD__ . " ! " . self::NEW_LINE; } } 128Mb的最大容量与$_COOKIE相同吗?
  • 使用哪种可能更快或更应该使用哪种速度,或者还有其他方法可以避免写入文件来提高性能?

1 个答案:

答案 0 :(得分:2)

  

我不确定是否应该使用$_COOKIE$_SESSION来存储它…

这两种东西都不大可能。

$_COOKIE映射到用户浏览器中的cookie。 It is limited to 4 kB per cookie, and a total of roughly 80 kB across all cookies.设置较大的Cookie将使对您网站的请求变慢,因此不建议这样做。

$_SESSION存储在Web服务器上的文件中,并且具有更宽松的大小限制,但为每个用户单独存储。除非您从此API收集的数据是特定于用户的,否则这也不是一个好选择。

如果您的数据确实只是从API收集的,并且应该在所有用户之间共享,那么将其存储在文件中(就像您已经在做的一样!)是一种非常合理的方法。 Cookie无法容纳100 MB,并且将其存储在会话中也无济于事。