静态文件缓存压缩以及保存

缓存

  1. 响应
    • Last-Modified
      服务器告诉浏览器更新时间 格式为格林时间
header ("Last-Modified: " . gmdate ('r', strtotime('2018-10-22'))); 
<meta http-equiv="last-modified" content="Thu, 21 Jun 2018 06:50:50 GMT">
  • Etag
    服务器告诉浏览器保存序列号
header ("Etag:fca75d26f6dc8111a7d1b24e9debd652" )); 
<meta http-equiv="Etag" content="fca75d26f6dc8111a7d1b24e9debd652">
  • Expires
    服务器告诉浏览器过期时间 格式为格林时间
    在HTTP 1.1版开始,使用Cache-Control: max-age=秒替代
  • Cache-Control
    服务器告诉浏览器怎么控制缓存
    must-revalidate页面过期 必须服务器进行获取
    Cache-control: no-cache 禁止缓存
    Cache-Control: max-age= 生存秒数
header ("Cache-Control:max-age=5");
<meta http-equiv="cache-control" content="max-age=5">
  1. 请求
    • If-Modified-Since 请求
      浏览器告诉服务器 上一次保存的时间
      如果 If-Modified-Since 小于 Last-Modified 则重新请求
    • If-None-Match
      浏览器告诉服务器 上一次保存序列号
      如果 If-None-Match与Etag 不匹配 则重新请求
#禁止缓存
<meta http-equiv="expires" content="0">
<meta http-equiv="cache-control" content="no-cache">

压缩服务器设置

  • expires
    s 秒 m分 h小时 d天
location ~[^.]+\.(gif|jpg|jpeg)$ {
expires 30d;
}
  • gizp 压缩
gzip on;#开启
gzip_min_length 1k;#大小大于1k才开启压缩
gzip_buffers 4 16k;
#gzip_http_version 1.0;#压缩协议级别
gzip_comp_level 2;#压缩级别 1-10 数字越大压缩的越好,越耗cpu
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;#压缩mine类型
gzip_vary off;
gzip_disable "MSIE [1-6]\.";#屏蔽IE

保存

storage类

namespace helper;
defined('IN')||exit;
class storage
{
    private $engine='';
    public function init($init=array())
    {
		$engine=\zc::get('storage/engine').'Storage';
		$this->engine=\zc::helper('storage/'.$engine,$init);
    }
    public function  __call($name,$arguments)
    {
     return call_user_func_array([$this->engine,$name],$arguments);
    }
}

oss存储
ossStorage类

namespace helper\storage;
defined('IN')||exit;
class ossStorage
{
    private $baseUrl = '';
    private $basePath = '';		
    private $oss = '';
    private $dirFen = '/';		
	private $curPath = '';
	private $uniqid = '';
	private $realPath = '';	
	public function init($init=array())
    {
		$this->baseUrl =isset($init[0])?$init[0]:\zc::get('storage/url');
		$this->basePath =isset($init[1])?$init[1]:\zc::get('storage/path');
        $AccessKeyId=isset($init[2])?$init[2]:\zc::get('oss/AccessKeyId');
        $AccessKeySecret=isset($init[3])?$init[3]:\zc::get('oss/AccessKeySecret');
		$city=isset($init[4])?$init[4]:\zc::get('oss/city');
		$networkType=isset($init[5])?$init[5]:\zc::get('oss/networkType');
		
		$this->oss =\JohnLui\AliyunOSS::boot(
      $city,
      $networkType,
      true,//内网
      $AccessKeyId,
      $AccessKeySecret
    );
    }
	
	public function set($uniqid,$curPath='',$sign='sign',$dirFen='/',$level=4)
    {
        $this->curPath=$curPath;
		$this->uniqid=$uniqid;
		$this->dirFen=$dirFen;
		$this->realPath=$this->getPath($sign,$level);
		$dir=$this->basePath.$this->dirFen.$this->realPath;
		$this->oss->setBucket($this->basePath);
        return $this;
    }	

	private function getPath($sign,$level)
    {
		$mid=md5($this->curPath.$this->dirFen.$this->uniqid.$this->dirFen.$sign);
		$path=array();
		$path[]=$this->curPath;
		$chu=intval(32/$level);
		for($i=0;$i<$level;$i++){
		$path[]=substr($mid,$i*$chu,$chu);	
		}
		$yu=32%$level;
		if($yu!==0){
		 $path[]=substr($mid,32-$yu);
		}
		return implode($this->dirFen,$path);	
	}
	public function delete()
    {
		return $this->oss->deleteObject($this->basePath,$this->realPath);
    }
	
