Manage Two Different Cache file in single cache fo

2019-08-08 16:05发布

I need a help to figure out one issue with the codeigniter caching.

I am running two functions to store a result in cache. This function is in my model :

public function cacheAllCurrencies()
    {
        $this->db->cache_on();
        $this->db->select("name,icon,currency_code");
        $this->db->from("currency");
        $this->db->where("status='Active'");
        $cache_currency_result = $this->db->get()->result();
        $this->db->cache_off();
        return $cache_currency_result;
    }

    public function cacheAllCategory()
    {
        $this->db->cache_on();
        $this->db->select("name,url");
        $this->db->from("category");
        $this->db->where("parent_category='0'");
        $this->db->where("status='Active'");
        $this->db->order_by('name','ASC');
        $cache_category_result = $this->db->get()->result();
        $this->db->cache_off();
        return $cache_category_result;
    }

My these two functions are called in header view like below :

$CI =& get_instance();
$CI->load->model(PUBLIC_DIR.'/commonPage','common');
$currencies = $CI->common->cacheAllCurrencies();
$categories = $CI->common->cacheAllCategory();

Now, when all the page loads, it creates a cache file according to the page opened like home, blog, blog+blogname etc.

Both query generates two cache file in cache folder

1580e4c2413cb09f6ed3bc7fae8cee45 - first function cache result d7e2452b0424f859e1a5984bd26cbd6c - second function cache result

Now, I have two questions :

  1. I need to delete 1580e4c2413cb09f6ed3bc7fae8cee45 cache file when I update currency table same for the category.
  2. How this file name generated ? I mean how codeigniter generates cache file name. In my cache 1580e4c2413cb09f6ed3bc7fae8cee45 for currency and d7e2452b0424f859e1a5984bd26cbd6c for category.

I hope this explanation makes sense and I hope most of the codeigniter developer having this problem which need to be sort it out.

Thanks, Ali

1条回答
趁早两清
2楼-- · 2019-08-08 16:54

In Codeigniter, you can clear the cache of DB using the table name like

$this->db->cache_delete('currency');
$this->db->cache_delete('category');

OR two table cache at the same time

$this->db->cache_delete('currency','category');

EDIT : CodeIgniter save filename by md5() encryption of SQL query

public function cacheAllCurrencies(){
    $this->db->cache_on();
    $this->db->select("name,icon,currency_code");
    $this->db->from("currency");
    $this->db->where("status='Active'");
    //here you get filename
    $file_name=md5($this->db->get_compiled_select());
    $cache_currency_result = $this->db->get()->result();
    $this->db->cache_off();
    return $cache_currency_result;
}
查看更多
登录 后发表回答