简体   繁体   English

无法在Codeigniter中缓存对象

[英]Can't caching object in codeigniter

I have this following code in the Address controller 我在地址控制器中有以下代码

public function index()
    {
        if($this->session->userdata('isLogin')) {
            $this->load->driver('cache');
            $this->load->model('MemberModel');
            if(!$this->cache->get('province') == false) {
                $this->load->model('ShippingModel');
                $data['provinces'] = $this->ShippingModel->the_provinces(); // it will return json object
                $this->cache->save('province', $data['provinces'], 300);
            } else {
                $data['provinces'] = $this->cache->get('province');
            }
            $userdata = $this->MemberModel->getProfile($this->session->userdata('userid'));
            $data['user'] = $userdata;

            $this->display_member_area('member/address', $data);
        }
        else {
            redirect(base_url());
        }
    }

When I want to get the data using: 当我想使用以下方法获取数据时:

var_dump($this->cache->get('province')); 

the result I get always shows 我得到的结果总是显示

bool(false)

but when I tried to do this instead 但是当我尝试这样做时

var_dump($data['provinces']) // it's show me json object, that I want

Can anyone please show me where I'am doing wrong? 谁能告诉我我做错了什么地方?

Thanks in advance 提前致谢

Remove false from the if condition and use this code. if条件中删除false并使用此代码。

       if(!$this->cache->get('province')) {
            $this->load->model('ShippingModel');
            $data['provinces'] = $this->ShippingModel->the_provinces();
            $this->cache->save('province', $data['provinces'], 300);
        } else {
            $data['provinces'] = $this->cache->get('province');
        }

Make sure you have the correct credential on your config at: 确保您在以下位置具有正确的凭据:

https://github.com/bcit-ci/CodeIgniter/blob/develop/application/config/memcached.php https://github.com/bcit-ci/CodeIgniter/blob/develop/application/config/memcached.php

it will be at you application/config/memcached.php file. 它将位于您的application/config/memcached.php文件中。 Hostname is the ip you setup your memcached instance. 主机名是您设置memcached实例的IP。 You could provide multiple server if you wish. 您可以根据需要提供多个服务器。

reference on installing memcached 有关安装memcached的参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM