Best way to cache a db result


what will be best way to cache a query result

Table: category {id:int, name:varchar}

so what will be better in performance?

$dependency = new CDbCacheDependency('SELECT MAX(id) FROM category');


Normal way to cache

	public static function myGetAll()


		$id = 'categories';

		$sql = 'SELECT * FROM '.self::tableName().' ORDER BY name ASC';




			$value = Yii::app()->db->createCommand($sql)






		return $value;


setting a very long time lime limit or unlimited and when a new category got added by and admin, updates and refresh categorys cache?


please any one?

I think It depends on your situation.

If the table records changes every time(like ads in big ads portal) then use second way with little time.

CDbCacheDependency anyway gives a query to db, so you should be carefull to use it.

thanks :)