网页实现缓存

 C#中禁止cache的方法!

+展开
-C#
Response.Buffer = true;
Response.ExpiresAbsolute=System.DateTime.Now.AddSeconds(-1);
Response.Expires=0;
Response.CacheControl= "no-cache";

服务端缓存有System.Web.Caching.cahe和memcached

当然System.Web.Caching.cahe是微软写的类,而memcached是第三方插件。System.Web.Caching.cache目前还不是分布式缓存,只能在一台电脑上(07时候),而

memcached是分布式的高速缓存。

服务器缓存可分为三种缓存

1.输出缓存:

要实现页面输出缓存,只要将一条 OutputCache 指令添加到页面即可。

<%@ OutputCache Duration="60" VaryByParam="*" %>

如同其他页面指令一样,该指令应该出现在 ASPX 页面的顶部,即在任何输出之前。它支持五个属性(或参数),其中两个是必需的。

Duration

必需属性。页面应该被缓存的时间,以秒为单位。必须是正整数。

Location

指定应该对输出进行缓存的位置。如果要指定该参数,则必须是下列选项之一:Any、Client、Downstream、None、Server 或 ServerAndClient。

VaryByParam

必需属性。Request 中变量的名称,这些变量名应该产生单独的缓存条目。“none” 表示没有变动。“*” 可用于为每个不同的变量数组创建新的缓存条目。变量之间用 “;” 进行分隔。

VaryByHeader

基于指定的标头中的变动改变缓存条目。

VaryByCustom

允许在 global.asax 中指定自定义变动(例如,“Browser”)。

2.片段缓存:

示例

<%@ OutputCache Duration="60" VaryByParam="*" %> 该示例将缓存用户控件60秒,并且将针对查询字符串的每个变动、针对此控件所在的每个页面创建单独的缓存条目。<%@ OutputCache Duration="60" VaryByParam="none"
VaryByControl="CategoryDropDownList" %> 该示例将缓存用户控件60秒,并且将针对CategoryDropDownList控件的每个不同的值、针对此控件所在的每个页面创建单独的缓存条目。<%@ OutputCache Duration="60" VaryByParam="none" VaryByCustom="browser"
Shared="true" %>



最后,该示例将缓存用户控件60秒,并且将针对每个浏览器名称和主要版本创建一个缓存条目。然后,每个浏览器的缓存条目将由引用此用户控件的所有页面共享(只要所有页面都用相同的ID引用该控件即可)。

3.编程用得最多的缓存:数据缓存

System.Web.Caching.cahe被httpRuntime.Cache或HttpContext.Current.Cache实例化。httpRuntime.Cache,HttpContext.Current.Cache是内置对象就想当与seesion。

HttpContext.Current.Cache与HttpRuntime.Cache区别:

HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。 HttpRuntime.Cache:获取当前应用程序的Cache。HttpContext.Current.Cache是调用了 HttpRuntime.Cache,且HttpContext.Current.Cache是用在web程序上,而HttpRuntime.Cache是用在任何程序上。System.web命名空间下。

其实HttpContext.Current.Cache是通过 HttpRuntime.Cache来实现的,所以一般实例化最好通过HttpRuntime.Cache来实例化:

例如:System.Web.Caching.Cache cache = HttpRuntime.Cache

System.Web.Caching.Cache有很多方法,但方法中add中是存缓存

参数中有缓存时间,依赖项。

缓存时间即到当缓存的东西达到指定时间就让缓存失效,而依赖项是当依赖项发生变化就会使缓存失效。

依赖项有一般依赖项 CacheDependency和数据库依赖项SqlCacheDependency。

虽然CacheDependency类完成了很重要的功能,但其组成结构却比较简单,主要有两个属性和一个方法。

— 属性“HasChanged”:判断CacheDependency对象是否已更改。

— 属性“UtcLastModified”:返回上次依赖项的修改日期

— 方法“Dispose”:释放CacheDependency对象所占有的资源。因为缓存类继承了接口“IDispose”,所以必须实现此方法。

CacheDependency

例如 Cache.Insert(“key”, myXMLFileData, DateTime.Now.AddMinutes(1),,new

System.Web.Caching.CacheDependency(Server.MapPath(“users.xml”)));

users.xml文件就相当一般依赖项,当xml文件被改了,则此缓存失效。

SqlCacheDependency一般是增对数据库的,这个设置需要在config里设置,而且还要启动数据库的这样服务。

Cache.Insert 中可设置跟数据库中那个表关联,一旦表有变化就会导致cache失效。

可到网上查询如何使用

类别:Asp.Net/C#/WCF 作者: 转载 日期:2009-07-06 【 评论:0 阅读:182】  繁體中文

public string GetNBBarData()
    {
        string strContent = "";
        if (HttpContext.Current.Cache["NBBar"] != null)

           {
              strContent = (string)HttpContext.Current.Cache["NBBar"];
          }
        else
        {
            string StrUrl = "http://www.126.com";

            System.Net.WebClient wc = new System.Net.WebClient();
            byte[] buffer = wc.DownloadData(new Uri(StrUrl));
            strContent = Encoding.UTF8.GetString(buffer);     // 获取网页内容
            HttpContext.Current.Cache.Insert("NBBar", strContent, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
        }
        return strContent;
    }


http://www.divmy.com/

转载地址
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是springboot实现网页图片缓存到本地的步骤: 1. 在pom.xml文件中添加以下依赖: ``` <!-- Spring Boot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Apache Commons IO --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> ``` 2. 创建一个名为FileUtil的工具类,用于保存图片到本地文件系统中: ``` import org.apache.commons.io.FileUtils; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; public class FileUtil { public static void saveImage(MultipartFile image, String path, String filename) throws IOException { File file = new File(path); if (!file.exists()) { file.mkdirs(); } FileUtils.copyInputStreamToFile(image.getInputStream(), new File(path, filename)); } } ``` 3. 在Controller中添加以下方法,用于下载网页中的图片并保存到本地: ``` import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.net.URL; import java.util.List; import java.util.UUID; @Controller public class HomeController { @Value("${image.path}") private String imagePath; @GetMapping("/") public String index() { return "index"; } @PostMapping("/") public String uploadImages(@RequestParam("url") String url, Model model) throws IOException { List<String> imageUrls = HTMLUtil.getImageUrls(url); for (String imageUrl : imageUrls) { URL imageLink = new URL(imageUrl); String imageFileType = imageUrl.substring(imageUrl.lastIndexOf('.') + 1); String imageName = UUID.randomUUID().toString() + "." + imageFileType; FileUtil.saveImage(imageLink.openStream(), imagePath, imageName); } model.addAttribute("message", "Images saved successfully"); return "index"; } } ``` 4. 在application.properties文件中添加以下配置,用于指定保存图片的目录: ``` image.path=/tmp/images/ ``` 5. 创建一个名为HTMLUtil的工具类,用于从网页中提取图片的链接: ``` import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class HTMLUtil { public static List<String> getImageUrls(String url) throws IOException { Document doc = Jsoup.connect(url).get(); List<String> imageUrls = new ArrayList<>(); doc.select("img").forEach(element -> { String src = element.attr("src"); if (!src.startsWith("http")) { src = "http://" + src; } imageUrls.add(src); }); return imageUrls; } } ``` 现在,当用户提交一个网址时,程序会提取出网页中的所有图片,将它们缓存到本地目录中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值