| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- // +----------------------------------------------------------------------
- // | Fanwe 方维直播系统
- // +----------------------------------------------------------------------
- // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Author: 云淡风轻(1956838968@qq.com)
- // +----------------------------------------------------------------------
- class CacheRediscacheService
- {
- private $mem;
- private $dir; //模拟的目录,即前缀
- var $prefix;
- var $file_prefix;
- // var $redis_distribution;
- /**
- +----------------------------------------------------------
- * 架构函数
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- */
- public function __construct($is_read=false)
- {
- // $count = intval($_REQUEST['count']);
- // $count = $count?$count: count($GLOBALS['distribution_cfg']['REDIS_DISTRIBUTION']);
- //
- //// $count = count($GLOBALS['distribution_cfg']['REDIS_DISTRIBUTION']);
- if($GLOBALS['distribution_cfg']['RDB_CLIENT']==$GLOBALS['distribution_cfg']['CACHE_CLIENT']){
- $this->mem = $GLOBALS['redisdb'];
- }else{
- $this->mem = new Rediscache($GLOBALS['distribution_cfg']['CACHE_CLIENT'], $GLOBALS['distribution_cfg']['CACHE_PORT'],$GLOBALS['distribution_cfg']['CACHE_PASSWORD']);
- }
- $this->prefix = $GLOBALS['distribution_cfg']['REDIS_PREFIX'];
- $this->file_prefix = $GLOBALS['distribution_cfg']['REDIS_PREFIX'].'filecache:';
- //
- // $count = count($GLOBALS['distribution_cfg']['REDIS_DISTRIBUTION']);
- // if($count){
- // $rid = hash_table(es_session::id(),$count);
- // $redis_distribution = $GLOBALS['distribution_cfg']['REDIS_DISTRIBUTION'][$rid];
- // $this->redis_distribution = new Rediscache($redis_distribution['CACHE_CLIENT'], $redis_distribution['CACHE_PORT'],$redis_distribution['CACHE_PASSWORD']);
- // }else{
- // $this->redis_distribution = $this->mem;
- // }
- }
- /**
- +----------------------------------------------------------
- * 读取缓存
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $name 缓存变量名
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- */
- public function get($name,$readonly=false)
- {
- if(!$this->mem)return false;
- //if(IS_DEBUG)return false;
- $var_name = $this->file_prefix.($this->dir.$name);
- global $$var_name;
- if($$var_name)
- {
- return $$var_name;
- }
- $data = $this->mem->get($var_name);
- if($data)
- {
- $data = unserialize($data);
- $$var_name = $data;
- }
- else
- {
- $data = false;
- }
- return $data;
- }
- /**
- +----------------------------------------------------------
- * 写入缓存
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $name 缓存变量名
- * @param mixed $value 存储数据
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- public function set($name, $value,$expire ="-1",$readonly=false)
- {
- //if(IS_DEBUG)return false;
- if(!$this->mem)return false;
- if($expire=='-1') $expire = 3600*24;
- $value = serialize($value);
- $key = $this->file_prefix.($this->dir.$name);
- return $this->mem->set($key,$value,$expire);
- }
- public function set_lock($name,$exp=10){
- if(!$this->mem)return false;
- $rand_num = rand();
- $key = $this->file_prefix.($this->dir.$name).'_lock';
- $ok = $this->mem->set_lock($key,$rand_num,$exp);
- return $ok;
- }
- public function del_lock($name){
- if(!$this->mem)return false;
- $key = $this->file_prefix.($this->dir.$name).'_lock';
- $ok = $this->mem->delete($key);
- return $ok;
- }
- /**
- +----------------------------------------------------------
- * 删除缓存
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $name 缓存变量名
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- public function rm($name)
- {
- if(!$this->mem)return false;
- $key = $this->file_prefix.($this->dir.$name);
- return $this->mem->delete($key);
- }
-
-
- public function clear()
- {
- if(!$this->mem)return false;
- //获取所有的数据缓存文件
- $keys = $this->mem->keys($this->file_prefix,true);
- if(!$keys){
- return true;
- }
- $this->mem->delete($keys);
- //$this->mem->flush(0);
- }
- public function clear_by_name($name){
- if(!$this->mem)return false;
- //获取所有的数据缓存文件
- $keys = $this->mem->keys($this->file_prefix.$name,true);
- if(!$keys){
- return true;
- }
- $this->mem->delete($keys);
- }
- public function celar_con(){
- if(!$this->mem)return false;
- $keys = array();
- //获取所有的数据缓存文件
- // $keys = $this->mem->keys('fanwe0000001:user_contribution:',true);
- // $keys1 = $this->mem->keys('fanwe0000001:video_contribution:',true);
- // $keys2 = $this->mem->keys('fanwe0000001:video:',true);
- // $keys3 = $this->mem->keys('fanwe0000001:video_gift:',true);
- // $keys4 = $this->mem->keys('fanwe0000001:user_winning:',true);
- // $keys5 = $this->mem->keys('fanwe0000001:video_viewer:',true);
- // $keys6 = $this->mem->keys('fanwe0000001:video_viewer_level:',true);
- // $keys7 = $this->mem->keys('fanwe0000001:user_followed_by:',true);
- //$keys8 = $this->mem->keys('fanwe0000001:cate:',true);
- // $keys9 = $this->mem->keys('fanwe0000001:video_condition',true);
- // $keys = array_merge($keys8);
- //$keys = $keys8;
- // $keys[] = 'fanwe0000001:video_vote_number';
- if(!$keys){
- return true;
- }
- $this->mem->delete($keys);
- }
- public function set_dir($dir='')
- {
- if($dir!='')
- {
- $this->dir =$this->prefix. ($dir);
- }
- }
- public function close(){
- $this->mem->close();
- }
- }//类定义结束
- ?>
|