PHP Class Google\Cloud\Datastore\DatastoreSessionHandler

Instead of storing the session data in a local file, it stores the data to Cloud Datastore. The biggest benefit of doing this is the data can be shared by multiple instances, so it's suitable for cloud applications. The downside of using Cloud Datastore is the write operations will cost you some money, so it is highly recommended to minimize the write operations with your session data with this handler. In order to do so, keep the data in the session as limited as possible; for example, it is ok to put only signed-in state and the user id in the session with this handler. However, for example, it is definitely not recommended that you store your application's whole undo history in the session, because every user operations will cause the Datastore write and then it will cost you lot of money. This handler doesn't provide pessimistic lock for session data. Instead, it uses Datastore Transaction for data consistency. This means that if multiple requests are modifying the same session data simultaneously, there will be more probablity that some of the write operations will fail. If you are building an ajax application which may issue multiple requests to the server, please design the session data carefully, in order to avoid possible data contentions. Also please see the 2nd example below for how to properly handle errors on write operations. It uses the session.save_path as the Datastore namespace for isolating the session data from your application data, it also uses the session.name as the Datastore kind, the session id as the Datastore id. By default, it does nothing on gc for reducing the cost. Pass positive value up to 1000 for $gcLimit parameter to delete entities in gc. Note: The datastore transaction only lasts 60 seconds. If this handler is used for long running requests, it will fail on write. Example without error handling: use Google\Cloud\ServiceBuilder; $cloud = new ServiceBuilder(); $datastore = $cloud->datastore(); or just $datastore = new \Google\Cloud\Datastore\DatastoreClient(); $handler = new DatastoreSessionHandler($datastore); session_set_save_handler($handler, true); session_save_path('sessions'); session_start(); Then read and write the $_SESSION array. The above example automatically writes the session data. It's handy, but the code doesn't stop even if it fails to write the session data, because the write happens when the code exits. If you want to know the session data is correctly written to the Datastore, you need to call session_write_close() explicitly and then handle E_USER_WARNING properly like the following example. Example with error handling: use Google\Cloud\ServiceBuilder; $cloud = new ServiceBuilder(); $datastore = $cloud->datastore(); or just $datastore = new \Google\Cloud\Datastore\DatastoreClient(); $handler = new DatastoreSessionHandler($datastore); session_set_save_handler($handler, true); session_save_path('sessions'); session_start(); Then read and write the $_SESSION array. function handle_session_error($errNo, $errStr, $errFile, $errLine) { # We throw an exception here, but you can do whatever you need. throw new Exception("$errStr in $errFile on line $errLine", $errNo); } set_error_handler('handle_session_error', E_USER_WARNING); If write fails for any reason, an exception will be thrown. session_write_close(); restore_error_handler(); You can still read the $_SESSION array after closing the session.
See also: http://php.net/manual/en/class.sessionhandlerinterface.php SessionHandlerInterface
Inheritance: implements SessionHandlerInterfac\SessionHandlerInterface
Exibir arquivo Open project: GoogleCloudPlatform/gcloud-php Class Usage Examples

Public Methods

Method Description
__construct ( DatastoreClient $datastore, integer $gcLimit = self::DEFAULT_GC_LIMIT ) Create a custom session handler backed by Cloud Datastore.
close ( ) Just return true for this implementation.
destroy ( $id ) Delete the session data from Cloud Datastore.
gc ( $maxlifetime ) Delete the old session data from Cloud Datastore.
open ( string $savePath, string $sessionName ) : boolean Start a session, by creating a transaction for the later write.
read ( $id ) Read the session data from Cloud Datastore.
write ( $id, $data ) Write the session data to Cloud Datastore.

Method Details

__construct() public method

Create a custom session handler backed by Cloud Datastore.
public __construct ( DatastoreClient $datastore, integer $gcLimit = self::DEFAULT_GC_LIMIT )
$datastore DatastoreClient Datastore client.
$gcLimit integer [optional] A number of entities to delete in the garbage collection. Defaults to 0 which means it does nothing. The value larger than 1000 will be cut down to 1000.

close() public method

Just return true for this implementation.
public close ( )

destroy() public method

Delete the session data from Cloud Datastore.
public destroy ( $id )

gc() public method

Delete the old session data from Cloud Datastore.
public gc ( $maxlifetime )

open() public method

Start a session, by creating a transaction for the later write.
public open ( string $savePath, string $sessionName ) : boolean
$savePath string The value of `session.save_path` setting will be used here. It will use this value as the Datastore namespaceId.
$sessionName string The value of `session.name` setting will be used here. It will use this value as the Datastore kind.
return boolean

read() public method

Read the session data from Cloud Datastore.
public read ( $id )

write() public method

Write the session data to Cloud Datastore.
public write ( $id, $data )