`
kankan1218
  • 浏览: 276701 次
  • 性别: 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});
}
分享到:
评论

相关推荐

    http协议-缓存控制:etag If-None-Match / Last-Modified If-Modified-Since

    本文将深入探讨两个关键的缓存机制:Etag与If-None-Match以及Last-Modified与If-Modified-Since。 1. Etag与If-None-Match: Etag(Entity Tag)是一种用于标识资源版本的机制。服务器在响应头中设置Etag,它通常是...

    Ajax 缓存问题,Ajax 缓存问题

    可以设置`"If-Modified-Since"`为`"0"`或`"Cache-Control"`为`"no-cache"`,如下: ```javascript xhr.setRequestHeader("If-Modified-Since", "0"); xhr.setRequestHeader("Cache-Control", "no-cache"); ``` ...

    Flume 的 File has been modified since being read异常解决方案

    https://www.cnblogs.com/pingjie/p/4146727.html 感谢这位大哥写的这篇文章。

    IF_SIG_CONTROL.zip_Modified

    signal controls using IF for verilog. it modified Xilinx. it use just red, yellow and green signals.signal controls using IF for verilog. it modified Xilinx. it use just red, yellow and green signals.

    【Android 文件管理】分区存储 ( 修改与删除图片文件 )

    1. 使用ContentResolver:通过ContentResolver和MediaStore类,应用可以查询、插入、更新或删除媒体库中的图片。例如,要修改图片元数据,可以先查询图片的Uri,然后使用ContentResolver的update()方法。 ```java ...

    php远程图片本地化插件

    在PHP开发中,远程图片本地化插件是一个重要的工具,它允许开发者将互联网上的图片资源下载到本地服务器,以便提高网站加载速度、节省带宽、防止因原图源站问题导致图片无法显示,并且可以更好地保护网站内容的完整...

    Lenovo ThinkPad x220_1.39 Modified_WL.exe

    Lenovo ThinkPad x220_1.39 Modified_WL.exeLenovo ThinkPad x220_1.39 Modified_WL.exeLenovo ThinkPad x220_1.39 Modified_WL.exeLenovo ThinkPad x220_1.39 Modified_WL.exeLenovo ThinkPad x220_1.39 Modified_...

    android 下载图片并缓存

    - **条件请求**:使用If-None-Match或If-Modified-Since头进行条件请求,如果服务器资源未改变,则返回304状态码,直接使用缓存。 6. **优化要点** - **图片压缩**:在显示图片前,根据ImageView的大小对图片进行...

    图片浏览小程序(java+applet)_modified_modified.zip

    该压缩包文件“图片浏览小程序(java+applet)_modified_modified.zip”主要包含了使用Java语言开发的一个图片浏览小程序的源代码和相关文档。这个程序可能是一个毕业设计或课程设计项目,适用于学习者提升技能,理解...

    h5截取视频当前一帧图片

    add_header Access-Control-Allow-Headers 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; if ($request_method = 'OPTIONS') { return 204; } ...

    C# MVC HttpModule 防盗链和图片304缓存

    客户端在下次请求时会携带之前的`ETag`和`If-Modified-Since`头,服务器比较这两个值来决定是否返回304状态码。 最后,要将HttpModule集成到你的MVC应用中,你需要在Web.config中配置模块: ```xml , ...

    ASP.NET缓存技术应用总结

    if (DateTime.TryParse(this.Request.Headers.Get("If-Modified-Since"), out IfModifiedSince)) { if ((DateTime.Now - IfModifiedSince.AddHours(8)).Seconds ) //缓存 10s { Response.Status = "304 Not ...

    Caching HTTP Headers, Last-Modified and ETag

    当客户端(浏览器)向服务器请求资源时,如果之前已经获取过该资源,它会在请求中包含一个 `If-Modified-Since` 头,带上上次获取资源时记录的 `Last-Modified` 时间戳。如果服务器发现资源自那时以来未被修改,它会...

    Inverse_Modified_;逆运动学_matlab_Modified_

    "Inverse_Modified" 指的可能是对标准逆运动学算法的一种改进或调整,以适应特定的机器人模型或优化性能。 MATLAB Robotic Toolbox 是一个强大的工具包,提供了各种用于机器人建模、仿真和控制的函数和类。它包含了...

    swfobject_modified.js

    swfobject_modified.js

    网络图片缓存实现

    例如,可以根据服务器返回的Last-Modified或ETag头部信息判断图片是否已更新,如果已更新,则重新下载并更新缓存。 在给定的文件`LibCacheBitmap_v1.4.jar`和`LibCacheBitmap_v1.4`中,可能包含了一个名为...

    matlab开发-lastmodified

    在MATLAB开发环境中,"lastmodified"是一个与文件系统交互的功能,主要用于获取目录或全局范围内最后被修改的文件的相关信息。这个功能对于数据科学家和机器学习领域的从业者来说尤其有用,因为他们经常需要处理大量...

    Modified SpriteOutline.rar

    Unity3D Modified SpriteOutline.cs : http://pastebin.com/swHe5L8u Modified Sprites-Outline.shader : http://pastebin.com/zzbKVJBM 网络分流

    Modified cuckoo search

    在给定的文件信息中,我们可以...该算法不仅在高维问题上表现出色,并且在一系列标准测试函数上,其性能优于或至少不劣于现有的几种优化算法,这对于工程、科学以及其他需要解决复杂优化问题的领域具有潜在的应用价值。

    使用IE或Edge浏览器通过SAS访问File文件只能访问一次问题分析-xiaoqiang1

    然而,当浏览器缓存了文件并再次请求时,如IE和Edge,它们会在后续请求中添加"If-Modified-Since"字段,询问服务器文件是否自上次访问以来有所更改。File Service不支持这种类型的条件请求头,导致请求失败。 对比...

Global site tag (gtag.js) - Google Analytics