Memcache wrapper class

by jiri
A simple wrapper for PHP's Memcache functions
Used by the Memcache component and helper. You can get the full package with instructions from my site.

Following goes in /app/vendors/Memcache.php:


<?php
/**
 * Wrapper for Memcache, v. 0.3
 *
 * By Jiri Kupiainen (http://jirikupiainen.com/)
 *
 * You are free to do whatever you please with this code. Enjoy.
 */
class CakeMemcache {
        var 
$_connected false;
        var 
$_Memcache null;

        var 
$servers = array('127.0.0.1:11211'); // you can add more servers by adding their hostname and port to this array. if port is default (11211), it can be omitted.

        /**
         * Connect to the memcached server(s)
         */
        
function _connect() {
                if (
defined('DISABLE_CACHE')) {
                        return 
false;
                }

                
$this->_Memcache =& new Memcache();

                
// several servers - use addServer
                
foreach ($this->servers as $server) {
                        
$parts explode(':'$server);

                        
$host $parts[0];
                        
$port ife(isset($parts[1]), $parts[1], 11211); // default port

                        
if ($this->_Memcache->addServer($host$port)) {
                                
$this->_connected true;
                        }
                }

                return 
$this->_connected;
        }

        
/**
         * Set a value in the cache
         *
         * Expiration time is one hour if not set
         */
        
function set($key$var$expires 3600) {
                if (
defined('DISABLE_CACHE') || !$this->_connected) {
                        return 
false;
                }

                if (!
is_numeric($expires)) {
                        
$expires strtotime($expires);
                }
                if (
$expires 1) {
                        
$expires 1// don't allow caching infinitely
                
}

                return 
$this->_Memcache->set($key$var0time()+$expires);
        }

        
/**
         * Get a value from cache
         */
        
function get($key) {
                if (
defined('DISABLE_CACHE') || !$this->_connected) {
                        return 
false;
                }

                return 
$this->_Memcache->get($key);
        }

        
/**
         * Remove value from cache
         */
        
function delete($key) {
                if (
defined('DISABLE_CACHE') || !$this->_connected) {
                        return 
false;
                }

                return 
$this->_Memcache->delete($key);
        }
}
?>

Report

More on Snippets

Advertising

Comments

  • mariano posted on 04/14/07 10:54:13 PM
    Can you apply CakePHP coding standards? Thanks.
  • jiri posted on 04/14/07 05:56:05 AM
    Bug fixed. Thanks for the info.
  • rikman posted on 04/14/07 02:32:03 AM
    In the set-Method is a bug:

    PHP's original memcache::set method takes the expire time as 4th parameter. In your call to memcache::set it's called with expire time as 3rd parameter which leads to strange things (take a look in the memcache log file).

    I fixed it by replacing the line with the memcache::set call with

    return $this->_memcache->set($key, $var, 0, time()+$expires);

    The 3rd argument controls the use of on the fly data compression.

    See also http://de3.php.net/manual/en/function.Memcache-set.php
login to post a comment.