Best way to cache a db result

Hi,

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');

OR

Normal way to cache


	public static function myGetAll()

	{

		$id = 'categories';

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

		$value=Yii::app()->cache->get($id);

		if($value===false)

		{

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

    			/*->select()

    			->from(self::tableName())*/

				->queryAll();

			Yii::app()->cache->set($id,$value);

		}

		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?

Thanks

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 :)