Zend Framework Cache Backend Libmemcached + Session Cache
If I haven't said it before, I suppose I will say it now, I love memcached; it is a wonderful tool to have in your tool belt. However, Zend Framework does not currently have an official cache adapter for Memcached (it has one for the extension memcache but not memcached). The ZF 1.x trunk now has a Libmemcached adapter thanks to Marc Bennewitz for implementing the changes required to my patch and for everyone that helped to get this in. I believe it will become a part of the 1.11 Zend Framework release.
The Libmemcached adapter implements the PHP a€omemcacheda€¯ extension. If you are currently using the current Zend Framework a€oMemcacheda€¯ cache adapter, I urge you to take a peak at the new one from the link above as there are a few options that have been added and it supports all of the client options from the Memcached PHP extension.
From here, it gets to be a little more funa€¦ About 6-9 months ago, I had implemented a Session_SaveHandler_Cache (although it seems some others have as well), to plug this all in together and keep things consistent in a ZF application. It now enables us to utilize the consistent hashing from the Libmemcached adapter by using the PHP Memcached extension. I'll go over this a little more below.
When looking to improve performance and you are utilizing PHP sessions, where are they currently stored? Did you know it is extremely hard to scale if you put them on a database server? What about an NFS mount or even separating load based on the visitor. All of these are bad practice as it is really not meant to store your sessions.
In comes memcached - memcached can handle a great deal more and you can spread around your sessions utilizing consistent hashing. This means that if one server was to go down you would still have most of your other sessions available. The speed increase that comes from this is very high certainly if you stored them in the database before. In our experience, the database load dropped over 100% after moving sessions out of there and into the memcached servers.
Storing Sessions in Memcached with Zend Framework
Zend Framework has an excellent API for sessions in order to build out your own session save handler. By taking this approach I can still leverage the cache manager and share the same connection to memcached for other things in my application. If you need to expire contents during a deployment this can still handle it utilizing the a€oclonea€¯ keyword.
On to the code! See the link above for the Libmemcached implementation if you do not have the latest RC release. Next we need to build out the session save handler. We will name this Zend_Session_SaveHandler_Cache as it will support any cache that you would like to throw at it. The benefit of this really comes down to the lower environments and the ability to simply store sessions in APC for a time or even going to a simple file based for development / testing.
You will want to store this file in library/Zend/Session/SaveHandler/Cache.php. This handles the main setup for doing the Cache save handler.A /** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-webat this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to email@example.com so we can send you a copy immediately. * * @category Zend * @package Zend_Session * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id$ */ A /** * @see Zend_Session */ require_once 'Zend/Session.php'; A /** * @see Zend_Config */ require_once 'Zend/Config.php'; A /** * @see Zend_Cache */ require_once 'Zend/Cache.php'; A /** * Zend_Session_SaveHa
Truncated by Planet PHP, read more at the original (another 28703 bytes)