[solved] Surprising difficulties using CCacheDependency

Hi

I am trying to make use of the CCacheDependency class to control the COutputCache widget. The problem is I have the generateDependentData value already retrieved, so I do not want to refetch this value. But I am having problems indicating this to the COutputCache widget.

I tried



class ZCacheDependency extends CCacheDependency {


   public $updateDate=false;


  


  public function generateDependentData() {


    


return $this->updateDate;


  }


}


And in creating the widget I do



        $dependency = new ZCacheDependency;


        $keys = array('id'=>$route.


                      " ".$content->moduleDataId, 


                      'dependency'=>$dependency);


        $dependency->updateDate = $content->updateDate;


        $cache = $this->createWidget('COutputCache',$keys);


...


But this does not work as expected because the ZCacheDependency is serialized and the new instance is not used when checking to see if the cache is valid.

So I changed things to do this



class ZCacheDependency extends CCacheDependency {


   public $updateDate=false;


  


  public function generateDependentData() {


    


return Yii::app()->params['updateDate'];


  }


}




Yii::app()->params['updateDate'] = $content->updateDate;


        $dependency = new ZCacheDependency;


        $keys = array('id'=>$route.


                      " ".$content->moduleDataId, 


                      'dependency'=>$dependency);


        $cache = $this->createWidget('COutputCache',$keys);





Ugly but it works, I guess I could also have used a static value also, but is there a better way ?

Thanks

nz

I think you can write __sleep() method to clear the $updateDate variable before the dependency instance is being serialized.

But would not the cached value be always invalid ?

My problem could be resolved if by changing the CCache::get($id) and ICacheDependency::getHasChanged() methods.

If both accepted another mixed value parameter then the ICacheDependency class could be written in a manner in which it could intelligently determine (based on the data passed to it) whether or not the cache is stale.

So storing the object would work as normal, but checking to see if the cache is valid would pass in an extra object to the CCache::get($id, $data=false) which in turn would be passed to ICacheDependency::getHasChanged($data=false).

Thoughts ?

nz

Sorry, I misunderstood you.

Nope, adding the additional parameter would make the whole thing ugly.

The essence of your dependency is that it depends on some external data that you set somewhere. Your current solution is fine (using app's params or using a static variable of the dependency class).

Cool , thanks

nz