EhCache cached object modification
<p>I've to implement caching with <a href="http://www.ehcache.org/documentation/2.8/index.html" rel="nofollow noreferrer">EhCache</a>. Basic requirement is, I have to keep that cached object for fixed interval ( for now 1 hours in code below). So, I implemented the code as below:</p>

<p>Sample domain object:</p>

<pre><code>import lombok.*;
@Getter
@Setter
@ToString
@AllArgsConstructor
public class City implements Serializable {
    public String name;
    public String country;
    public int population;
}
</code></pre>

<p>Cache manager class:</p>

<pre><code>import net.sf.ehcache.*;
public class JsonObjCacheManager {
    private static final Logger logger = LoggerFactory.getLogger(JsonObjCacheManager.class);
    private CacheManager manager;

    private Cache objectCache;

    public JsonObjCacheManager(){
        manager = CacheManager.create();

        objectCache =  manager.getCache("jsonDocCache");

        if( objectCache == null){
            objectCache = new Cache(
                    new CacheConfiguration("jsonDocCache", 1000)
                            .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU)
                            .eternal(false)
                            .timeToLiveSeconds(60 * 60)
                            .timeToIdleSeconds(0)
                            .diskExpiryThreadIntervalSeconds(0)
                            .persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP)));
            objectCache.disableDynamicFeatures();
            manager.addCache(objectCache);
        }
    }

    public List&lt;String&gt; getKeys() { return objectCache.getKeys();}

    public void clearCache(){
        manager.removeAllCaches();
    }

    public void putInCache(String key, Object value){
        try{
            objectCache.put(new Element(key, value));
        }catch (CacheException e){
            logger.error(String.format( "Problem occurred while putting data into cache: %s", e.getMessage()));
        }
    }

    public Object retrieveFromCache(String key){
        try {
            Element element = objectCache.get(key);
            if(element != null)
                return element.getObjectValue();
        }catch (CacheException ce){
            logger.error(String.format("Problem occurred while trying to retrieveSpecific from cache: %s", ce.getMessage()));
        }
        return null;
    }
}
</code></pre>

<p>It caches and retrieves the values very properly. But my requirement is, I must modify the object that I retrieve from cache for given key. What I'm getting is, if I modify the object that I retrieved from cache, then cached object for that key is also getting modified.</p>

<p>Below is the example:</p>

<pre><code>    public class Application {

        public static void main(String[] args) {
            JsonObjCacheManager manager = new JsonObjCacheManager();

            final City city1 = new City("ATL","USA",12100);
            final City city2 = new City("FL","USA",12000);

            manager.putInCache(city1.getName(), city1);
            manager.putInCache(city2.getName(), city2);

            System.out.println(manager.getKeys());

            for(String key: manager.getKeys()){
                System.out.println(key + ": "+ manager.retrieveFromCache(key));
            }

            City cityFromCache = (City) manager.retrieveFromCache(city1.getName());
            cityFromCache.setName("KTM");
            cityFromCache.setCountry("NPL");
            System.out.println(manager.getKeys());

            for(String key: manager.getKeys()){
                System.out.println(key  + ": "+ manager.retrieveFromCache(key));
            }
        } 
}
</code></pre>

<p>The output that I'm getting is:</p>

<pre><code>[ATL, FL]
ATL: City(name=ATL, country=USA, population=12100)
FL: City(name=FL, country=USA, population=12000)
[ATL, FL]
ATL: City(name=KTM, country=NPL, population=12100)
FL: City(name=FL, country=USA, population=12000)
</code></pre>

<p>This means, whenever I'm retrieving and modifying the object for given key, it also being reflected in cached value. </p>

<p>What my requirement is, the cached object for given key should not be modified. Is there any way to achieve this? Or is it not correct way to implement EhCache? Or I'm missing some fundamental principle?</p>

<p>I'm using EhCache V2.10.3</p>

<p>Thank you!</p>