	public function uploadSave($tmpfile,$key,$ext)
    {
		$file=$this->realPath.$this->dirFen.$key.($ext?'.'.$ext:'');
		if(!$this->oss->uploadFile($file,$tmpfile)){
			return false;
		}
		$url=$this->oss->getPublicUrl($file);
		return $this->baseUrl.$this->dirFen.$url;
    }
	
	public function contentSave($content,$ext,$extraMeta=array())
    {
		//$this->delete();
		$content=(array)$content;
		$_meta=array();
		foreach($content as $k=>$v){
		$bfile=$this->realPath.$this->dirFen;
		if(is_array($ext)){
		$bfile.=($k+1).'.'.$ext[$k];	
		}elseif(!empty($ext)){
		$bfile.=($k+1).'.'.$ext;
		}else{
		$bfile.=($k+1);	
		}
		$this->oss->uploadContent($bfile, $v);
		$_meta[$k]['url']=$this->baseUrl.$this->dirFen.$bfile;
		$_meta[$k]['order']=$k+1;
		if(isset($extraMeta[$k])){
		$_meta[$k]=array_merge($_meta[$k],$extraMeta[$k]);	
		}else{
		$_meta[$k]=array_merge($_meta[$k],$extraMeta);
		}}
		$this->setMeta($_meta);
		return true;
    }

	public function setMeta($meta=array())
    {
		$meta=json_encode($meta);
		return $this->oss->uploadContent($this->realPath .$this->dirFen.'__meta__', $meta);
	}
	
	public function setExtraMeta($extraMeta=array())
    {
		$url=$this->oss->getPublicUrl($this->realPath .$this->dirFen.'__meta__');
		$return=file_get_contents($this->baseUrl.$this->dirFen.$url);
		$all=array();
		if(empty($return)){
			if(isset($extraMeta[0])){
			$all=$extraMeta;
		}else{
		$all=array($extraMeta);
		}}else{
			$return=json_decode($return,true);
		foreach($return as $k=>$v){
			if(isset($extraMeta[$k])){
		$all[$k]=array_merge($return[$k],$extraMeta[$k]);	
		}else{
		$all[$k]=array_merge($return[$k],$extraMeta);
		}}	
		}
		return $this->setMeta($all);
	}
	
	
	public function getMeta($lists=array(),$shift=false)
    {
		$url=$this->oss->getPublicUrl($this->realPath .$this->dirFen.'__meta__');
		$return=file_get_contents($this->baseUrl.$this->dirFen.$url);
		$return=json_decode($return,true);
		if(!empty($return)){
			if(empty($lists)){
				foreach($return as $k=>$v){
				$pxu[$k]  = $v['order'];	
				}
			array_multisort($pxu,SORT_ASC,$return);	
			return $return;
			}else{
				$result=array();	
				$lists=(array)$lists;
            foreach($return as $k=>$v){
				$pxu[$k]  = $v['order'];
				foreach($v as $kk=>$vv){
					if(!empty($lists)){
					foreach($lists as $zl){
					if(strpos(','.$zl.',',','.$kk.',')!==false){
						$result[$k][$kk]  = $vv;	
						}	
					}}
					}
				}
			array_multisort($pxu,SORT_ASC,$result);	
			$return=array();
			if($shift){
				foreach($result as $k=>$v){
				foreach($v as $kk=>$vv){
					$return[$kk][$k]  = $vv;
					}
				}
			}else{
			$return=$result;	
			}
			return $return;
			}
		}
		return array();
	}
}

本地存储
localStorage类

namespace helper\storage;
defined('IN')||exit;
class localStorage
{
    private $baseUrl = '';
    private $basePath = '';		
    private $fileObj = '';
    private $dirFen = '/';		
	private $curPath = '';
	private $uniqid = '';
	private $realPath = '';	
	public function init($init=array())
    {
        $this->fileObj =\zc::helper('fileDir');
		$this->baseUrl =isset($init[0])?$init[0]:\zc::get('storage/url');
		$this->basePath =isset($init[1])?$init[1]:\zc::get('storage/path');
    }

	public function set($uniqid,$curPath='',$sign='sign',$dirFen='/',$level=4)
    {
        $this->curPath=$curPath;
		$this->uniqid=$uniqid;
		$this->dirFen=$dirFen;
		$this->realPath=$this->getPath($sign,$level);
		//exec("chmod -R 0777 /web/public/gamenet");
		$dir=$this->basePath.$this->dirFen.$this->realPath;
		if(!is_dir($dir)){
			$this->fileObj->mkdirs($dir,0777);
		}
        return $this;
    }	

