Always Specify a Key When Using Zend_Cache
I've been using Zend_Cache for a while to improve performance on my sites and it has this cool feature where you don't have to specify the key when you perform the save. You end up with a lot of code that looks like this:
$key = 'permission' . $role->getId();
if(($permissions = $cache->load($key)) == false){
$permissions = $this->someReallyComplicatedQuery();
$cache->save($permissions);
}
This works really really well but then I started noticing some performance problem and random page failures on one of the sites I maintain. I started looking into the problems and I found some of these sections weren't actually saving the correct data so I started breaking down what exactly someReallyComplicatedQuery
was doing only to find ANOTHER one of these blocks. It turns on the second call to $cache->load()
was resetting the value and causing all the problem. So from now on I'm always saving using this form (actually I've rewritten all of these blocks on several of my sites):
$key = 'permission' . $role->getId();
if(($permissions = $cache->load($key)) == false){
$permissions = $this->someReallyComplicatedQuery();
$cache->save($permissions , $key);
}
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows