Skip to content

Instantly share code, notes, and snippets.

@raybz
Last active July 14, 2019 07:26
Show Gist options
  • Select an option

  • Save raybz/c872782f850cfe874100ac401d89b2ea to your computer and use it in GitHub Desktop.

Select an option

Save raybz/c872782f850cfe874100ac401d89b2ea to your computer and use it in GitHub Desktop.
memcache
<?php
/**
* Memcache 相关操作封装
*
* @author
* @date 2014/4/25
*/
class OpMemcache {
/**
* 是否压缩
* @var isCompressd
*/
protected $isCompressd = false;
/**
* 默认过期时间
* @var expireTime
*/
protected $expireTime = 300; //默认5分钟
/**
* Memcache对象句柄
* @var $Mchandler
*/
protected $Mchandler = NULL;
/**
* MC配置
* @var $config
*/
protected $config = array();
/**
* Memcache 实例对象
*
* @var null
*/
protected static $instance = NULL;
/**
* 构造方法私有化,静止实例化,保证单例
*/
private function __construct() {
//$this->config = Configer::readKey('memcache');
global $config;
$this->config = $config['memcache'];
try{
$this->isSupportMemcache();
} catch( Exception $e ) {
echo $e->getMessage();exit;
}
$this->init();
}
/**
* 获取实例静态方法
*
* @return null|OpMemcache
*/
public static function getInstance() {
if ( self::$instance == NULL || !self::$instance instanceof OpMemcache ) {
self::$instance = new OpMemcache();
}
return self::$instance;
}
/**
* 初始化操作
*/
private function init() {
try {
if ( is_array( $this->config ) && !empty( $this->config ) ) {
if ( $this->Mchandler == NULL ) {
$this->Mchandler = new Memcache();
}
foreach ( $this->config as $idx => $item ) {
$memcachePort = $item['port'] ? $item['port'] : 11211;
$this->Mchandler->addserver( $item['host'], $memcachePort );
}
} else {
throw new Exception("memcache config is empty");
}
} catch (Exception $e ) {
echo $e->getMessage();
}
}
/**
* 检测是否已经加载memcache扩展
*
* @throws Exception
*/
public function isSupportMemcache() {
if ( ! extension_loaded('memcache') ) {
throw new Exception("memcache extension is not install");
}
}
/**
* 设置一个值
*
* @params string $key 缓存键值
* @params mixed $value 缓存值
* @params int $flag 是否压缩
* @params int $expire 过期时间
*/
public function set( $key, $value, $expire = 300 ) {
if ( $this->Mchandler != NULL ) {
return $this->Mchandler->set( $key, $value, $this->isCompressd ? MEMCACHE_COMPRESSED : 0, $expire );
}
}
/**
* 设置一个值(键值存在返回失败)
*
* @params string $key 缓存键值
* @params mixed $value 缓存值
* @params int $flag 是否压缩
* @params int $expire 过期时间
*/
public function add( $key, $value, $expire = 300 ) {
if ( $this->Mchandler != NULL ) {
return $this->Mchandler->add( $key, $value, $this->isCompressd ? MEMCACHE_COMPRESSED : 0, $expire );
}
}
/**
* 获取缓存值
*
* @params string $key
*/
public function get( $key ) {
if($key){
if(!empty($this->Mchandler)){
return $this->Mchandler->get( $key );
}
}
}
/**
* 删除缓存值
*
* @params string $key
* @params int $timeout 定时删除,默认立即删除,时间单位:秒
*/
public function del( $key, $timeout = 0 ) {
if ( $this->Mchandler != NULL ) {
return $this->Mchandler->delete( $key, $timeout );
}
}
/**
* 增加一个元素的值
*
* @params string $key
* @params int $count 要增加的值
*/
public function inc( $key, $count = 1 ) {
if ( $this->Mchandler != NULL ) {
return $this->Mchandler->increment( $key, $count );
}
}
/**
* 减小一个元素的值
*
* @params string $key
* @params int $count 要增加的值
*/
public function dec( $key, $count = 1 ) {
if ( $this->Mchandler != NULL ) {
return $this->Mchandler->decrement( $key, $count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment