<?php
/**
* Memcache Cache operation
* @author HXM
* @version 1.0
* @since 2015.05.04
*/
Class Mcache extends Object implements Cacheface
{
Private $mem = null; Mem Object
Private $sId = 1; Servier Service ID
/**
* Initialize Memcache
*
* @return Object
*/
Public Function __construct ()
{
if (!class_exists (' Memcache '))
{
throw new Qexception (' PHP extension does not exist:memcache ');
}
$this->mem = new Memcache ();
}
/**
* Link MEMCAHCE Service
*
* @access Private
* @param string $key keyword
* @param string $value cache content
* @return Array
*/
Private function Connect ($SID)
{
$file = $this->cachefile ();
Require $file;
if (! isset ($cache))
{
throw new Qexception (' cache config file does not exist '. $file);
}
$server = $cache [$this->cacheid];
$sid = Isset ($sid) = = 0? $this->sid: $sid;//memcache service Selection
if (! $server [$sid])
{
throw new Qexception (' The cache server configuration file for the current operation does not exist ');
}
$host = $server [$sid] [' Host '];
$port = $server [$sid] [' Port '];
try {
$this->mem->connect ($host, $port);
} catch (Exception $e) {
Exit (' Memecache connection failed, error message: '. $e->getmessage ());
}
}
/**
* Write Cache
*
* @access Private
* @param string $key keyword
* @param string $value cache content
* @return Array
*/
Public function set ($key, $value, $sid, $expire = 0)
{
$data = $this->get ($key, $sid); If a key value already exists
if ($data)
{
return $this->mem->set ($key, $value, memcache_compressed, $expire);
} else {
return $this->mem->add ($key, $value, memcache_compressed, $expire);
}
}
/**
* Read Cache
*
* @access Private
* @param string $key keyword
* @param int $sid Select the first few memcache servers
* @return Array
*/
Public function Get ($key, $sid)
{
$this->connect ($SID);
return $this->mem->get ($key);
}
/**
* Clean (delete) all the elements that have been stored
*
* @access Private
* @return Array
*/
Public Function Flush ()
{
$this->connect ();
return $this->mem->flush ();
}
/**
* Delete Cache
*
* @access Private
* @param string $key keyword
* @param int $sid Select the first few memcache servers
* @return Array
*/
Public function Remove ($key, $sid)
{
$this->connect ();
return $this->mem->delete ($key);
}
/**
* destructor
* Last Close Memcache
*/
Public Function __destruct ()
{
/*if (! $this->mem)
{
$this->mem->close ();
}*/
}
}
PHP memcache Basic Operations