Optimizing Web Application Speed with Caching in Yii Framework

A major feature where Yii leaves all its counterparts far behind in the curve and standout to be incredible is its Cache Management. By using its core caching techniques, developers can work on making page load much faster and hence what you get is a speed optimized web application and services.

Cache management is related to saving of static information and data along with queries associated to Active Record. With Yii you get a strong hold on caching mechanism which is a very efficient way to improve the performance of a web application. It reduces the time needed to generate the data when requested thereby increasing speed.

Here we will have a walk through the process of caching data and queries using Yii:

Step 1: Enable Cache:

Prior to any step, we should see that the cache tool must be enabled. To enable this we must follow these steps-

  • In the config file (protected/config/), we have to get to the ‘components’ section.
  • In the component, we have to affix some code lines to the array:

This addition of code allows us to work with the Yii caching tool, CapcCache. This tool gives us an efficient means to increase the speed and the performance of the application. Also there is another tool available for this optimization, CDBCache. It is designed to work with the sqlite database.

Step 2: Basic Caching Process

Caching is an easy approach when it uses storage of variables. With Yii, we get two major functions to achieve caching-

Proceeding in this step we have to set up a value for caching. In this process we have to choose a unique ID for the variable.

Step 3: Stored Values & Clearance

Sometimes we have to remove the value that has been saved in database. There can be various reasons why this process is needed. There are times when variables are not needed in the programming construct, hence it becomes necessary to free the space and clear out the stored variable.

Yii provides an incredibly robust layered caching scheme which can easily be altered without touching the application code. With this caching mechanism web applications can be made really fast with accelerated page loading. Hence get to YII to see your website run with speed.

Yii-logo-transparent

Query Caching

Built on top of the Data Caching system, this is a very useful feature, especially for heavy apps that rely intensely on a Database. The concept of this feature is fairly easy but pretty solid.
Firstly, what we have to do is to define a dependency query. In other words, we define a much simpler and lighter Database Query that we will call before the one that we really need. The reason for doing that is to check if anything has changed since the last time that we executed that query.

If, for example, the data we want to retrieve is a list of Book Authors, our dependency query might well be:
SELECT MAX(id) FROM authors
By doing so, we will be able to see if any new author has been added since the last time we checked. If no new author has been added, Yii’s Cache component will take the Authors list directly from the cache, without executing again our big query, which could be something like:

Yii Query Builder

To use Query Caching with the Yii Query Builder, this is what we have to write [using the Authors’ example showed before]:

As explained before, when running this code, Yii will check for the result of the dependency query before anything else. Should it not find anything, or a different value from the one stored before, it will execute the big query and store the result in cache. Otherwise it will extract the big query result from the cache.

Active Record

It is also possible to cache the result of a query made using Active Record. The concept remains the same as explained before; but with a different syntax, of course:

For information about Data Caching with APC go to this link-
For Yii-1 version-
http://www.yiiframework.com/doc/api/1.1/CApcCache/
For Yii-2 version-
http://www.yiiframework.com/doc-2.0/guide-caching-data.html/

Comments