Skip to content

Make CacheControl immutable #33366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 68 additions & 49 deletions spring-web/src/main/java/org/springframework/http/CacheControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,51 +51,64 @@
*/
public class CacheControl {

private static final CacheControl EMPTY = new CacheControl(null, false, false, false,
false, false, false, false, null, null,
null, false);

@Nullable
private Duration maxAge;
private final Duration maxAge;

private boolean noCache = false;
private final boolean noCache;

private boolean noStore = false;
private final boolean noStore;

private boolean mustRevalidate = false;
private final boolean mustRevalidate;

private boolean noTransform = false;
private final boolean noTransform;

private boolean cachePublic = false;
private final boolean cachePublic;

private boolean cachePrivate = false;
private final boolean cachePrivate;

private boolean proxyRevalidate = false;
private final boolean proxyRevalidate;

@Nullable
private Duration staleWhileRevalidate;
private final Duration staleWhileRevalidate;

@Nullable
private Duration staleIfError;
private final Duration staleIfError;

@Nullable
private Duration sMaxAge;

private boolean immutable = false;


/**
* Create an empty CacheControl instance.
* @see #empty()
*/
protected CacheControl() {
private final Duration sMaxAge;

private final boolean immutable;

private CacheControl(@Nullable Duration maxAge, boolean noCache, boolean noStore,
boolean mustRevalidate, boolean noTransform, boolean cachePublic,
boolean cachePrivate, boolean proxyRevalidate, @Nullable Duration staleWhileRevalidate,
@Nullable Duration staleIfError, @Nullable Duration sMaxAge, boolean immutable) {
this.maxAge = maxAge;
this.noCache = noCache;
this.noStore = noStore;
this.mustRevalidate = mustRevalidate;
this.noTransform = noTransform;
this.cachePublic = cachePublic;
this.cachePrivate = cachePrivate;
this.proxyRevalidate = proxyRevalidate;
this.staleWhileRevalidate = staleWhileRevalidate;
this.staleIfError = staleIfError;
this.sMaxAge = sMaxAge;
this.immutable = immutable;
}


/**
* Return an empty directive.
* <p>This is well suited for using other optional directives without "max-age",
* "no-cache" or "no-store".
* @return {@code this}, to facilitate method chaining
*/
public static CacheControl empty() {
return new CacheControl();
return EMPTY;
}

/**
Expand Down Expand Up @@ -132,9 +145,8 @@ public static CacheControl maxAge(long maxAge, TimeUnit unit) {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.8">rfc7234 section 5.2.2.8</a>
*/
public static CacheControl maxAge(Duration maxAge) {
CacheControl cc = new CacheControl();
cc.maxAge = maxAge;
return cc;
return new CacheControl(maxAge, false, false, false, false, false, false, false,
null, null, null, false);
}

/**
Expand All @@ -150,9 +162,8 @@ public static CacheControl maxAge(Duration maxAge) {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.2">rfc7234 section 5.2.2.2</a>
*/
public static CacheControl noCache() {
CacheControl cc = new CacheControl();
cc.noCache = true;
return cc;
return new CacheControl(null, true, false, false, false, false, false, false,
null, null, null, false);
}

/**
Expand All @@ -163,9 +174,8 @@ public static CacheControl noCache() {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.3">rfc7234 section 5.2.2.3</a>
*/
public static CacheControl noStore() {
CacheControl cc = new CacheControl();
cc.noStore = true;
return cc;
return new CacheControl(null, false, true, false, false, false, false, false,
null, null, null, false);
}


Expand All @@ -178,8 +188,9 @@ public static CacheControl noStore() {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.1">rfc7234 section 5.2.2.1</a>
*/
public CacheControl mustRevalidate() {
this.mustRevalidate = true;
return this;
return new CacheControl(this.maxAge, this.noCache, this.noStore, true, this.noTransform,
this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
}

/**
Expand All @@ -191,8 +202,9 @@ public CacheControl mustRevalidate() {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.4">rfc7234 section 5.2.2.4</a>
*/
public CacheControl noTransform() {
this.noTransform = true;
return this;
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, true,
this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
}

/**
Expand All @@ -204,8 +216,9 @@ public CacheControl noTransform() {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.5">rfc7234 section 5.2.2.5</a>
*/
public CacheControl cachePublic() {
this.cachePublic = true;
return this;
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
true, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
}

/**
Expand All @@ -216,8 +229,9 @@ public CacheControl cachePublic() {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.6">rfc7234 section 5.2.2.6</a>
*/
public CacheControl cachePrivate() {
this.cachePrivate = true;
return this;
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
this.cachePublic, true, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
}

/**
Expand All @@ -228,8 +242,9 @@ public CacheControl cachePrivate() {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.7">rfc7234 section 5.2.2.7</a>
*/
public CacheControl proxyRevalidate() {
this.proxyRevalidate = true;
return this;
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
this.cachePublic, this.cachePrivate, true, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
}

/**
Expand All @@ -256,8 +271,9 @@ public CacheControl sMaxAge(long sMaxAge, TimeUnit unit) {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.9">rfc7234 section 5.2.2.9</a>
*/
public CacheControl sMaxAge(Duration sMaxAge) {
this.sMaxAge = sMaxAge;
return this;
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, sMaxAge, this.immutable);
}

/**
Expand Down Expand Up @@ -290,8 +306,9 @@ public CacheControl staleWhileRevalidate(long staleWhileRevalidate, TimeUnit uni
* @see <a href="https://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a>
*/
public CacheControl staleWhileRevalidate(Duration staleWhileRevalidate) {
this.staleWhileRevalidate = staleWhileRevalidate;
return this;
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
this.cachePublic, this.cachePrivate, this.proxyRevalidate, staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
}

/**
Expand All @@ -318,8 +335,9 @@ public CacheControl staleIfError(long staleIfError, TimeUnit unit) {
* @see <a href="https://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a>
*/
public CacheControl staleIfError(Duration staleIfError) {
this.staleIfError = staleIfError;
return this;
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
staleIfError, this.sMaxAge, this.immutable);
}

/**
Expand All @@ -333,8 +351,9 @@ public CacheControl staleIfError(Duration staleIfError) {
* @see <a href="https://tools.ietf.org/html/rfc8246">rfc8246</a>
*/
public CacheControl immutable() {
this.immutable = true;
return this;
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, true);
}

/**
Expand Down
Loading