【问题标题】:Manage Two Different Cache file in single cache folder in codeigniter在codeigniter的单个缓存文件夹中管理两个不同的缓存文件
【发布时间】:2017-12-31 02:03:04
【问题描述】:

我需要帮助来找出与 codeigniter 缓存有关的一个问题。

我正在运行两个函数来将结果存储在缓存中。这个函数在我的模型中:

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

现在,当所有页面加载时,它会根据打开的页面(如主页、博客、博客+博客名称等)创建一个缓存文件。

两个查询都在缓存文件夹中生成两个缓存文件

1580e4c2413cb09f6ed3bc7fae8cee45 - 第一个函数缓存结果 d7e2452b0424f859e1a5984bd26cbd6c - 第二个函数缓存结果

现在,我有两个问题:

  1. 当我更新该类别的货币表时,我需要删除 1580e4c2413cb09f6ed3bc7fae8cee45 缓存文件。
  2. 这个文件名是怎么产生的?我的意思是codeigniter 如何生成缓存文件名。在我的缓存中 1580e4c2413cb09f6ed3bc7fae8cee45 用于货币,d7e2452b0424f859e1a5984bd26cbd6c 用于类别。

我希望这个解释是有道理的,我希望大多数 codeigniter 开发人员遇到这个问题需要解决。

谢谢, 阿里

【问题讨论】:

  • 2.看起来像 MD5。
  • @WillParky93 :是的,它似乎就像 md5。但是什么名字被转换成 md5 呢?
  • codeigniter第660行,缓存是从完整的uri路径创建的
  • @WillParky93 : 这两个查询运行的不同
  • $file_name=md5($this->db->get_compiled_select());这就是答案:)

标签: php mysql codeigniter caching


【解决方案1】:

Codeigniter中,可以使用表名清除DB的缓存 喜欢

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

OR 同时缓存两个表

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

编辑: CodeIgniter 通过 SQL 查询的 md5() 加密保存文件名

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;
}

【讨论】:

  • 当我应用这段代码时,它会生成很多其他缓存文件
  • 但是当应用你的代码时它会给我这个名字“c374e48f9b2cf50386c051b6dbec427b”这是不正确的
  • 检查system/database/DB_cache.php中的write()函数可能是你的ci diff版本,使用diff逻辑来存储缓存文件。
  • 不,它现在正在工作.. 但不能为另一个功能做同样的事情。它给出了没有选择表的数据库错误
  • 两者都工作正常。我需要在单行而不是多行中运行查询: $sql = $this->db->select('name,url')->from('category')->where("parent_category='0'" )->where("status='Active'")->order_by('name','ASC')->get_compiled_select();;回声 md5($sql);出口;然后它会给我确切的名字:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
相关资源
最近更新 更多