Reputation: 547
I'm using EHCache on a java project. I got a problem on this code. My object is cached at this time !
Element lElem = cacheManager.get("KEY");
if (lElem != null) {
// Get my cached objects
lLMyVO = (List<MyVO>) lElem.getObjectValue();
}else{
// Do something to set element
}
// Add some elements on lLMyVO list :
for (MyVO lAnotherMyVO : lAnotherList) {
lLMyVO .add(lMyVO );
}
At this point, my lLMyVO
list has elements of lAnotherList
! And that's ok. But in my EHCache object elements of lAnotherList
are added too ! And it's not ok, I don't want to change the cache.
It seems that lLMyVO
it's not a new object but just something like a reference to the object in cache.
Do you know the way to do ?
Upvotes: 0
Views: 2309
Reputation: 303
(This may not be ideal solution, just giving another option)
To add to @Alexey's answer.. If you have only a few scenarios where mutation can occur, you can simply opt for cloning the object after retrieving from EHCache. This may save some overhead of copying it every time.
Upvotes: 0
Reputation: 2916
By default your elements will be stored as references in EHCache until, file write or other replication appears. To prevent that you can use 'copyOnWrite'/'copyOnRead' attributes in your cache configuration. So objects will be copied on your put/get operations and no mutations will be allowed.
Ehcache documentation for reference
Upvotes: 3