栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP实现的Redis操作通用类示例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP实现的Redis操作通用类示例

本文实例讲述了PHP实现的Redis操作通用类。分享给大家供大家参考,具体如下:

找到一个比较全的Redis PHP操作类库,分享给大家

30,
    //选择的数据库。
    'db_id'=>0,
  );
  //什么时候重新建立连接
  protected $expireTime;
  protected $host;
  protected $port;
  private function __construct($config,$attr=array())
  {
    $this->attr    =  array_merge($this->attr,$attr);
    $this->redis  =  new Redis();
    $this->port    =  $config['port'] ? $config['port'] : 6379;
    $this->host    =  $config['host'];
    $this->redis->connect($this->host, $this->port, $this->attr['timeout']);
    if($config['auth'])
    {
      $this->auth($config['auth']);
      $this->auth  =  $config['auth'];
    }
    $this->expireTime  =  time() + $this->attr['timeout'];
  }
  
  public static function getInstance($config, $attr = array())
  {
    //如果是一个字符串,将其认为是数据库的ID号。以简化写法。
    if(!is_array($attr))
    {
      $dbId  =  $attr;
      $attr  =  array();
      $attr['db_id']  =  $dbId;
    }
    $attr['db_id']  =  $attr['db_id'] ? $attr['db_id'] : 0;
    $k  =  md5(implode('', $config).$attr['db_id']);
    if(! (static::$_instance[$k] instanceof self))
    {
      static::$_instance[$k] = new self($config,$attr);
      static::$_instance[$k]->k    =  $k;
      static::$_instance[$k]->dbId  =  $attr['db_id'];
      //如果不是0号库,选择一下数据库。
      if($attr['db_id'] != 0){
 static::$_instance[$k]->select($attr['db_id']);
      }
    }
    elseif( time() > static::$_instance[$k]->expireTime)
    {
      static::$_instance[$k]->close();
      static::$_instance[$k]     =   new self($config,$attr);
      static::$_instance[$k]->k  =  $k;
      static::$_instance[$k]->dbId=  $attr['db_id'];
      //如果不是0号库,选择一下数据库。
      if($attr['db_id']!=0){
 static::$_instance[$k]->select($attr['db_id']);
      }
    }
    return static::$_instance[$k];
  }
  private function __clone(){}
  
  public function getRedis()
  {
    return $this->redis;
  }
  
  
  public function hGet($key,$field)
  {
    return $this->redis->hGet($key,$field);
  }
  
  public function hSet($key,$field,$value)
  {
    return $this->redis->hSet($key,$field,$value);
  }
  
  public function hExists($key,$field)
  {
    return $this->redis->hExists($key,$field);
  }
  
  public function hdel($key,$field)
  {
    $fieldArr=explode(',',$field);
    $delNum=0;
    foreach($fieldArr as $row)
    {
      $row=trim($row);
      $delNum+=$this->redis->hDel($key,$row);
    }
    return $delNum;
  }
  
  public function hLen($key)
  {
    return $this->redis->hLen($key);
  }
  
  public function hSetNx($key,$field,$value)
  {
    return $this->redis->hSetNx($key,$field,$value);
  }
  
  public function hMset($key,$value)
  {
    if(!is_array($value))
      return false;
    return $this->redis->hMset($key,$value); 
  }
  
  public function hMget($key,$field)
  {
    if(!is_array($field))
      $field=explode(',', $field);
    return $this->redis->hMget($key,$field);
  }
  
  public function hIncrBy($key,$field,$value)
  {
    $value=intval($value);
    return $this->redis->hIncrBy($key,$field,$value);
  }
  
  public function hKeys($key)
  {
    return $this->redis->hKeys($key);
  }
  
  public function hVals($key)
  {
    return $this->redis->hVals($key);
  }
  
  public function hGetAll($key)
  {
    return $this->redis->hGetAll($key);
  }
  
  
  public function zAdd($key,$order,$value)
  {
    return $this->redis->zAdd($key,$order,$value);  
  }
  
  public function zinCry($key,$num,$value)
  {
    return $this->redis->zinCry($key,$num,$value);
  }
  
  public function zRem($key,$value)
  {
    return $this->redis->zRem($key,$value);
  }
  
  public function zRange($key,$start,$end)
  {
    return $this->redis->zRange($key,$start,$end);
  }
  
  public function zRevRange($key,$start,$end)
  {
    return $this->redis->zRevRange($key,$start,$end);
  }
  
  public function zRangeByScore($key,$start='-inf',$end="+inf",$option=array())
  {
    return $this->redis->zRangeByScore($key,$start,$end,$option);
  }
  
  public function zRevRangeByScore($key,$start='-inf',$end="+inf",$option=array())
  {
    return $this->redis->zRevRangeByScore($key,$start,$end,$option);
  }
  
  public function zCount($key,$start,$end)
  {
    return $this->redis->zCount($key,$start,$end);
  }
  
  public function zScore($key,$value)
  {
    return $this->redis->zScore($key,$value);
  }
  
  public function zRank($key,$value)
  {
    return $this->redis->zRank($key,$value);
  }
  
  public function zRevRank($key,$value)
  {
    return $this->redis->zRevRank($key,$value);
  }
  
  public function zRemRangeByScore($key,$start,$end)
  {
    return $this->redis->zRemRangeByScore($key,$start,$end);
  }
  
  public function zCard($key)
  {
    return $this->redis->zCard($key);
  }
  
  
  public function rPush($key,$value)
  {
    return $this->redis->rPush($key,$value); 
  }
  
  public function rPushx($key,$value)
  {
    return $this->redis->rPushx($key,$value);
  }
  
  public function lPush($key,$value)
  {
    return $this->redis->lPush($key,$value);
  }
  
  public function lPushx($key,$value)
  {
    return $this->redis->lPushx($key,$value);
  }
  
  public function lLen($key)
  {
    return $this->redis->lLen($key); 
  }
  
  public function lRange($key,$start,$end)
  {
    return $this->redis->lrange($key,$start,$end);
  }
  
  public function lIndex($key,$index)
  {
    return $this->redis->lIndex($key,$index);
  }
  
  public function lSet($key,$index,$value)
  {
    return $this->redis->lSet($key,$index,$value);
  }
  
  public function lRem($key,$count,$value)
  {
    return $this->redis->lRem($key,$value,$count);
  }
  
  public function lPop($key)
  {
    return $this->redis->lPop($key);
  }
  
  public function rPop($key)
  {
    return $this->redis->rPop($key);
  }
  
  
  public function set($key,$value)
  {
    return $this->redis->set($key,$value);
  }
  
  public function get($key)
  {
    return $this->redis->get($key);
  }
  
  public function setex($key,$expire,$value)
  {
    return $this->redis->setex($key,$expire,$value);
  }
  
  public function setnx($key,$value)
  {
    return $this->redis->setnx($key,$value);
  }
  
  public function mset($arr)
  {
    return $this->redis->mset($arr);
  }
  
  
  public function sMembers($key)
  {
    return $this->redis->sMembers($key);
  }
  
  public function sDiff($key1,$key2)
  {
    return $this->redis->sDiff($key1,$key2);
  }
  
  public function sAdd($key,$value)
  {
    if(!is_array($value))
      $arr=array($value);
    else
      $arr=$value;
    foreach($arr as $row)
      $this->redis->sAdd($key,$row);
  }
  
  public function scard($key)
  {
    return $this->redis->scard($key);
  }
  
  public function srem($key,$value)
  {
    return $this->redis->srem($key,$value);
  }
  
  
  public function select($dbId)
  {
    $this->dbId=$dbId;
    return $this->redis->select($dbId);
  }
  
  public function flushDB()
  {
    return $this->redis->flushDB();
  }
  
  public function info()
  {
    return $this->redis->info();
  }
  
  public function save()
  {
    return $this->redis->save();
  }
  
  public function bgSave()
  {
    return $this->redis->bgSave();
  }
  
  public function lastSave()
  {
    return $this->redis->lastSave();
  }
  
  public function keys($key)
  {
    return $this->redis->keys($key);
  }
  
  public function del($key)
  {
    return $this->redis->del($key);
  }
  
  public function exists($key)
  {
    return $this->redis->exists($key);
  }
  
  public function expire($key,$expire)
  {
    return $this->redis->expire($key,$expire);
  }
  
  public function ttl($key)
  {
    return $this->redis->ttl($key);
  }
  
  public function exprieAt($key,$time)
  {
    return $this->redis->expireAt($key,$time);
  }
  
  public function close()
  {
    return $this->redis->close();
  }
  
  public static function closeAll()
  {
    foreach(static::$_instance as $o)
    {
      if($o instanceof self)
 $o->close();
    }
  }
  
  
  public function dbSize()
  {
    return $this->redis->dbSize();
  }
  
  public function randomKey()
  {
    return $this->redis->randomKey();
  }
  
  public function getDbId()
  {
    return $this->dbId;
  }
  
  public function getAuth()
  {
    return $this->auth;
  }
  public function getHost()
  {
    return $this->host;
  }
  public function getPort()
  {
    return $this->port;
  }
  public function getConnInfo()
  {
    return array(
      'host'=>$this->host,
      'port'=>$this->port,
      'auth'=>$this->auth
    );
  }
  
  
  public function watch($key)
  {
    return $this->redis->watch($key);
  }
  
  public function unwatch()
  {
    return $this->redis->unwatch();
  }
  
  public function multi($type=Redis::MULTI)
  {
    return $this->redis->multi($type);
  }
  
  public function exec()
  {
    return $this->redis->exec();
  }
  
  public function discard()
  {
    return $this->redis->discard();
  }
  
  public function ping()
  {
    return $this->redis->ping();
  }
  public function auth($auth)
  {
    return $this->redis->auth($auth);
  }
  
  
  public function hashAll($prefix,$ids)
  {
    if($ids==false)
      return false;
    if(is_string($ids))
      $ids=explode(',', $ids);
    $arr=array();
    foreach($ids as $id)
    {
      $key=$prefix.'.'.$id;
      $res=$this->hGetAll($key);
      if($res!=false)
 $arr[]=$res;
    }
    return $arr;
  }
  
  public function pushMessage($lkey,$msg)
  {
    if(is_array($msg)){
      $msg  =  json_encode($msg);
    }
    $key  =  md5($msg);
    //如果消息已经存在,删除旧消息,已当前消息为准
    //echo $n=$this->lRem($lkey, 0, $key)."n";
    //重新设置新消息
    $this->lPush($lkey, $key);
    $this->setex($key, 3600, $msg);
    return $key;
  }
  
  public function delKeys($keys,$dbId)
  {
    $redisInfo=$this->getConnInfo();
    $cmdArr=array(
      'redis-cli',
      '-a',
      $redisInfo['auth'],
      '-h',
      $redisInfo['host'],
      '-p',
      $redisInfo['port'],
      '-n',
      $dbId,
    );
    $redisStr=implode(' ', $cmdArr);
    $cmd="{$redisStr} KEYS "{$keys}" | xargs {$redisStr} del";
    return $cmd;
  }
}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+redis数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/39681.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号