如何存储我的Google云端硬盘服务以避免在每次通话中重新使用它?

I'm using the code above, from https://developers.google.com/drive/delegation, to get access to a specific Google Drive account:

function buildService($userEmail) {
    global $DRIVE_SCOPE, $SERVICE_ACCOUNT_EMAIL, $SERVICE_ACCOUNT_PKCS12_FILE_PATH;
    $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
    $auth = new Google_AssertionCredentials(
        $SERVICE_ACCOUNT_EMAIL,
        array($DRIVE_SCOPE),
        $key);
    $auth->sub = $userEmail;
    $client = new Google_Client();
    $client->setUseObjects(true);
    $client->setAssertionCredentials($auth);
    return new Google_DriveService($client);
}

It Works fine, but is very slow the creation of the object. I want to store the service in the session to avoid recreating it in each request to the server. Serialize is not possible. Any idea?

If you want to reuse your service in the same script execution, you can make a Singleton to store all methods you need.

In this case, your class is instanciated once, and when you call once more, the previous instance is returned instead of creating a new one.