Caching For Country List

For caching the country list (used in drop list) by using api, which is the best way or which cache is best in Yii framework? When we can set cache the country list…?

It sounds like it might be best to simply cache the query used to generate the country list, as long as it doesn’t change very often (it shouldn’t).




$cache = Yii::app()->cache->get('countryListCache');                                                                                                         

                                                                                                                                                                          

        if( $cache !== false ) $data = $cache;                                                                                                                            

        else                                                                                                                                                              

        {                                                                                                                                                                 

                $data = $this->getData();                                                                                                                                 

                Yii::app()->cache->set('countryListCache, $data, 10800);                                                                                 

        } 



$this->getData() is the method where you will call country list, maybe Country::model()->getListData() could replace it. The 10800 is time in seconds, which means 180 minutes. You should be able to check your application log to verify that it is being served from cache.

Using this method you must make sure that your country data is stored in cache with a unique name–there are cases when it needs to change per user; not your case, but keep that in mind.

This code attempts to load the cache; if it is set then it sets your data variable with its contents, otherwise it gets the results from the database and saves them to the cache for next time. Good luck.

Is there any method for cache the country list when application starts? Means when we open the application we need to cache the country list.