Caching

In the form of a model, need to use the dropdown list. The values for dropdown list selected from database table of another model. Now i want to use the cache in this. Storing the drop list values and use that cache in the form of drop down list. I read the wiki in Yii site but not clear about which method is used for that. When i can perform this?

In your second model, you could create a method to return the drop down list options and handle caching within that. Something like the following (with more appropriate names and keys):




    public static function getOptions()

    {

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


        if (!$result)

        {

            $result = getOptionsHere();

            Yii::app()->cache->set('ClassName_Options', $result, 3600);

        }


        return $result;

    }



Note that you’ll have to heavily modify this depending on how you populate the options. The function may or may not need to be static and your cache key may have to have additional identifying information.

Also you sould check if the cache is enabled because when in feature disable the cache you will some issues to fix

I suppose you could handle the missing cache component like so:




    public static function getOptions()

    {

        if (Yii::app()->cache === null)

            return getOptionsHere();


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


        if (!$result)

        {

            $result = getOptionsHere();

            Yii::app()->cache->set('ClassName_Options', $result, 3600);

        }


        return $result;

    }



I’m not overly fond of the two return statements, but that seems the easiest way to handle it.

Which changes are needed in the config/main.php…? and what you mean by this ‘ClassName_Options’?


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

Have another doubt. How can i use this "return $result;" in the dropdown list of the form?

I wanted to give a little - one more sugestion, its ok :)

How can i use this return data in the from drop down list?

Regarding changes to config, you’ll need to configure a cache component. I often use the file caching mechanism:




'cache'=>array(

    'class'=>'system.caching.CFileCache',

),



It’s up to you to get the return value into a form suitable for dropdown list options. I suspect that you’ll want to perform some sort of find request and use CHtml::listOptions() to get them into an associative array. This should be done as part of the getOptionsHere() step.

Treat the code above as a guide on how to solve the problem. You’ll need to adapt it for your own situation.

I want to use memory for cache not a file. For using memory how i cache the countries?

And i’m not clear the Classname_options in that function and how can access cache in dropdownlist?

could you give us the code of getOptionsHere?

"How can i use this return data in the from drop down list"

did you mean data to drop down list? or vice versa?

‘class’=>‘CMemCache’, from memory but you have to install it on your server

for more information about supported cache in Yii see

http://www.yiiframework.com/doc/api/1.1/#system.caching

To use memory, simply use a different cache class in your config (see subclasses here), everything else remains the same.

In my concept the country list is used in many forms of my application so for each form dropdown list i want to use this cache. A Global cache variable for used in every form drop down. Which way is better to implement this?

This is essentially global. The code to fetch the list should be in your Country model. This method is accessible from anywhere in your application by using Country::getOptions().

Please use a more appropriate method name than that though :)

Where i put this function? In a model or controller?

Yii have any method for using a variable for perform this by using the class file in components?

In the Country model. Avoid putting global methods into controllers as not all controllers are accessible in every request; your base controller notwithstanding.