codeigniter: trivial memcached library

Here’s a little library I’ve used a few times to access memcached from codeigniter.

CI =& get_instance();
    
        $this->mc = new Memcached();
        if (!count($this->mc->getServerList())) {
            $slist = $this->CI->config->item('memcache_servers');
            if ($slist) {
                $this->mc->addServers( $slist );
            } else {
                $this->mc->addServer('localhost',11211);
            }
        }        
    }    

    function was_found() {
        return !$this->was_not_found();
    }

    function was_not_found() {
        return ($this->mc->getResultCode() == Memcached::RES_NOTFOUND);
    }
    
    function was_not_stored() {
        return ($this->mc->getResultCode() == Memcached::RES_NOTSTORED);
    }

    function get_raw_result() {
        return $this->mc->getResultCode();
    }

    function make_key($key) {
        $nkey = $this->CI->config->item('memcache_prefix');
        return "$nkey.$key";
    }

    function get($key)
    {
        return $this->mc->get($this->make_key($key));
    }
    
    function set($key,$value,$expiration=600)
    {        
        return $this->mc->set($this->make_key($key),$value,$expiration);
    }
}

?>

Related Posts:

This entry was posted in Programming and tagged , , . Bookmark the permalink.

Comments are closed.