Optimization Techniques

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.

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 ...

Get PHP and Smarty on Large-Scale Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.