Processing large data sets via AJAX brings no speed benefits

牧云@^-^@ 提交于 2019-12-04 17:49:21

If you are using sessions at all during any of the given AJAX requests, they will effectively execute serially, in order of request. This is due to locking of the session data file at the operating system level. The key to getting those requests to be asynchronous is to close (or never start) the session as quickly as possible.

You can use session_write_close (docs) to close the session as soon as possible. I like to use a couple of helper functions for this, the set_session_var function below will open the session, write the var, then close the session - in and out as quickly as possible. When the page loads, you can call session_start to get the $_SESSION variable populated, then immediately call session_write_close. From then on out, only use the set function below to write.

The get function is completely optional, since you could simply refer to the $_SESSION global, but I like to use this because it provides for a default value and I can have one less ternary in the main body of the code.

function get_session_var($key=false, $default=null) {
    if ($key == false || strlen($key) < 0)
        return false;
    if (isset($_SESSION[$key]))
        $ret = $_SESSION[$key];
    else
        $ret = $default;
    return $ret;
}
function set_session_var($key=false, $value=null) {
    if ($key == false || strlen($key) < 0)
        return false;
    session_start();
    if ($value === null)
        unset($_SESSION[$key]);
    else
        $_SESSION[$key] = $value;
    session_write_close();
}

Be aware that there are a whole new set of considerations once the AJAX requests are truly asynchronous. Now you have to watch out for race conditions (you have to be wary of one request setting a variable that can impact another request) - for you see, with the sessions closed, one request's changes to $_SESSION will not be visible to another request until it rebuilds the values. You can help avoid this by "rebuilding" the $_SESSION variable immediately before a critical use:

function rebuild_session() {
    session_start();
    session_write_close();
}

... but this is still susceptible to a race condition.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!