> 백엔드 개발 > PHP 튜토리얼 > PHP 작업 Redis 캐시 방법

PHP 작업 Redis 캐시 방법

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
풀어 주다: 2016-07-29 09:13:34
원래의
1022명이 탐색했습니다.
<span style="font-size:12px;"><?php
/**
 * Redis缓存操作
 * @author hxm
 * @version 1.0
 * @since 2015.05.04
 */
class RCache extends Object implements CacheFace 
{
  private $redis = null; //redis<strong>对象</strong>
    
  private $sId  = 1;  //servier服务ID
    
  private $con  = null;//链接资源
    
  /**
   * 初始化Redis
   *
   * @return Object
   */
  public function __construct()
  {
    if ( !class_exists(&#39;Redis&#39;) )
    {
      throw new Q<strong>Exception</strong>(&#39;PHP extension does not exist: Redis&#39;);
    }
    $this->redis = new Redis();
  }
    
  /**
   * 链接memcahce服务
   *
   * @access private
   * @param  string $key 关键字
   * @param  string $value 缓存内容
   * @return array
   */
  private function connect( $sid )
  {
    $file = $this->CacheFile();
    <strong>require</strong> $file;
    if(! isset($cache) )
    {
      throw new Q<strong>Exception</strong>('缓存配置文件不存在'.$file);
    }
    $server = $cache[$this->cacheId];
    $sid  = isset($sid) == 0 ? $this->sId : $sid;//memcache服务选择
    if ( ! $server[$sid])
    {
      throw new Q<strong>Exception</strong>('当前操作的缓存服务器配置文件不存在');
    }
    $host = $server[$sid]['host'];
    $port = $server[$sid]['port'];
    try {
      $this->redis->connect( $host , $port );
    } catch (<strong>Exception</strong> $e) {
      exit('memecache连接失败,错误信息:'. $e->getMessage());
    }
  }
    
  /**
   * 写入缓存
   *
   * @access private
   * @param  string $key 关键字
   * @param  string $value 缓存内容
   * @return array
   */
  public function set( $key , $value , $sid , $expire = 0)
  {
    $data = $this->get($key , $sid); //如果已经存在key值
    if( $data ) 
    {
      return $this->redis->getset( $key , $value);
    } else {
      return $this->redis->set( $key , $value);
    }
  }
    
  /**
   * 读取缓存
   *
   * @access private
   * @param  string $key 关键字
   * @param  int   $sid 选择第几台memcache服务器
   * @return array
   */
  public function get( $key , $sid)
  {
    $this->connect( $sid );
    return $this->redis->get($key);
  }
    
  /**
   * 清洗(删除)已经存储的所有的元素
   *
   * @access private
   * @return array
   */
  public function flush()
  {
    $this->connect();
    return $this->redis->flushall();
  }
  /**
   * 删除缓存
   *
   * @access private
   * @param  string $key 关键字
   * @param  int   $sid 选择第几台memcache服务器
   * @return array
   */
  public function remove( $key , $sid)
  {
    $this->connect();
    return $this->redis->del($key);
  }
    
  /**
   * 析构函数
   * 最后关闭memcache
   */
  public function __destruct()
  {
    if($this->redis)
    {
      $this->redis->close();
    }
  }
}</span>
로그인 후 복사

위 내용은 require, Exception, 객체 내용 등 PHP에서 Redis Cache를 동작시키는 방법을 소개하고 있으니 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되었으면 좋겠습니다.

관련 라벨:
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