Click Fraud Protection
PHP related blog, freelancer to hire!

sfMemcachePlugin limitations

I’m using quite hapilly sfMemcachePlugin, but recently i needed to store object which was around 2Mb, and problems started. I’ve spend couple minutes trying to figure out why app is not working, finally i Found that memcache doesn’t store stuff more than 1Mb. To solve that, I’ve decided to compress data and then store it, but if data is still over limit i’m switching to sessions anyway.

if (!extension_loaded('zlib'))
{
// there is 1024Kb limitation on memcache, so we need to compress
$this->_storing_method = 'sessions';
}
	public function someFunctionToStoreData()
	{
		if ($this->_storing_method=='sessions')
		{
			sfContext::getInstance()->getUser()->setAttribute('variableId',$this->_varToStore);
		} else
		{
			// compress
                        $data = gzcompress(serialize($this->_varToStore),9);
			// compute data size
                        $data_size = strlen($data)/1024/1024;
                        // log it in symfony
			sfContext::getInstance()->getLogger()->info('Data size for memcache to store is '.$data_size." Mb");
                        // check data size - 0.9Mb - just in case to not go over 1Mb
			if ($data_size>0.9)
			{
				$this->_storing_method='sessions';
				$this->someFunctionToStoreData();
			}
			sfMemcacheFunctionCache::getInstance()->set( 'variableId', 'OurNameSpace', $data);
		}
		sfContext::getInstance()->getLogger()->info('Setting search ordering variable for country '.$this->_locationId);
	}

Leave a Reply