Zend Framework Caching
November 2nd, 2011Credits:
- Zend Framework Docs
- Zendguru – The examples at zendguru have typos and such, so be careful.
Why Should You Cache?
The reason why you should cache is pretty simple – PERFORMANCE. There are things that your application does often that can be CPU/Memory/Time consuming. When this happens, and you are noticing a little lag, maybe it’s time to start thinking about caching.
What Should You Cache?
Cache things that:
- Don’t change often
- Are accessed a lot
Some Examples of what to cache:
- XML config files – You don’t want to parse XML files over and over again.
- Domain data in the database – Database queries can be expensive, and unneeded ones are just that…unneeded.
How Should You Cache?
You need to set up the cache object for every request. This object just tells your application where to cache and some attributes about the cache.
Note: In this example, I’m doing caching to the file system.
The initialization of the cache should happen in the Bootstrap, and once you have it, just shove it into the registry to be used throughout the request.
/**
* Initializes the cache
*
* @return nothing
*/
protected function _initCache(){
$frontend= array('lifetime' => 7200, 'automatic_serialization' => true);
// We use a helper class to get us our ini values. The value in the ini file is: BASE_PATH "/tmp/"
$backend= array('cache_dir' => App_Helper::getIniValue('directory', 'cache'));
$cache = Zend_Cache::factory('core', 'File', $frontend, $backend);
Zend_Registry::set('cache',$cache);
}
Here is an example of how to cache a database query. The example below is querying a table that has domain data. Before, it just called the fetchAll() for the table, but now, we:
- Get the cache object from the registry
- Check to see if what we are looking for is already in the registry
- If it is, use it.
- If it isn’t, we do the work necessary to go and get it, and then set it into the registry.
/**
* Gets a collection of the domaindata objects
*
* @return array An array of DomainData objects
*/
public function fetchAll(){
$cache = Zend_Registry::get('cache');
if(!$result = $cache->load('domaindata')) {
$result = parent::fetchAll();
$cache->save($result, 'domaindata');
}
return $result;
}
Oh No! We have added something to the domaindata table and we need to refresh the cache! Everywhere you do an ADD or an UPDATE to the table, you should put the following code afterwards:
$cache = Zend_Registry::get('cache');
$cache->remove('domaindata');
…and if you need to dump the entire cache do this:
$cache = Zend_Registry::get('cache');
$cache->clean();
Mac OS X + Postfix + Gmail
November 1st, 2011Credits:
Tonight, I was trying to get emails to send from my localhost. I am running Mac OS X Snow Leopard. I tried to follow a few tutorials out there, and found that the combination of the two above were the best…so I combined them. I am going to use my GMail SMTP server so there are two (kind of 3) files that we need to mess with:
1. /etc/postfix/main.cf
Take the snippet of configuration below and add it to the VERY BOTTOM of the main.cf file.
###### # sudo nano /etc/postfix/smtp_sasl_passwords # add 'smtp.gmail.com:587 your.name@gmail.com:your.password' # save and run 'sudo postmap /etc/postfix/smtp_sasl_passwords' # start postfix with 'sudo postfix start' # Minimum Postfix-specific configurations relayhost = smtp.gmail.com:587 # Enable SASL authentication in the Postfix SMTP client. smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/smtp_sasl_passwords smtp_sasl_security_options = smtp_sasl_local_domain = local.dns.com broken_sasl_auth_clients = yes smtpd_pw_server_security_options = noanonymous # Enable Transport Layer Security (TLS), i.e. SSL. smtp_use_tls = yes smtp_tls_security_level = encrypt tls_random_source = dev:/dev/urandom
2. /etc/postfix/smtp_sasl_passwords
Add the following line to the smtp_sasl_passwords file. FYI, this file probably doesn’t exist, so you will need to create it.
smtp.gmail.com:587 username@gmail.com:password
Where “username” is your gmail username, and “password” is your gmail password.
3. /etc/postfix/smtp_sasl_passwords.db
To create the smtp_sasl_passwords.db file you need to run the following command:
sudo postmap /etc/postfix/smtp_sasl_passwords
Troubleshooting
To troubleshoot I used the Console.app and tried to fix any issues that showed up in there.
Things to Note
It seems like there is a lag in the sending of the emails. Sometimes it is a full minute ore a little more before the email appears in my GMail box.