download this file: class.memcache.php view text/plain: class.memcache.php file encoding: UTF-8 [goback]
<?php
##
## this file name is 'class.memcache.php'
##
## mcache object
##
## [author]
##  - Chilbong Kim<san2(at)linuxchannel.net>
##
## [changes]
##  - 2004.12.20 : support pecl-memcache-1.4, add multi get(mget())
##  - 2004.06.16 : new build
##
## [references]
##
## [example]
##  require_once 'class.memcache.php';
##  $sql = 'SELECT ... FROM ... WHERE ...';
##  $mcache = new mcache; // none debug mode
##  $mcache->connect('localhost',11211);
##  $result = $mcache->get($sql);
##  $mcache->close();
##  print_r($result);
##
## [available of functions, class(member functions)], grep '{{{ proto' memcache.c
##  - object memcache_connect( string host [, int port [, int timeout ] ])
##  - object memcache_pconnect( string host [, int port [, int timeout ] ])
##  - string memcache_get_version( object memcache ) /*** obj->getversion() ***/
##  - bool   memcache_add( object memcache, string key, mixed var [, int flags [, int expire ] ] )
##  - bool   memcache_set( object memcache, string key, mixed var [, int flags [, int expire ] ] )
##  - bool   memcache_replace( object memcache, string key, mixed var [, int flags [, int expire ] ] )
##  - mixed  memcache_get( object memcache, mixed keys ) /*** support multi get, in array ***/
##  - bool   memcache_delete( object memcache, string key, [, int flags [, int expire ] ] )
##  - bool   memcache_debug( bool onoff ) /*** not exists member fuunction ***/
##  - array  memcache_get_stats( object memcache ) /*** obj->getststs() ***/
##  - int    memcache_increment( object memcache, string key [, int value ] )
##  - int    memcache_decrement( object memcache, string key [, int value ] )
##  - bool   memcache_close( object memcache )
##  - bool   memcache_flush( object memcache )
##
## [url]
##  - http://pecl.php.net/package-info.php?package=memcache ; // pecl extension download
##  - http://cvs.php.net/pecl/memcache ; // example of usage
##  - http://www.php.net/manual/en/ref.memcache.php ; // only CVS version
##

class mcache
{
  var 
$debug FALSE;    // boolean, 1(true), 0(false), debug mode
  
var $link;        // resource link id

  ## set debug mode
  ##
  
function &mcache($debug=FALSE)
  {
    
$this->debug $debug;
    if(
$debug$this->debug($this->debug);
  }

  
## memached connect
  ##
  ## return :
  ##    $link    resource link id
  ##
  
function &connect($host$port=11211$timeout=2)
  {
    
//define('__CACHESTART__',microtime());

    
$this->link memcache_connect($host,$port,$timeout);

    return 
$this->link;
  }

  
## memcached pconnect
  ##
  
function &pconnect($host$port=11211$timeout=2)
  {
    
$this->link memcache_pconnect($host,$port,$timeout);

    return 
$this->link;
  }

  
## memcache close
  ##
  
function &close($link='')
  {
    
$link $link $link $this->link;
    
$result = @memcache_close($link);

    
//define('__CACHEEND__',microtime());

    
return $result// boolean
  
}

  
## memcache_add
  ##
  
function &add($sql$var$expire=10)
  {
    return 
memcache_add($this->link,md5($sql),$var,2,$expire); // MEMCACHE_COMPRESSED = 2
  
}

  
## Sets the value of an item. Item may exist or not
  ##
  
function &set($sql$var$expire=10)
  {
    return 
memcache_set($this->link,md5($sql),$var,2,$expire); // MEMCACHE_COMPRESSED = 2
  
}

  
## Returns value of existing item or false
  ## -- patch 2004.12.20, support multi-get
  ##
  
function &get($sql)
  {
    if(
is_array($sql))
    {
        
//$key = array_map(array($this?$this:'mcache','_callback_md5'),$sql);
        
$key array_map('md5',$sql);
        
$arr memcache_get($this->link,$key);

        
$r = array(); // reset
        
$size sizeof($sql);

        
// merge , 32 bit char
        
for($i=0$i<$size$i++) $r["$sql[$i]"] = $arr["$key[$i]"];
    }
    else 
$r memcache_get($this->link,md5($sql));

    return 
$r// mixed or associative array
  
}

  
## _callback_md5
  ##
  
function &_callback_md5($val)
  {
    return 
md5($val);
  }

  
## Replaces existing item. Returns false if item doesn't exist
  ##
  
function &replace($sql$var$expire=10)
  {
    return @
memcache_replace($this->link,md5($sql),$var,1,$expire);
  }

  
## memcache_increment
  ##
  
function &increment($sql$var=0)
  {
    if(
$var) return memcache_increment($this->link,md5($sql),$var);
    else return 
memcache_increment($this->link,md5($sql));
  }

  
## memcache_decrement
  ##
  
function &decrement($sql$var=0)
  {
    if(
$var) return memcache_decrement($this->link,md5($sql),$var);
    else return 
memcache_decrement($this->link,md5($sql));
  }

  
## Deletes existing item
  ##
  
function &delete($sql)
  {
    return @
memcache_delete($this->link,md5($sql));
  }

  
## Flushes cache
  ##
  
function &flush()
  {
    return @
memcache_flush($this->link);
  }

  
## returns server's statistics
  ##
  
function &getstats()
  {
    return 
memcache_get_stats($this->link); // array
  
}

  function &
get_stats()
  {
    return 
$this->getstats();
  }

  function &
getversion()
  {
    return 
memcache_get_version($this->link);
  }

  function &
get_version()
  {
    return 
$this->getversion();
  }

  function &
debug($debug)
  {
    return 
memcache_debug($debug);
  }
// end of class

class memcached extends mcache {}
class 
mcd extends mcache {}

?>