Code doubles the number of cache files instead of creating just one set in codeigniter
I have a function that I need to cache in PHP. So I created one cache file for the controller and another for the view. Cache files are generated in my cache folder, but a different set of cache files are generated with it.
Here is my controller code:
function ajax_best_tag_call(){
$postcat= $_POST['catId'];
$ifLanguage = $this->session->userdata('lang');
$BestTagcache_fileName = 'bestTagCall/BestTag_'.$ifLanguage.'_'.$_SESSION['cityAlias'].'_'.$postcat.'_controller';
if(!$data = $this->cache->file->get($BestTagcache_fileName)){
$data = array();
$besttag_count = $this->input->post('besttag_count');
$besttag_neglected = $this->input->post('besttag_neglected');
$total_shown = $besttag_count + $besttag_neglected;
$excludeVenues = $this->input->post('exclude_venues');
$data['response'] = $this->best_tag_data($postcat,$besttag_count,$total_shown,$excludeVenues);
$data['BesttagArr'] = $data['response'] ;
$data['catId'] = $postcat;
$data['exclude_venues'] = $data['response']['exclude_venues'];
$data['total_best_tag'] = $data['response']['total_best_tag'];
$this->cache->file->save($BestTagcache_fileName, $data,86400);//cache this page for 1 day
}
$ajaxresponse = $this->load->view("best_tag_ajax_data",$data,true);
$content_ajax_send = array("html_detail"=>$ajaxresponse,"total_best_tag"=>$data['total_best_tag'],"total_shown"=>count($data['response']['mainarray']),"exclude_venues"=>$data['response']['exclude_venues'],"neglected"=>count($data['response']["neglected"]),"neglected_tags"=>$data['response']["neglected"]);
echo json_encode($content_ajax_send);
}
and here is the code like:
<?php
$ifLanguage = $this->session->userdata('lang');
$BestTagcache_file = 'bestTagCall/BestTag_'.$ifLanguage.'_'.$_SESSION['cityAlias'].'_'.$catId.'_view';
if(!$html_detail = $this->cache->file->get($BestTagcache_file)){
$html_detail = '';
foreach($BesttagArr['mainarray'] as $value)
{
$best_tag_name = $value[3];
$UrlArr = explode("/",$value[1]);
$lastUrlText = end($UrlArr); unset($UrlArr[count($UrlArr)-1]);
$urlFinal = implode("/",$UrlArr)."/".$lastUrlText;
$bestUrl = $urlFinal;
$html_detail .='<div class="col-lg-4 besttags">
<div class="img_box addtransform">
<a href="'.$bestUrl.'">
<img class="four_by_three" src="'.$value[0].'">
<div class="img_Title" id="img_Titles">
<div class="c_title"></div>
<div class="c_subtitle">'.ucwords(traslate_word($best_tag_name)).'</div>
</div>
</a>
</div>
</div>';
}
$this->cache->file->save($BestTagcache_file, $html_detail,86400);//cache this page for 1 day
}
echo $html_detail;
?>
and here is a screenshot of the files that are generated
As you can see, the first two files are exactly what I did in the code, but the last two are not intended. the first two have information, but the second second does not provide complete information. I don't understand how the last two files are generated. Please, help.
source to share
Do not add code to cache on the view. I would recommend that you only do this in your controller, seeing that the routing will call you controller before the view is rendered.
So when your route is called a cached file, it gets created and over again when your view is eventually rendered.
source to share