Search

27.3. Local Server-Side Response Cache

download PDF
RESTEasy has a local, server-side, in-memory cache for your JAX-RS services. It automatically caches marshalled responses from HTTP GET JAX-RS invocations if your JAX-RS resource method sets a Cache-Control header. When a GET is received, the RESTEasy Server Cache checks whether the URI is stored in the cache. If true, the marshalled response is returned without invoking your JAX-RS method. Each cache entry has a maximum age for which the specifications in the Cache-Control header of the initial request are valid. The cache also automatically generates an ETag using an MD5 hash on the response body. This lets the client perform HTTP 1.1 cache revalidation with the IF-NONE-MATCH header. The cache will also perform revalidation if there is no initial cache hit, but the JAX-RS method returns a body with the same ETag.
To set up the server-side cache with Maven, you must use the resteasy-cache-core artifact:
<dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>resteasy-cache-core</artifactId>
   <version>1.1.GA</version>
</dependency>
Next, add a ServletContextListener: org.jboss.resteasy.plugins.cache.server.ServletServerCache. You must specify this after the ResteasyBootstrap listener in your web.xml file.
<web-app>
    <listener>
        <listener-class>
            org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        </listener-class>
    </listener>

    <context-param>
        <param-name>resteasy.server.cache.maxsize</param-name>
        <param-value>1000</param-value>
    </context-param>

    <context-param>
        <param-name>resteasy.server.cache.eviction.wakeup.interval</param-name>
        <param-value>5000</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.jboss.resteasy.plugins.cache.server.ServletServerCache
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/rest-services/*</url-pattern>
    </servlet-mapping>

</web-app>
The cache implementation is based on the JBoss Cache project. You can set two context-param configuration variables: resteasy.server.cache.maxsize sets the number of elements that can be cached, and resteasy.server.cache.eviction.wakeup.interval sets the rate at which the background eviction thread runs to purge the cache of stale entries.
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.