Eldy
Eldy

Reputation: 1096

Use copyOnRead behaviour in Ehcache 3 with XML configuration

I have a Spring Boot application using Ehcache 3.6. I'm using an XML file (ehcache.xml) to configure the cache.

I want to enable to enable the copyOnRead behaviour described in the documentation here: https://www.ehcache.org/documentation/2.8/configuration/configuration.html#copyonread-and-copyonwrite-cache-configuration

A cache can be configured to copy the data, rather than return reference to it on get or put. This is configured using the copyOnRead and copyOnWrite attributes of cache and defaultCache elements in your configuration [...]

However, this is the documentation for Ehcache 2.8. When I look at the equivalent documentation for Ehcache 3.6 (https://www.ehcache.org/documentation/3.6/xml.html), I'm unable to find information on the copyOnRead behaviour.

Is it still possible to enable the copyOnRead behaviour from the XML configuration file in Ehcache 3? If yes, how?

Upvotes: 1

Views: 120

Answers (1)

Vlad L
Vlad L

Reputation: 1694

If you look at the ehcache 2 implementation, it's using serialization to implement this.

This approach was retained in ehcache 3 and for XML configuration it is possible to reference their implementation of SerializingCopier by using this syntax:

    <cache alias="your-cache-name">
        <value-type copier="org.ehcache.impl.copy.SerializingCopier">
            com.example.demo.MyObject
        </value-type>
    </cache>

Running the app, when this copier is enabled I can see that the object being returned from the cache has a different address each time.

Without the copier the cache returns the same object each time.

Upvotes: 0

Related Questions