PHP memcache redis类
来源:互联网 发布:网络炒作事件 编辑:程序博客网 时间:2024/06/16 07:15
<?phpglobal $config;$memcache = $config['memcache'];class MemClass{ private $mmc = NULL; private static $instance; public static function getInstance(){ if(!(self::$instance instanceof self)){ self::$instance = new self(); } print_r(self::$instance);die; return self::$instance; } private function __construct() { if(!($this -> mmc instanceof Memcache)){ $this -> mmc = new Memcache; } //实现addServer功能 foreach($GLOBALS['memcache'] as $key => $value) { $this -> mmc -> addServer($value['host'] , $value['port'],true,$value['weight']); } } //读取缓存 public function get($key) { return $this->mmc->get($key); } //设置缓存 public function set($key,$value,$expire = 86400) { return $this -> mmc -> set($key , $value ,MEMCACHE_COMPRESSED,$expire); } //添加缓存 public function add($key, $value, $expire = 86400) { if(!$this->mmc->get($key)){ return $this->mmc->add($key, $value,MEMCACHE_COMPRESSED,$expire); }else{ echo "设置失败,该键值被已被注册"; return false; } } //替换缓存 public function replace($key, $value, $expire = 1800){ return $this->mmc->replace($key, $value,MEMCACHE_COMPRESSED,$expire); } //自增1 public function inc($key, $value = 1) { return $this->mmc->increment($key, $value); } //自减1 public function des($key, $value = 1) { return $this->mmc->decrement($key, $value); } //删除 public function del($key) { return $this->mmc->delete($key); } //全部清空 public function clear() { return $this->mmc->flush(); } //关闭缓存 public function close() { return $this->mmc->close(); }}
//REDIS
<?php/** * redis 单例 * Class Redis * @package common\models */class RedisClass{ /** * 实例数组 * @var array */ private static $_instance = array(); /** * 生成实例 * Redis constructor. * @param $sHostName * @param $iPort * @param $iDb * @param $sPassword */ private function __construct($sHostName, $iPort, $iDb, $sPassword) { $oRedis = new Redis(); $oRedis->pconnect($sHostName, $iPort); $sPassword && $oRedis->auth($sPassword); $oRedis->select($iDb); $sKey = $sHostName . $iPort . $iDb; self::$_instance[$sKey] = $oRedis; } /** * 防止克隆 */ public function __clone() { trigger_error('Clone is not allow!', E_USER_ERROR); } /** * 取redis 实例 * @param int $iDb * @param string $sHostName * @param int $iPort * @param string $sPassword * @return Redis * @throws Exception */ public static function getInstance($iDb = 1, $sHostName = '127.0.0.1', $iPort = 6379, $sPassword = '') { try { $sKey = $sHostName . $iPort . $iDb; if (!isset(self::$_instance[$sKey])) { new self($sHostName, $iPort, $iDb, $sPassword); } return self::$_instance[$sKey]; } catch (Exception $e) { throw new Exception('redis 配置错误' . $e->getMessage()); } }}
阅读全文
0 0
- PHP memcache redis类
- php memcache类(memcache队列)
- liunx php 安装redis memcache 扩展
- memcache redis 下载地址 php扩展
- php redis 和memcache 的扩展
- PHP Memcache类
- php 封装memcache类
- redis/memcache
- Redis&Memcache
- memcache redis
- memcache/redis
- Redis、memcache
- redis memcache
- redis/memcache
- memcache redis
- php-fpm的安装与连接memcache、redis
- php session存储到文件、memcache或redis
- MAC PHP+nginx+mysql+redis+memcache+mongo....环境配置
- 兔子数列变形
- Maven 多环境配置:开发,测试,生产环境
- java中枚举
- [Servlet] Java Web乱码
- LintCode Introduce Java & Coding Interview
- PHP memcache redis类
- Spring Web MVC(三)之注解
- ECP系统J2EE架构开发平台
- Activiti进阶(六)——连线(SequenceFlow)
- mysql官网下载旧版本
- AJAX实现局部刷新
- 错误:Mixed Content: The page at ‘https://XXX’ was loaded over HTTPS, but requested an insecure........
- 设计模式之单例模式
- topic