Codeigniter缓存 Controller 问题

标签 codeigniter caching controller

我有 2 个 Controller :

cart/
cart/buy

在两者中显示库购物车的内容

    <tbody>
        <?php foreach($this->cart->contents() as $items): ?>
        <tr>
            <td><?php echo $items['name'] ?></td>
            <td>$ <?php echo $this->cart->format_number($items['price']); ?></td>
            <td><?php echo $items['qty'] ?></td>
            <td>$ <?php echo $this->cart->format_number($items['subtotal']); ?></td>
        </tr>
            <?php endforeach; ?>
    </tbody>

我的问题是,当我将第一个商品添加到购物车时, Controller 购买仍保留在缓存中。我的意思是, Controller cart/ 有 5 件商品, Controller cart/buy 有 1 件商品。我必须按 Ctrl + F5 才能查看所有项目

我能够解决这部分问题:

function buy()
    {

        $this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
        $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        $this->output->set_header('Pragma: no-cache');
        $this->output->set_header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');  

        if($this->cart->contents())
        {

            $this->load->view('web/products/buy_view');
        }
        else
        {
            redirect('cart');   
        }       

    }

但是你看,我想知道购物车中是否有数据,如果购物车是空的,则重定向到另一个页面。

显然if($ this->cart->contents()),“保留在缓存中”,例如购物车可以填充cart/,但是购物车/购买中为空,直到我按 Ctrl F5 条件仍然失败。

有什么办法可以解决这个问题,或者也许我做错了什么?

<小时/>

p.d.我在购物车 Controller 中的添加方法:

function add_item()
    {
        if($this->cart_model->validate_add_item() == TRUE)
        {  
            redirect('cart');
        }
    }

我在购物车模型中的添加方法:

function validate_add_item()
    {
        $id = $this->input->post('producto_id'); 
        $cantidad = $this->input->post('cantidad');

        $this->db->select('vNombre, dPrecio');
        $this->db->where('iIdProducto', $id);
        $query = $this->db->get('product', 1);

        if($query->num_rows > 0)
        { 

            foreach ($query->result() as $row)  
            {  

                $data = array(  
                        'id'      => $id,  
                        'qty'=> $cantidad,  
                        'price'  => $row->dPrecio,  
                        'name'  => $row->vNombre  
                );  


                $this->cart->insert($data);   

                return TRUE;    
            }
        }
        else
        {  
            return FALSE;  
        }   
    }

最佳答案

我没有发现你的代码有任何问题。我认为这是您提到的缓存问题,因此您可以尝试此方法来清除缓存。这将清除 codeigniter 之前创建的所有缓存,并防止将来创建缓存:

    $this->load->driver('cache');
    $this->cache->clean();

    $this->output->cache(0);

关于Codeigniter缓存 Controller 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9879620/

相关文章:

php - 在 codeigniter 中发现数据库中有重复数据后显示提示

caching - 仅使用 RxJS 操作符缓存 Http 请求

javascript - HTA 文件缓存导致重大问题

php - 使用 codeigniter 从表中进行条件选择

php - fsockopen() : php_network_getaddresses: getaddrinfo failed: Name or service not known

ruby-on-rails - 从 Controller 传递变量以查看

json - 如何在 Odoo Controller 中获取 JSON 数据?

yii - Yii2 Controller 与 Yii1 Controller 的结合

mysql - Codeigniter Active Record JSON 对象中的值

firebase - Cloudflare 和 Firebase