Use Windows Azure storage–table service to process session objects

Source: Internet
Author: User
Keywords Session Object

The previous article describes the use of the "blob storage service in Windows http://www.aliyun.com/zixun/aggregation/13357.html" >azure Storage Service "to store files, and this article describes The Windows Azure storage service is another storage service--"table storage service. The Table storage Service is a NoSQL data storage service available on Windows Azure that can be used to store data in a variety of formats, and to access it with key values (key), this article introduces Windows "table Storage Service for Azure storage Services".

Table Storage Service Introduction

The Windows Azure Storage Service provides a variety of data storage services, a NoSQL data storage service that is suitable for storing data that is not relational and can be accessed quickly using key values (keys) for use in application services that generate large amounts of data. Its structural concept is like this one:

Windows Azure Storage Table Service

A Windows Azure memory account can create multiple table, and the data is stored in a table, and each stored amount (a set of properties) is treated as a Entity, which has the following characteristics:

Each Entity can have 252 properties, and the system automatically generates 3 properties, namely partition key, row key, and timestamp.
The Entity class is the table-> Partition-> Row, which means that the Partition keys under the same Table are unique, and the Row keys under the same Partition are unique respectively.
The maximum amount of data that can be stored in the Entity is 1MB.

As with most Windows Azure services, you can access the table storage service data using the RESTful Web service, but the examples in this article are presented using Windows Azure SDK for PHP. (Refer to the installation method described in the previous article)

PHP Session Object

Many PHP applications use the Session object ($_session in the program in PHP) to handle the "state" of the application, such as the current user identity in the login, the contents of the cart, the data in the edit, and so on. And most of the PHP execution environment is in the default state of the file system to store the session object data, so will encounter the application on the expansion of the difficulties (the situation mentioned in the previous article), we now know how to use the Windows Azure storage services in the Blob Storage services to store files, of course, you can also use BLOB memory to process the session object, but this will be demonstrated using the Table storage service.

Implementation of PHP Session object

Since the PHP default file operation to store the data of the session object, to change to Table storage services to store as long as the session object to get rid of the behavior. Fortunately, the task does not have to change the PHP translation of the program code, PHP provides a simple approach:

Implement the Sessionhandlerinterface interface, which is how you handle session data.
Before you start using $_session, use the Session_set_save_handler () method to set your own design Sessionhandler.

So our goal is just to make a sessionhandler, and it's good to use Table storage when it comes to accessing data. The following example creates a table named "phpsessions", and then the session data is placed under the partition named "sessions", and the key value of the $_session object holds the data as a row key.
Practice Sessionhandlerinterface

So we can produce a Windowsazuretablesessionhandler class to implement Sessionhandlerinterface:

Class Windowsazuretablesessionhandler implements Sessionhandlerinterface {

...

Public function __construct ($storageAccountName, $storageAccountKey,
$sessionContainer = ' Phpsessions ', $sessionContainerPartition = ' sessions ') {...}

Public Function __destruct () {...}

Public function open ($savePath, $sessionName) {...}
Public function Close () {...}
Public function Read ($sessionId) {...}
Public function Write ($sessionId, $sessionData) {...}
Public function Destroy ($SESSIONID) {...}
Public Function GC ($lifeTime) {...}
}

Then look at how each method is implemented.

The construction and the function of deconstruction

The Sessionhandler constructor is called whenever the PHP execution environment is to use the session object, so the entity that connects the Table storage service can be established in the constructor through Windows Azure SDK for PHP, while declaring that the Call the Session_write_close function when the session object is called. Similarly, the destructor directly calls Session_write_close ().

/**
* Session Handler constructor.
*
* @param $storageAccountName the Windows Azure Table Services Account Name.
* @param $storageAccountKey the Windows Azure Table Service account key.
* @param $sessionContainer the name of the table for storing session data.
* @param $sessionContainerParition the name of the partition for storing session data.
*/
Public function __construct ($storageAccountName, $storageAccountKey, $sessionContainer = ' phpsessions ', $ Sessioncontainerpartition = ' Sessions ') {
//Create the Conneciton string for creating the table service Rest proxy Intance.
$connectionString = "DEFAULTENDPOINTSPROTOCOL=HTTPS; Accountname= ".
$storageAccountName.
; Accountkey= ".
$storageAccountKey;

//Create the table service instance.
$this->_tablerestproxy = servicebuilder::getinstance ()-> Createtableservice ($connectionString);

//Set up the table and partition names
$this->_sessioncontainer =$sessionContainer;
$this->_sessioncontainerpartition = $sessionContainerPartition; The
//Register session shutdown function.
Register_shutdown_function (' session_write_close ');
}

/**
* destructor.
*/
Public Function __destruct () {
Session_write_close ();
}

Open/close method

The Open method is also a way to call when you start using the Session object, because we use the table storage service without special switch files, but here I follow the SDK design: try to get a table first, and create a table if exception is generated.

Close is doing nothing, and simply returning TRUE means that it works.

/**
* Callback function for session handler. It ' s invoked while the session is maximally opened.
*
* @param $savePath The session of the path to store.
* @param $sessionName The name of the session.
*
* @return Boolean If The Open twist success.
*/
Public function open ($savePath, $sessionName) {
try {
Get table to the if the table exists.
$this->_tablerestproxy->gettable ($this->_sessioncontainer);
catch (Serviceexception $e) {
cant get the table, so create it
$this->_tablerestproxy->createtable ($this->_sessioncontainer);
}
return TRUE;
}

/**
* Callback function for session handler. It ' s invoked while the session is maximally closed.
*
* @return Boolean If the close twist success.
*/
Public function Close () {
Doing nothing
return TRUE;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.