-
-
Save deniapps/bb1df00975b52def248887860bb09057 to your computer and use it in GitHub Desktop.
The wrapper of PHP-Memcached
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* There are two PHP Memcache extensions available from the PHP Extension Community Library: PHP Memcached and PHP Memcache. | |
* This wrapper is for "PHP Memcached" | |
* | |
* Usuage: see in the comment | |
* | |
* | |
*/ | |
class CacheMemcached | |
{ | |
public $iTtl = 600; // Time To Live (number of second) | |
public $bEnabled = true; // Memcached enabled? | |
public $oCache = null; | |
// constructor | |
public function __construct() | |
{ | |
if (class_exists('Memcached')) { | |
$this->oCache = new Memcached(); | |
if (!$this->oCache->addServer('localhost', 11211)) { | |
$this->oCache = null; | |
$this->bEnabled = false; | |
} | |
} else { | |
$this->bEnabled = false; | |
} | |
} | |
// get data from cache server | |
public function getData($sKey) | |
{ | |
$vData = $this->oCache->get($sKey); | |
return false === $vData ? null : $vData; | |
} | |
// save data to cache server | |
public function setData($sKey, $vData) | |
{ | |
return $this->oCache->set($sKey, $vData, $this->iTtl); | |
} | |
// delete data from cache server | |
public function delData($sKey) | |
{ | |
return $this->oCache->delete($sKey); | |
} | |
// flush data from cache server | |
public function flushData() | |
{ | |
return $this->oCache->flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example to use this Wrapper: