category=$category; $this->table_name='cache'.($this->category?('_'.$this->category):''); } /*获取实例化类*/ public static function getInstance($category=''){ if(!isset(self::$instances[$category])){ self::$instances[$category] = new static($category); } return self::$instances[$category]; } /** * 获取数据库连接 * @return \think\db\Query */ public function db(){ try { $db=db($this->table_name); $db->getPk(); }catch (\Exception $ex){ $this->create_table(); $db=db($this->table_name); } return $db; } public function getCount($cname){ return $this->db()->where('cname',$cname)->count(); } /** * 获取缓存 * @param string $cname 缓存名称 * @param string $key 缓存的数据键名 * @return mixed */ public function getCache($cname,$key=null){ $cache=$this->db()->where('cname',$cname)->find(); switch($cache['ctype']){ case 1:$cache['data']=intval($cache['data']);break; case 2:$cache['data']=unserialize($cache['data']);break; } return $key?$cache[$key]:$cache; } /** * 设置缓存 * @param string $cname 缓存名称 * @param string $value 缓存数据 */ public function setCache($cname,$value){ $data=array('cname'=>$cname,'ctype'=>0); if(is_array($value)){ $data['ctype']=2; $data['data']=serialize($value); }elseif(is_integer($value)){ $data['ctype']=1; $data['data']=intval($value); }else{ $data['data']=$value; } $data['dateline']=time(); $this->db()->insert($data,true); } /** * 缓存是否过期 * @param string $cname 缓存名称 * @param int $timeout 过期时间 * @return boolean */ public function expire($cname,$timeout=72000){ $cache=$this->getCache($cname); if(empty($cache)||abs(NOW_TIME-$cache['dateline']>$timeout)){ return true; }else{ return false; } } /** * 创建表 * @return boolean */ public function create_table(){ $tname=config('database.prefix').$this->table_name; $exists=db()->query("show tables like '{$tname}'"); if(empty($exists)){ $table=<<execute($table); } } } ?>