Chapter 5. Optimization Techniques

Optimization Techniques > Cache

Because sometimes things don't run as fast as you'd like them to, you must have some knowledge of optimization techniques. Basically, optimization at the application level is all about cache, but there are also other techniques to improve things generally.

5.1. Cache

Cache is one of the most important things in your application. A well-implemented cache system will boost the application performance and save you money on extra hardware.

When implementing cache, it's a good idea to divide it into two levels: the API and the presentation.

The API level is where all the business logic resides, with its code being shared by all the interfaces for your application, like web services, for instance. The presentation level is handled by Smarty, of course, and we discuss it in detail in the next section.

Although caching data might seem simple, there are several things you have to keep in mind when implementing and tuning the cache system. One of them is business logic. You have to make sure that all the data is consistent and follows the same rules.

Let's take a look at the Book application and pretend we are fetching the book data from a database. For the purpose of this example, we're also adding another property to the book named $sales.

In this example, we want to make sure that we get the right sales numbers, but we don't want to fetch them from the database every time we need them.

The solution is to cache the sales number's value, but whenever we get a new order, the cache for the sales number is deleted, forcing the value to be fetched from the database the next time we need it.

Another thing to take into account is that you should only load other modules such as database access libraries when you need them, as seen in the next PHP script. The script will make use of PEAR's Cache_Lite package to handle all cache-related actions:

<?php // Load PEAR's Cache_Lite package. require_once 'Cache/Lite.php'; // Load configuration values require_once 'config/.php/cache.php'; class Book {     private $cache = null;     public function __contruct()     {         // Instantiate a new Cache_Lite object         $this->cache = new Cache_Lite();     }     public function getSales ($bookId)     {         $cacheId = md5('getSales' . $bookId);         if ($sales = $this->cache->get($cache_Id)) {             return $sales;         } else {             require_once 'lib/database.php';             // Fetch the sales from database into $sales.             $sales = Database::get('sales');             $this->cache->setLifeTime($conf['cache']['book']['sales'][lifetime]);             $this->cache->save($sales, $cacheId);             return $title;         }     }     public function newSale ($bookId, $value)     {         require_once 'lib/database.php';         // save the new sale in the database.         $error = Database::insert('sales', $bookId, $value);         if (!$error) {             $cacheId = md5('getSales' . $bookId);             $this->cache->remove($cacheId);         }     } } ?>     

Notice again that some values are obtained from the $conf array instead of being written directly in the script. Whenever a new sale is created, the cache is invalidated by issuing the remove() method for the corresponding cache id.

 

 



PHP and Smarty on Large-Scale Web Development
PHP and Smarty on Large-Scale Web Development
ISBN: 047008023X
EAN: N/A
Year: 2007
Pages: 20
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net