Optimizing Web Application Speed with Caching in Yii Framework
- Digital Engineering
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:
1 2 |
'cache'=>array( 'class'=>'system.caching.CApcCache' ) |
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-
1 2 |
Get() Set() |
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.
1 2 3 4 5 6 7 |
// Storing the variable in Cache $val = "variable to be stored"; $uid = "myVal"; Yii::app()->cache->set($uid, $val); Once the value is saved in the database, it is very easy to access it. Here we use Get() method- Yii::app()->cache->get($uid); |
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.
1 2 3 |
Yii::app()->cache->delete($uid); to flush out all the data :- Yii::app()->cache->flush(); |
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.
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:
1 2 3 |
SELECT authors.*, book.title FROM authors JOIN book ON book.id = authors.book_id |
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]:
1 2 3 4 5 6 7 8 9 |
// big query $query = ' SELECT authors.*, book.title FROM authors JOIN book ON book.id = authors.book_id'; // dependency query $dependency = new CDbCacheDependency('SELECT MAX(id) FROM authors'); // executing query using Yii Query Builder $result = Yii::app()->db->cache(1000, $dependency)->createCommand($query)->queryAll(); The arguments passed to Yii::app()->db->cache() are, respectively, the amount of seconds that the result should be stored for and the dependency query. |
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:
1 2 |
$dependency = new CDbCacheDependency('SELECT MAX(id) FROM authors'); $authors = Author::model()->cache(1000, $dependency)->with('book')->findAll(); |
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/
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s