Okhttp3源码(五) ------ CacheControl

在学习本篇博客的朋友,如果你对Http首部字段的Cache-Control 字段值不太了解的话,这里强烈建议参见

Http首部的字段及相应的取值内容

了解Cache-Control字段值的朋友,都知道它的一个特点,就是——多!
为了简化对缓存的相关操作,OkHttp3对它们做了封装,几乎每一个字段值对应一个属性。

我们看一下它的源码。

1、CacheControl的属性

//下面这些属性就是多Cache-Control字段值的封装。
private final boolean noCache;
private final boolean noStore;
private final int maxAgeSeconds;
private final int sMaxAgeSeconds;
private final boolean isPrivate;
private final boolean isPublic;
private final boolean mustRevalidate;
private final int maxStaleSeconds;
private final int minFreshSeconds;
private final boolean onlyIfCached;
private final boolean noTransform;

//两个默认的实现,第一个是直接从源服务器拿资源,第二个是直接从缓存服务器的缓存中拿资源
public static final CacheControl FORCE_NETWORK = new Builder().noCache().build();
public static final CacheControl FORCE_CACHE = new Builder()
.onlyIfCached()
.maxStale(Integer.MAX_VALUE, TimeUnit.SECONDS)
.build();

是不是觉得属性挺多的!不过不要紧,如果你这是想弄明白OkHttp3的实现原理及相关类的构造,这里你就不必全部记下来,只要看到了知道是什么就行。

2、构造函数

private CacheControl(boolean noCache, boolean noStore, int maxAgeSeconds, int sMaxAgeSeconds,
      boolean isPrivate, boolean isPublic, boolean mustRevalidate, int maxStaleSeconds,
      int minFreshSeconds, boolean onlyIfCached, boolean noTransform, String headerValue) {
    this.noCache = noCache;
    this.noStore = noStore;
    this.maxAgeSeconds = maxAgeSeconds;
    this.sMaxAgeSeconds = sMaxAgeSeconds;
    this.isPrivate = isPrivate;
    this.isPublic = isPublic;
    this.mustRevalidate = mustRevalidate;
    this.maxStaleSeconds = maxStaleSeconds;
    this.minFreshSeconds = minFreshSeconds;
    this.onlyIfCached = onlyIfCached;
    this.noTransform = noTransform;
    this.headerValue = headerValue;
  }

  CacheControl(Builder builder) {
    this.noCache = builder.noCache;
    this.noStore = builder.noStore;
    this.maxAgeSeconds = builder.maxAgeSeconds;
    this.sMaxAgeSeconds = -1;
    this.isPrivate = false;
    this.isPublic = false;
    this.mustRevalidate = false;
    this.maxStaleSeconds = builder.maxStaleSeconds;
    this.minFreshSeconds = builder.minFreshSeconds;
    this.onlyIfCached = builder.onlyIfCached;
    this.noTransform = builder.noTransform;
  }

CacheControl的构造函数,只是对属性赋值。我们可以看到这两个构造函数的权限都不太友好,不让用户自己直接构造。
看第二个构造函数,是不是觉得挺熟悉的,没错!CacheControl也是通过Builder设计模式给相关属性赋值的

3、CacheControl的主要方法

public boolean noCache() {
return noCache;
}
public boolean noStore() {
return noStore;
}
public int maxAgeSeconds() {
return maxAgeSeconds;
}
public int sMaxAgeSeconds() {
return sMaxAgeSeconds;
}
public boolean isPrivate() {
return isPrivate;
}
public boolean isPublic() {
return isPublic;
}
public boolean mustRevalidate() {
return mustRevalidate;
}
public int maxStaleSeconds() {
return maxStaleSeconds;
}
public int minFreshSeconds() {
return minFreshSeconds;
}
public boolean onlyIfCached() {
return onlyIfCached;
}
public boolean noTransform() {
return noTransform;
}
//就是根据headers中设置的Cache-Control的字段值来初始化CacheControl属性,并返回该CacheControl对象。
public static CacheControl parse(Headers headers) {
}

上面的方法大部分是返回相应属性的,从这一点也可以看出,CacheControl在功能上并没有什么实际用途,只是封装Cache-Control字段值用的
我们知道Cache-Control字段是在请求头被附上各种值得,所以这里通过最后一个方法来解析请求头实例化CacheControl对象。

4、Buildler

扫描二维码关注公众号,回复: 2612604 查看本文章
//Builder模式这里就不细讲了,相信你如果对前面几篇博客分析后,在这里扫一眼注释就明白了。
public static final class Builder {
//Builder里面的方法主要是对下面的属性赋值。
boolean noCache;
boolean noStore;
int maxAgeSeconds = -1;
int maxStaleSeconds = -1;
int minFreshSeconds = -1;
boolean onlyIfCached;
boolean noTransform;
//。。。
// 。。。
// 。。。
// 。。。
}
}

通过给请求Request设置CacheControl后,后面自动会将里面的内容加到Header中,最后内容随着Header一起写入到服务器。

学习心得:观看资料 + 思考问题 + 实践证明 + 总结概括 + 整理记录 = 有思想的技术大牛。

猜你喜欢

转载自blog.csdn.net/look_Future/article/details/79730667