`
kankan1218
  • 浏览: 278649 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

更具if modified since更新图片,并且删除陈旧的图片

    博客分类:
  • perl
阅读更多

更具if modified since更新图片,并且删除陈旧的图片:

# ---------------------------------------------
# This method will get the document identified by $url and store it in file called $filename.
# If the file already exists, then the request will contain an "If-Modified-Since" header matching the modification time of the file.
# If the document on the server has not changed since this time, then nothing happens. 
# If the document has been updated, it will be downloaded again. 
# The modification time of the file will be forced to match that of the server.
# The return value is the the response object.
# Modify Log
# 11/29/2010 edison create
# ---------------------------------------------
sub mirrorFile {
    my($this, $url, $file) = @_;
	
	my $browser = $this->{'browser'};
    my $request = HTTP::Request->new('GET', $url);

    # If the file exists, add a cache-related header
    if ( -e $file ) {print "exist\n";
        my ($mtime) = ( stat($file) )[9];
        if ($mtime) {
            $request->header( 'If-Modified-Since' => HTTP::Date::time2str($mtime) );
        }
        # use the current time to replace the last access time
        my $last_access_time = time();
        utime $last_access_time, $mtime, $file;
    }
    my $tmpfile = "$file-$$";
	
	# If a $tmpfile is provided with the :content_file option, then the response content will be saved here instead of in the response object. 
    my $response = $browser->request($request, $tmpfile);
    if ( $response->header('X-Died') ) {
		die $response->header('X-Died');
    }

    # Only fetching a fresh copy of the would be considered success.
    # If the file was not modified, "304" would returned, which 
    # is considered by HTTP::Status to be a "redirect", /not/ "success"
    if ( $response->is_success ) {print "success\n";
        my @stat        = stat($tmpfile) or die "Could not stat tmpfile '$tmpfile': $!";
        my $file_length = $stat[7];
        my ($content_length) = $response->header('Content-length');

        if ( defined $content_length and $file_length < $content_length ) {
            unlink($tmpfile);
            die "Transfer truncated: " . "only $file_length out of $content_length bytes received\n";
        }
        elsif ( defined $content_length and $file_length > $content_length ) {
            unlink($tmpfile);
            die "Content-length mismatch: " . "expected $content_length bytes, got $file_length\n";
        }
        # The file was the expected length. 
        else {
            # Replace the stale file with a fresh copy
            if ( -e $file ) {
                # Some dosish systems fail to rename if the target exists
                chmod 0777, $file;
                unlink $file;
            }
            rename( $tmpfile, $file )
                or die "Cannot rename '$tmpfile' to '$file': $!\n";

            # make sure the file has the same last modification time
            # and use the current time to replace the last access time
            if ( my $lm = $response->last_modified ) {
                my $last_access_time = time();
                utime $last_access_time, $lm, $file;
            }
        }
    }
    # The local copy is fresh enough, so just delete the temp file  
    else {
		unlink($tmpfile);print "unsuccess\n";
    }
    return $response;
}


# ---------------------------------------------
# Remove images which are not fresh enough to save disk space.
# Modify Log
# 11/30/2010 edison create 
# ---------------------------------------------
sub removeStaleImages {
	my $this = shift;
	
	my $settedNotAccessDay = $this->{'config'}->{'image_not_access_day'};
	if(!$settedNotAccessDay){
		die "miss 'image_not_access_day' in base.conf file\n";
	}
	if($settedNotAccessDay =~ /[^\d]/){
		die "'image_not_access_day' in base.conf file should be numeric\n";
	}
	my $deleteImageCount = 0;
	my $data_feed_path = $this->{'config'}->{'data_feed_path'};
	my @fullPathImages = glob($data_feed_path.'/??/??/*');
	foreach (@fullPathImages){
		my @stat        = stat($_) or die "Could not stat imagefile '$_': $!";
		my $atime = $stat[8];
		my $timeNotAccess = time()-$atime;
		
		if($timeNotAccess > $settedNotAccessDay*24*3600){
			unlink $_;
			$deleteImageCount ++;
		}
	}
	Log::Write::setLog('A400004',{'COUNT'=>$deleteImageCount});
}
分享到:
评论

相关推荐

    Etag和Expires.docx

    在后续请求中,客户端会在HttpRequest Header中发送If-Modified-Since报头,包含之前接收到的Last-Modified时间。如果服务器上的文件自上次请求后未被修改,服务器将返回304 Not Modified状态码,客户端则直接从本地...

    高性能网站建设指南

    - 关闭ETags机制,使用Last-Modified和If-Modified-Since头代替,简化缓存验证流程。 12. **Ajax缓存技术** - 实现智能Ajax缓存策略,减少服务器端的数据请求。 13. **最小化技术** - 对图片等资源进行适当的...

    http.zip_规范的包文

    - Last-Modified和If-Modified-Since:通过资源的最后修改时间进行验证。 6. 安全与隐私: - HTTPS:HTTP加上SSL/TLS加密,确保通信安全。 - CORS:跨源资源共享,允许不同源之间的交互。 - Content-Security-...

    前端面试题(JavaScript).docx

    11. **Ajax 缓存问题**:发送 AJAX 请求时,可以通过设置请求头 "If-Modified-Since" 为 "0" 来避免浏览器使用缓存的响应,确保每次请求都从服务器获取最新数据。 以上是对前端面试中常见 JavaScript 知识点的详细...

    关于 Http 通讯的一个简单事例

    常见的缓存控制头有Cache-Control、ETag、If-None-Match、If-Modified-Since等。 六、HTTP安全 HTTPS(HTTP over SSL/TLS)是HTTP的安全版,通过SSL/TLS协议为HTTP提供了加密处理、服务器身份验证和消息完整性检查...

    http1.1协议中文版

    它支持多种缓存验证头,如`If-Modified-Since`和`ETag`,使得服务器能够指示客户端何时应当从缓存中读取数据,而不是每次都从服务器获取新数据。 #### 三、HTTP/1.1协议关键概念 ##### 3.1 请求(Request) 客户端向...

Global site tag (gtag.js) - Google Analytics