📜  Java的.net.ResponseCache类在Java中

📅  最后修改于: 2022-05-13 01:55:33.571000             🧑  作者: Mango

Java的.net.ResponseCache类在Java中

Java的ResponseCache用于构造URLConnection缓存的实现,它指定必须缓存哪些资源以及需要缓存资源的持续时间。

可以通过执行以下操作使用系统创建 ResponseCache 实例:

使用上述语句创建的实例将调用 ResponseCache 的对象,以便:

  1. 用于将从外部源检索到的资源数据存储到缓存中。
  2. 用于根据请求获取已存储在缓存中的资源。
  3. 响应缓存可以通过Java.net包导入

ResponseCache 类的方法:

MethodDescription
get(URI uri, String rqstMethod, Map > rqstHeaders)This method is used for retrieving the cached response depending upon the requesting URI, request method and request headers.
getDefault()This method is used for retrieving the system-wide cache response.
put(URI uri, URLConnection conn)The protocol handler calls this method whenever a resource has been retrieved and the ResponseCache must decide whether to store the resource in its cache.
setDefault(ResponseCache responseCache)This method is used to set or unset the system-wide cache

ResponseCache 类的应用:

1.在Java.net包中,ResponseCache用于实现各种网络应用的资源缓存,例如:

  1. 电子邮件
  2. 文件传输
  3. 远程终端访问
  4. 加载网页

2. 在Java.net 中,ResponseCache 用于提取系统范围的响应缓存。

3. 在Java .net 中,ResponseCcahe 用于设置或取消设置系统范围的缓存。

用于实现Java .net.ResponseCache 的Java程序:

Java
import java.io.IOException;
import java.net.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class JavaResponseCacheExample1 {
    public static void main(String args[]) throws Exception
    {
  
        // passing the string uri
        String uri = "https://www.onlinegdb.com";
  
        // Calling the constructor of the URI class
        URI uri1 = new URI(uri);
  
        // passing the url
        URL url = new URL("http://www.onlinegdb.com");
  
        // calling the constructor of the URLConnection
        URLConnection urlcon = url.openConnection();
        ResponseCache responseCache = new ResponseCache() {
            // calling the abstract methods
            @Override
            public CacheResponse get(
                URI uri, String rqstMethod,
                Map > rqstHeaders)
                throws IOException
            {
                return null;
            }
  
            @Override
            public CacheRequest put(URI uri,
                                    URLConnection conn)
                throws IOException
            {
                return null;
            }
        };
  
        // The sets the system-wide response cache.
        ResponseCache.setDefault(responseCache);
  
        // The getDefault() method returns
        // the system-wide ResponseCache .
        System.out.println("Default value: "
                           + ResponseCache.getDefault());
        Map > maps
            = new HashMap >();
        List list = new LinkedList();
        list.add("REema");
  
        // put() method sets all the applicable cookies,
        // present in the response headers into a cookie
        // cache
        maps.put("1", list);
        System.out.println(
            "The put() method has been called...");
  
        // The put() method returns the
        // CacheRequest for recording
        System.out.println(
            "The put() method returns: "
            + responseCache.put(uri1, urlcon));
        System.out.println(
            "The get() method has been called...");
  
        // The get() method returns a CacheResponse
        // instance if it is available
        System.out.println(
            "The get() method returns: "
            + responseCache.get(uri1, uri, maps));
    }
}


输出 :