Friday 10 May 2024

Caching REST Resources In Jakarta REST

I found the following blog post on caching interesting. See reference [1]

I like caching, but I understand there's a very real danger of seeing outdated results. The Blogpost highlights this quite good.

HTTP Caching has several great options, apparently:

  • Expired Header (HTTP 1.0) - ResponseBuilder.expires
  • Cache-Control Header (HTTP 1.1) - CacheControl class for more control
  • ETags - EntityTag class, indicates a kind of hashcode to verify if a Resource has changed, without actually accessing the resource

Nice.

References

[1] Caching REST Resources In Jakarta REST (formerly JAX-RS)
https://blog.payara.fish/caching-rest-resources-in-jakarta-rest-formerly-jax-rs?hs_preview=dxtFzcIy-164496731149

Thursday 2 May 2024

Stream.sorted() - For unordered streams, no stability guarantees are made.

So I was writing code, and I had to sort a stream, because originally I had a Set, and everyone knows Sets are unordered by default.

But my eye fell on the javadoc on .sorted(). To be more specific:

For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.

And the sentence kind of made me nervous.

Example

An example of an unstable sort is visible in the unit test below. The unit test will sometimes pass and sometimes fail.

The reason for this is that Ken Thompson and Simon Thompson are equivalent in the sorting and, as it is an unsorted stream to begin with, its order can change between program runs.

Something to keep in mind.

References

Oracle Javadoc - Stream (Java SE 21 & JDK 21)
https://docs.oracle.com/en%2Fjava%2Fjavase%2F21%2Fdocs%2Fapi%2F%2F/java.base/java/util/stream/Stream.html#sorted()