How to use memcached from codeigniter

2019-03-15 12:30发布

问题:

How to use memcached from codeigniter, and how to store session data to memcached. Please help me.

Thanks

回答1:

Here is the link to my memcached_library for codeigniter

http://github.com/tomschlick/memcached-library

let me know what you think and if you have any issues please raise them in the issues section of the github repository



回答2:

Codeigniter V2.1.0 supports caching http://codeigniter.com/user_guide/libraries/caching.html#memcached



回答3:

Here is an introduction to memcached and PHP:

enhance_php_session_management

As far as using memcached from CI, I imagine you would want to either add the caching code directly into your models, or from your Controllers you would want to check the cache before querying data from a model.



回答4:

public function index()
    {
        // manual connection to Mamcache
        $memcache = new Memcache;
        $memcache->connect("localhost",11211);

        $data=$memcache->get("test_key");

        if($data){
            echo 'cache data:';
            var_dump($data);
        }else{
            $data=$this->db->query("SELECT count(*) as ca FROM table WHERE typ=1 ")->row();
            $memcache->set("test_key",$data,false,10); // 10 seconds
            echo 'real data:';
            var_dump($data);
        }

    }