	private function getPath($sign,$level)
    {
		$mid=md5($this->curPath.$this->dirFen.$this->uniqid.$this->dirFen.$sign);
		$path=array();
		$path[]=$this->curPath;
		$chu=intval(32/$level);
		for($i=0;$i<$level;$i++){
		$path[]=substr($mid,$i*$chu,$chu);	
		}
		$yu=32%$level;
		if($yu!==0){
		 $path[]=substr($mid,32-$yu);
		}
		return implode($this->dirFen,$path);	
	}
	//删除
	public function delete()
    {
		$dir=$this->basePath.$this->dirFen.$this->realPath;
		return $this->fileObj->dirDelete($dir,false);
    }
	//上传
	public function uploadSave($tmpfile,$key,$ext)
    {
		$bfile=$this->dirFen.$this->realPath.$this->dirFen.$key.($ext?'.'.$ext:'');
		$file=$this->basePath.$bfile;
		if(move_uploaded_file($tmpfile,$file)===false){
			return false;
		}
		return $this->baseUrl.$bfile;
    }
	//内容保存
	public function contentSave($content,$ext,$extraMeta=array())
    {
		//$this->delete();
		$content=(array)$content;
		$_meta=array();
		foreach($content as $k=>$v){
		$bfile=$this->dirFen.$this->realPath.$this->dirFen;
		if(is_array($ext)){
		$bfile.=($k+1).'.'.$ext[$k];	
		}elseif(!empty($ext)){
		$bfile.=($k+1).'.'.$ext;
		}else{
		$bfile.=($k+1);	
		}
		$file=$this->basePath.$bfile;
		file_put_contents($file,$v);
		$_meta[$k]['url']=$this->baseUrl.$bfile;
		$_meta[$k]['order']=$k+1;
		if(isset($extraMeta[$k])){
		$_meta[$k]=array_merge($_meta[$k],$extraMeta[$k]);	
		}else{
		$_meta[$k]=array_merge($_meta[$k],$extraMeta);
		}}
		$this->setMeta($_meta);
		return true;
    }
	//设置文件基本meta信息
	public function setMeta($meta=array())
    {
		$dir=$this->basePath.$this->dirFen.$this->realPath;
		$meta=json_encode($meta);
		return file_put_contents($dir.$this->dirFen.'__meta__',$meta);
	}
	//追加meta信息
	public function setExtraMeta($extraMeta=array())
    {
		$dir=$this->basePath.$this->dirFen.$this->realPath;
		$return=file_get_contents($dir.$this->dirFen.'__meta__');
		$all=array();
		if(empty($return)){
			if(isset($extraMeta[0])){
			$all=$extraMeta;
		}else{
		$all=array($extraMeta);
		}}else{
		$return=json_decode($return,true);	
		foreach($return as $k=>$v){
			if(isset($extraMeta[$k])){
		$all[$k]=array_merge($return[$k],$extraMeta[$k]);	
		}else{
		$all[$k]=array_merge($return[$k],$extraMeta);
		}}	
		}
		return $this->setMeta($all);
	}
	//获取meta信息
	public function getMeta($lists=array(),$shift=false)
    {
		$dir=$this->basePath.$this->dirFen.$this->realPath;
		$file=$dir.$this->dirFen.'__meta__';
		if(is_file($file)){
			$return=file_get_contents($file);
		$return=json_decode($return,true);
		if(!empty($return)){
			if(empty($lists)){
				foreach($return as $k=>$v){
				$pxu[$k]  = $v['order'];	
				}
			array_multisort($pxu,SORT_ASC,$return);	
			return $return;
			}else{
				$result=array();	
				$lists=(array)$lists;
            foreach($return as $k=>$v){
				$pxu[$k]  = $v['order'];
				foreach($v as $kk=>$vv){
					if(!empty($lists)){
					foreach($lists as $zl){
					if(strpos(','.$zl.',',','.$kk.',')!==false){
						$result[$k][$kk]  = $vv;	
						}	
					}}
					}
				}
			array_multisort($pxu,SORT_ASC,$result);	
			$return=array();
			if($shift){
				foreach($result as $k=>$v){
				foreach($v as $kk=>$vv){
					$return[$kk][$k]  = $vv;
					}
				}
			}else{
			$return=$result;	
			}
			return $return;
			}
		}
		}	
		return array();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_38984126/article/details/83269558
今日推荐