次の方法で共有


PHP, Memcache, and Windows Azure Caching

Yesterday, my colleague Larry Franks posted about Windows Azure Caching (Preview) and Ruby Cloud Services. As he points out, most of his post is relevant to any application using Windows Azure Caching, not just Ruby applications. He also pointed out that Windows Azure Caching supports the Memcache wire protocol. So, putting 2 and 2 together (not hard to do in this case), I figured I should be able to take a PHP application that uses the Memcache extension, deploy it as a Windows Azure Web Role, and it should “just work”. And, it does, but with the Caching service being a “preview”, the user experience isn’t as smooth as we’d like it to be, so I’m hoping to get your feedback.

I strongly suggest you read Larry’s post, especially the information about the co-located vs. dedicated options. I will be following Larry’s example by using a co-located cache. And, to keep things simple, my “application” will be a single file that uses code from Example #1 of the Memcache extension Basic Usage documentation

Install the Windows Azure SDK for .NET

Unfortunately, installing the Windows Azure SDK for PHP does not (currently) get you the all the bits you need to use the Caching service. However, getting the bits is still fairly easy: run the WindowsAzureLibsForNet-x64.msi or the WindowsAzureLibsForNet-x86.msi (depending on your architecture) from the Windows Azure SDK for .NET – June 2012 download page.  (Note: I’m assuming you also have the Windows Azure SDK for PHP installed.)

Create a Windows Azure PHP Web Role

Rather than repeat the steps for creating a PHP Web Role, I’ll refer you to a post I wrote a couple of weeks ago: Creating a PHP Web Role in Windows Azure (you can probably start with the Import your publish settings section). However, when you get to the Customize your PHP configuration section, you will need to add the php_memcache.dll file to the ext folder (not the php_mongo.dll file as shown in my example) and the Memcache extension will need to be enabled in the php.ini file.

Note: If you don’t have the php_memcache.dll file, I recommend getting the correct version from Pierre Joye’s archive of extensions here: https://downloads.php.net/pierre/.

Edit the Service Definition and Service Configuration Files

Enabling the Caching service (currently) requires that you manually edit the service definition (.csdef) and service configuration (.cscfg) files. So, after you have created your PHP Web Role, open the ServiceDefinition.csdef and ServiceConfiguration.Cloud.cscfg files in your favorite text editor and make the following additions:

In the ServiceDefinition file…

1. Import the caching module by adding the following element as a child of the <Imports> element:

 <Import moduleName="Caching" />

2. Add a local store by adding the following element as a child of the <WebRole> element:

 <LocalResources> 
    <LocalStorage cleanOnRoleRecycle="false"  name="Microsoft.WindowsAzure.Plugins.Caching.FileStore" sizeInMB="1000"/> 
 </LocalResources>

3. Add an endpoint that the client will use to access the cache using the Memcache protocol by adding the following element as a child of the <Endpoints> element:

 <InternalEndpoint name="memcache_default" protocol="tcp" port="11211" />

In the ServiceConfiguration file…

Add the following elements as children of the <ConfiguratinSettings> element (replacing “your_storage_account_name” and “your_storage_account_key” with the appropriate values):

 <Setting name="Microsoft.WindowsAzure.Plugins.Caching.NamedCaches" value=""/>
 <Setting name="Microsoft.WindowsAzure.Plugins.Caching.Loglevel" value=""/>
 <Setting name="Microsoft.WindowsAzure.Plugins.Caching.CacheSizePercentage" value="30"/>
 <Setting name="Microsoft.WindowsAzure.Plugins.Caching.ConfigStoreConnectionString" value="DefaultEndpointsProtocol=https;AccountName=your-storage_account_name;AccountKey=your_storage_account_key"/>

Modify your Memcache connection code

The server name in your Memcache connection code is of the form localhost_webRoleName, where webRoleName is the name of your Web Role. In the example in my previous post, I used myWebRole as the Web Role name, so my Memcache connection code looks like this:

 $memcache = new Memcache;
 $memcache->connect('localhost_myWebRole', 11211) or die ("Could not connect");

As I mentioned earlier, the rest of my “application” is the code from Example #1 of the Memcache extension Basic Usage documentation.

Publish your application

You can now publish your application (or test it in the emulator). If you are following my simple example, you should see something like this:

Screen Shot 2012-07-02 at 12.17.19 PM

That’s it! I know that the Windows Azure team is looking at ways to make enabling the Caching service easier. We’d love to hear your feedback on this, as well as any feedback you have on the service itself.

Thanks.

-Brian

Comments

  • Anonymous
    February 17, 2016
    Its a shame that even in 2016, this is the best resource on getting memcache working on azure web apps - and its very likely out of date with the current version of Azure. Thank you for sharing, this was quite informative :)

  • Anonymous
    February 22, 2016
    Hi Simon: I believe this article may be more up to date: azure.microsoft.com/.../web-sites-connect-to-redis-using-memcache-protocol.