public static void main(final String[] args) throws Exception {

        final HttpHost target = new HttpHost("https", "www.apache.org");

        try (final CloseableHttpClient httpclient = CachingHttpClients.custom()
                .setCacheConfig(CacheConfig.custom()
                        .setMaxObjectSize(200000)
                        .setHeuristicCachingEnabled(true)
                        .build())
                .setResourceFactory(HeapResourceFactory.INSTANCE)
                .build()) {

            final ClassicHttpRequest httpget1 = ClassicRequestBuilder.get()
                    .setHttpHost(target)
                    .setPath("/")
                    .build();

            // Use default cache control
            final HttpCacheContext context = CacheContextBuilder.create()
                    .setCacheControl(RequestCacheControl.DEFAULT)
                    .build();

            System.out.println("Executing request " + httpget1.getMethod() + " " + httpget1.getUri());
            httpclient.execute(httpget1, context, response -> {
                System.out.println("----------------------------------------");
                System.out.println(httpget1 + "->" + new StatusLine(response));
                EntityUtils.consume(response.getEntity());
                System.out.println("Cache status: " + context.getCacheResponseStatus());
                System.out.println("Request cache control: " + context.getRequestCacheControlOrDefault());
                System.out.println("Response cache control: " + context.getResponseCacheControlOrDefault());
                final HttpCacheEntry cacheEntry = context.getCacheEntry();
                if (cacheEntry != null) {
                    System.out.println("Cache entry resource: " + cacheEntry.getResource());
                    System.out.println("Date: " + cacheEntry.getInstant());
                    System.out.println("Expires: " + cacheEntry.getExpires());
                    System.out.println("Last modified: " + cacheEntry.getLastModified());
                }
                return null;
            });

            final ClassicHttpRequest httpget2 = ClassicRequestBuilder.get()
                    .setHttpHost(target)
                    .setPath("/")
                    .build();

            // Ensure a custom freshness for the cache entry
            context.setRequestCacheControl(RequestCacheControl.builder()
                    .setMinFresh(100)
                    .build());

            System.out.println("Executing request " + httpget2.getMethod() + " " + httpget2.getUri());
            httpclient.execute(httpget2, context, response -> {
                System.out.println("----------------------------------------");
                System.out.println(httpget2 + "->" + new StatusLine(response));
                EntityUtils.consume(response.getEntity());
                System.out.println("Cache status: " + context.getCacheResponseStatus());
                System.out.println("Request cache control: " + context.getRequestCacheControlOrDefault());
                System.out.println("Response cache control: " + context.getResponseCacheControlOrDefault());
                final HttpCacheEntry cacheEntry = context.getCacheEntry();
                if (cacheEntry != null) {
                    System.out.println("Cache entry resource: " + cacheEntry.getResource());
                    System.out.println("Date: " + cacheEntry.getInstant());
                    System.out.println("Expires: " + cacheEntry.getExpires());
                    System.out.println("Last modified: " + cacheEntry.getLastModified());
                }
                return null;
            });

            Thread.sleep(2000);

            final ClassicHttpRequest httpget3 = ClassicRequestBuilder.get()
                    .setHttpHost(target)
                    .setPath("/")
                    .build();

            // Try to force cache entry re-validation
            context.setRequestCacheControl(RequestCacheControl.builder()
                    .setMaxAge(0)
                    .build());

            System.out.println("Executing request " + httpget3.getMethod() + " " + httpget3.getUri());
            httpclient.execute(httpget3, context, response -> {
                System.out.println("----------------------------------------");
                System.out.println(httpget3 + "->" + new StatusLine(response));
                EntityUtils.consume(response.getEntity());
                System.out.println("Cache status: " + context.getCacheResponseStatus());
                System.out.println("Request cache control: " + context.getRequestCacheControlOrDefault());
                System.out.println("Response cache control: " + context.getResponseCacheControlOrDefault());
                final HttpCacheEntry cacheEntry = context.getCacheEntry();
                if (cacheEntry != null) {
                    System.out.println("Cache entry resource: " + cacheEntry.getResource());
                    System.out.println("Date: " + cacheEntry.getInstant());
                    System.out.println("Expires: " + cacheEntry.getExpires());
                    System.out.println("Last modified: " + cacheEntry.getLastModified());
                }
                return null;
            });
        }


    }
This is an example demonstrating how to control execution of cache
operation and determine its outcome using the classic HTTP cache API.
httpclient5-cache/src/test/java/org/apache/hc/client5/http/cache/example/ClientCacheControl.java