Search

27.2. Client "Browser" Cache

download PDF
RESTEasy can create a client-side, browser-like cache for use with the Client Proxy Framework or with raw ClientRequests. This cache locates Cache-Control headers that are returned with a server response. If the Cache-Control headers specify that the client may cache the response, RESTEasy caches it within local memory. This cache obeys max-age requirements, and automatically performs HTTP 1.1 cache revalidation if either or both of the Last-Modified or ETag headers are returned with the original response. (See the HTTP 1.1 specification for details about Cache-Control or cache revalidation.)
Enabling RESTEasy caching is simple. The following shows the client cache being used with the Client Proxy Framework:
@Path("/orders")
public interface OrderServiceClient {

   @Path("{id}")
   @GET
   @Produces("application/xml")
   public Order getOrder(@PathParam("id") String id);
}
You can create a proxy for this interface and enable caching for that proxy like so:
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.cache.CacheFactory;
import org.jboss.resteasy.client.cache.LightweightBrowserCache;

public static void main(String[] args) throws Exception
{
      RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
      OrderServiceClient proxy = ProxyFactory.create(OrderServiceClient.class, generateBaseUrl());

      // This line enables caching
      LightweightBrowserCache cache = CacheFactory.makeCacheable(proxy);
}
If you are using the ClientRequest class instead of the proxy server to perform invocations, you can enable the cache like so:
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.cache.CacheFactory;
import org.jboss.resteasy.client.cache.LightweightBrowserCache;

public static void main(String[] args) throws Exception
{
      RegisterBuiltin.register(ResteasyProviderFactory.getInstance());

      // This line enables caching
      LightweightBrowserCache cache = new LightweightBrowserCache();

      ClientRequest request = new ClientRequest("http://example.com/orders/333");
      CacheFactory.makeCacheable(request, cache);
}
By default, the LightweightBrowserCache has a maximum caching space of two megabytes. You can change this programmatically by calling the setMaxBytes() method. If the cache becomes full, all cached data will be deleted automatically. For more complex caching solutions, or support for third-party cache options, contact the resteasy-development list and discuss your ideas with the community.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.