Skip to content

Instantly share code, notes, and snippets.

@ericsk
Created December 30, 2013 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericsk/8183069 to your computer and use it in GitHub Desktop.
Save ericsk/8183069 to your computer and use it in GitHub Desktop.
WindowsAzureSessionHandler 的 Write 方法
/**
* Callback function for session handler. It's invoked while the session data is being written.
*
* @param $sessionId The session ID.
* @param $sessionData The data to be written in session.
*
* @return boolean If the write operation success.
*/
public function write($sessionId, $sessionData) {
// serialize and encode the session data.
$serializedData = base64_encode(serialize($sessionData));
try {
// try to retrive the stored session entity and update it.
$result = $this->_tableRestProxy->getEntity($this->_sessionContainer, $this->_sessionContainerPartition, $sessionId);
$entity = $result->getEntity();
// update data and expiry time
$entity->setPropertyValue('data', $serializedData);
$entity->setPropertyValue('expires', time());
// update entity
$this->_tableRestProxy->updateEntity($this->_sessionContainer, $entity);
} catch (ServiceException $e) {
// otherwise, create a new session entity to store the data.
$entity = new Entity();
// set partition key and use session id as the row key.
$entity->setPartitionKey($this->_sessionContainerPartition);
$entity->setRowKey($sessionId);
// set data and expiry time
$entity->addProperty('data', EdmType::STRING, $serializedData);
$entity->addProperty('expires', EdmType::INT32, time());
// insert the entity
$this->_tableRestProxy->insertEntity($this->_sessionContainer, $entity);
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment